|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIsTimeoutError(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + err error |
| 14 | + want bool |
| 15 | + }{ |
| 16 | + {"nil", nil, false}, |
| 17 | + {"DeadlineExceeded", context.DeadlineExceeded, true}, |
| 18 | + {"wrapped DeadlineExceeded", errors.Join(errors.New("wrap"), context.DeadlineExceeded), true}, |
| 19 | + {"timeout in message", errors.New("connection timeout"), true}, |
| 20 | + {"Timeout in message", errors.New("connection Timeout"), true}, |
| 21 | + {"i/o timeout", errors.New("read tcp: i/o timeout"), true}, |
| 22 | + {"deadline exceeded in message", errors.New("context deadline exceeded"), true}, |
| 23 | + {"other error", errors.New("something went wrong"), false}, |
| 24 | + } |
| 25 | + for _, tt := range tests { |
| 26 | + t.Run(tt.name, func(t *testing.T) { |
| 27 | + if got := IsTimeoutError(tt.err); got != tt.want { |
| 28 | + t.Errorf("IsTimeoutError() = %v, want %v", got, tt.want) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestOnTimeout_SuccessFirstTry(t *testing.T) { |
| 35 | + ctx := context.Background() |
| 36 | + calls := 0 |
| 37 | + err := OnTimeout(ctx, 3, 10*time.Millisecond, func() error { |
| 38 | + calls++ |
| 39 | + return nil |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + t.Errorf("OnTimeout() err = %v, want nil", err) |
| 43 | + } |
| 44 | + if calls != 1 { |
| 45 | + t.Errorf("expected 1 call, got %d", calls) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestOnTimeout_NonTimeoutErrorNoRetry(t *testing.T) { |
| 50 | + ctx := context.Background() |
| 51 | + wantErr := errors.New("permanent error") |
| 52 | + calls := 0 |
| 53 | + err := OnTimeout(ctx, 3, 10*time.Millisecond, func() error { |
| 54 | + calls++ |
| 55 | + return wantErr |
| 56 | + }) |
| 57 | + if !errors.Is(err, wantErr) { |
| 58 | + t.Errorf("OnTimeout() err = %v, want %v", err, wantErr) |
| 59 | + } |
| 60 | + if calls != 1 { |
| 61 | + t.Errorf("expected 1 call (no retry on non-timeout), got %d", calls) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestOnTimeout_RetryThenSuccess(t *testing.T) { |
| 66 | + ctx := context.Background() |
| 67 | + calls := 0 |
| 68 | + err := OnTimeout(ctx, 3, 5*time.Millisecond, func() error { |
| 69 | + calls++ |
| 70 | + if calls < 2 { |
| 71 | + return context.DeadlineExceeded |
| 72 | + } |
| 73 | + return nil |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("OnTimeout() err = %v, want nil", err) |
| 77 | + } |
| 78 | + if calls != 2 { |
| 79 | + t.Errorf("expected 2 calls (retry then success), got %d", calls) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestOnTimeout_ExhaustRetries(t *testing.T) { |
| 84 | + ctx := context.Background() |
| 85 | + calls := 0 |
| 86 | + err := OnTimeout(ctx, 3, 5*time.Millisecond, func() error { |
| 87 | + calls++ |
| 88 | + return context.DeadlineExceeded |
| 89 | + }) |
| 90 | + if err != context.DeadlineExceeded { |
| 91 | + t.Errorf("OnTimeout() err = %v, want DeadlineExceeded", err) |
| 92 | + } |
| 93 | + if calls != 3 { |
| 94 | + t.Errorf("expected 3 calls (all retries), got %d", calls) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestOnTimeout_ContextCanceled(t *testing.T) { |
| 99 | + ctx, cancel := context.WithCancel(context.Background()) |
| 100 | + cancel() |
| 101 | + calls := 0 |
| 102 | + err := OnTimeout(ctx, 3, 100*time.Millisecond, func() error { |
| 103 | + calls++ |
| 104 | + return context.DeadlineExceeded |
| 105 | + }) |
| 106 | + if err != context.Canceled { |
| 107 | + t.Errorf("OnTimeout() err = %v, want context.Canceled", err) |
| 108 | + } |
| 109 | + if calls != 1 { |
| 110 | + t.Errorf("expected 1 call then context cancel, got %d", calls) |
| 111 | + } |
| 112 | +} |
0 commit comments