Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ func TestWithMaxRetries(t *testing.T) {
}
}

func TestWithMaxRetries_Zero(t *testing.T) {
t.Parallel()

// This test verifies the behavior of WithMaxRetries when the maximum number
// of retries is set to 0.
b := retry.WithMaxRetries(0, retry.BackoffFunc(func() (time.Duration, bool) {
return 1 * time.Second, false
}))

val, stop := b.Next()
if !stop {
t.Errorf("should stop")
}
if val != 0 {
t.Errorf("expected %v to be %v", val, 0)
}
}

func ExampleWithMaxRetries() {
ctx := context.Background()

Expand Down
31 changes: 31 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ func TestDo(t *testing.T) {
t.Errorf("expected %v to be %v", err, context.DeadlineExceeded)
}
})

t.Run("deadline_exceeded", func(t *testing.T) {
t.Parallel()

// This test verifies that the Do function immediately respects the context
// deadline, even if the backoff duration is significantly longer than the
// remaining context time. This is critical to ensure that the application
// does not hang or wait unnecessarily when the context has already
// expired.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

// Backoff is longer than context timeout
b := retry.NewConstant(20 * time.Millisecond)

start := time.Now()
err := retry.Do(ctx, b, func(_ context.Context) error {
return retry.RetryableError(fmt.Errorf("oops"))
})
if err == nil {
t.Fatal("expected err")
}
if got, want := err, context.DeadlineExceeded; got != want {
t.Errorf("expected %v to be %v", got, want)
}

// Should have returned roughly around the timeout, not the backoff
if got, want := time.Since(start), 15*time.Millisecond; got > want {
t.Errorf("expected %s to be less than %s", got, want)
}
})
}

func ExampleDo_simple() {
Expand Down
Loading