Skip to content

Commit

Permalink
Merge pull request #10 from Kyash/fix-timeout-conf-bug
Browse files Browse the repository at this point in the history
fix timeout bug
  • Loading branch information
convto authored Dec 26, 2022
2 parents a8d841e + daa4e7b commit a88f170
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions async_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func (a *asyncRetry) call(ctx context.Context, f RetryableFunc, config *Config)
return retry.Do(
func() error {
if config.timeout > 0 {
var timeoutCancel context.CancelFunc
ctx, timeoutCancel = context.WithTimeout(ctx, config.timeout)
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, config.timeout)
defer timeoutCancel()
return f(timeoutCtx)
}
return f(ctx)
},
Expand Down
9 changes: 8 additions & 1 deletion async_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func Test_asyncRetry_Do(t *testing.T) {
name: "Timeout set correctly for each try",
args: args{
f: func(ctx context.Context) error {
select {
case <-ctx.Done():
// Check ctx passed from async-retry is not closed at the start of f() processing.
return fmt.Errorf("context already closed")
default:
}

counter++
select {
case <-ctx.Done():
Expand All @@ -140,7 +147,7 @@ func Test_asyncRetry_Do(t *testing.T) {
},
opts: []Option{
Delay(1 * time.Millisecond),
Timeout(10 * time.Millisecond),
Timeout(1 * time.Second),
Attempts(5),
},
},
Expand Down

0 comments on commit a88f170

Please sign in to comment.