Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling extra params for DoRetryWithTimeout method to capture Test Case Name as well for SSIE runs #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ type ErrTimedOut struct {
Reason string
}

// ExtraArgs defines a function type that takes a pointer to AdditionalInfo and modifies it.
// This is used for passing and applying configuration options.
type ExtraArgs func(*AdditionalInfo)

// AdditionalInfo holds configuration details that can be modified through functional options.
// Currently, it only holds a TestName, but more fields can be added as needed.
type AdditionalInfo struct {
TestName string // TestName is a descriptor that can be used to identify or describe the test being performed.
}

// WithAdditionalInfo returns an ExtraArgs function that sets the TestName of an AdditionalInfo.
// This function is a functional option that allows callers to specify a test name for logging or identification purposes.
func WithAdditionalInfo(testName string) ExtraArgs {
return func(cfg *AdditionalInfo) {
cfg.TestName = testName // Sets the TestName field of the AdditionalInfo struct.
}
}

func (e *ErrTimedOut) Error() string {
errString := "timed out performing task."
if len(e.Reason) > 0 {
Expand All @@ -29,7 +47,15 @@ func (e *ErrTimedOut) Error() string {
// DoRetryWithTimeout performs given task with given timeout and timeBeforeRetry
// TODO(stgleb): In future I would like to add context as a first param to this function
// so calling code can cancel task.
func DoRetryWithTimeout(t func() (interface{}, bool, error), timeout, timeBeforeRetry time.Duration) (interface{}, error) {
func DoRetryWithTimeout(t func() (interface{}, bool, error), timeout, timeBeforeRetry time.Duration, opts ...ExtraArgs) (interface{}, error) {
args := &AdditionalInfo{}
for _, opt := range opts {
opt(args)
}
if len(opts) > 0 && args.TestName != "" {
log.Printf("In DoRetryWithTimeout method for test case: {%v}", args.TestName)
}

// Use context.Context as a standard go way of timeout and cancellation propagation amount goroutines.
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand All @@ -53,7 +79,11 @@ func DoRetryWithTimeout(t func() (interface{}, bool, error), timeout, timeBefore
if err != nil {
if retry {
errInRetires = append(errInRetires, err.Error())
log.Printf("DoRetryWithTimeout - Error: {%v}, Next try in [%v], timeout [%v]", err, timeBeforeRetry, timeout)
if len(opts) > 0 && args.TestName != "" {
log.Printf("DoRetryWithTimeout [%s] - Error: {%v}, Next try in [%v], timeout [%v]", args.TestName, err, timeBeforeRetry, timeout)
} else {
log.Printf("DoRetryWithTimeout - Error: {%v}, Next try in [%v], timeout [%v]", err, timeBeforeRetry, timeout)
}
time.Sleep(timeBeforeRetry)
} else {
errChan <- err
Expand All @@ -72,8 +102,12 @@ func DoRetryWithTimeout(t func() (interface{}, bool, error), timeout, timeBefore
return result, nil
case err := <-errChan:
if err == context.DeadlineExceeded {
timeoutReason := fmt.Sprintf("DoRetryWithTimeout timed out. Errors generated in retries: {%s}", strings.Join(errInRetires, "}\n{"))
if len(opts) > 0 && args.TestName != "" {
timeoutReason = fmt.Sprintf("DoRetryWithTimeout [%s] timed out. Errors generated in retries: {%s}", args.TestName, strings.Join(errInRetires, "}\n{"))
}
return nil, &ErrTimedOut{
Reason: fmt.Sprintf("DoRetryWithTimeout timed out. Errors generated in retries: {%s}", strings.Join(errInRetires, "}\n{")),
Reason: timeoutReason,
}
}

Expand Down