|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// Poll tests |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +func TestPoll_SuccessOnFirstCheck(t *testing.T) { |
| 15 | + err := Poll(context.Background(), 10*time.Millisecond, time.Second, func(_ context.Context) (bool, error) { |
| 16 | + return true, nil |
| 17 | + }) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("expected nil, got %v", err) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestPoll_SuccessAfterSeveralChecks(t *testing.T) { |
| 24 | + calls := 0 |
| 25 | + err := Poll(context.Background(), 10*time.Millisecond, time.Second, func(_ context.Context) (bool, error) { |
| 26 | + calls++ |
| 27 | + return calls >= 3, nil |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("expected nil, got %v", err) |
| 31 | + } |
| 32 | + if calls < 3 { |
| 33 | + t.Fatalf("expected at least 3 calls, got %d", calls) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestPoll_Timeout(t *testing.T) { |
| 38 | + err := Poll(context.Background(), 10*time.Millisecond, 50*time.Millisecond, func(_ context.Context) (bool, error) { |
| 39 | + return false, nil |
| 40 | + }) |
| 41 | + if err == nil { |
| 42 | + t.Fatal("expected timeout error, got nil") |
| 43 | + } |
| 44 | + if !errors.Is(err, ErrTimeout) { |
| 45 | + t.Fatalf("expected ErrTimeout, got %v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestPoll_ContextCancellation(t *testing.T) { |
| 50 | + ctx, cancel := context.WithCancel(context.Background()) |
| 51 | + cancel() |
| 52 | + |
| 53 | + err := Poll(ctx, 10*time.Millisecond, time.Second, func(_ context.Context) (bool, error) { |
| 54 | + return false, nil |
| 55 | + }) |
| 56 | + if !errors.Is(err, context.Canceled) { |
| 57 | + t.Fatalf("expected context.Canceled, got %v", err) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestPoll_FatalErrorStopsPolling(t *testing.T) { |
| 62 | + fatal := errors.New("fatal failure") |
| 63 | + calls := 0 |
| 64 | + err := Poll(context.Background(), 10*time.Millisecond, time.Second, func(_ context.Context) (bool, error) { |
| 65 | + calls++ |
| 66 | + return false, fatal |
| 67 | + }) |
| 68 | + if !errors.Is(err, fatal) { |
| 69 | + t.Fatalf("expected fatal error, got %v", err) |
| 70 | + } |
| 71 | + if calls != 1 { |
| 72 | + t.Fatalf("expected 1 call, got %d", calls) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// --------------------------------------------------------------------------- |
| 77 | +// Do tests |
| 78 | +// --------------------------------------------------------------------------- |
| 79 | + |
| 80 | +func TestDo_SuccessOnFirstAttempt(t *testing.T) { |
| 81 | + err := Do(context.Background(), 3, 10*time.Millisecond, func(_ context.Context) error { |
| 82 | + return nil |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("expected nil, got %v", err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestDo_SuccessAfterRetries(t *testing.T) { |
| 90 | + calls := 0 |
| 91 | + err := Do(context.Background(), 5, 10*time.Millisecond, func(_ context.Context) error { |
| 92 | + calls++ |
| 93 | + if calls < 3 { |
| 94 | + return errors.New("not yet") |
| 95 | + } |
| 96 | + return nil |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("expected nil, got %v", err) |
| 100 | + } |
| 101 | + if calls != 3 { |
| 102 | + t.Fatalf("expected 3 calls, got %d", calls) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestDo_AllAttemptsExhausted(t *testing.T) { |
| 107 | + lastErr := errors.New("persistent failure") |
| 108 | + err := Do(context.Background(), 3, 10*time.Millisecond, func(_ context.Context) error { |
| 109 | + return lastErr |
| 110 | + }) |
| 111 | + if !errors.Is(err, lastErr) { |
| 112 | + t.Fatalf("expected last error, got %v", err) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestDo_ContextCancellationDuringDelay(t *testing.T) { |
| 117 | + ctx, cancel := context.WithCancel(context.Background()) |
| 118 | + calls := 0 |
| 119 | + err := Do(ctx, 5, time.Second, func(_ context.Context) error { |
| 120 | + calls++ |
| 121 | + if calls == 1 { |
| 122 | + // Cancel during the delay before the next retry. |
| 123 | + cancel() |
| 124 | + } |
| 125 | + return errors.New("fail") |
| 126 | + }) |
| 127 | + if !errors.Is(err, context.Canceled) { |
| 128 | + t.Fatalf("expected context.Canceled, got %v", err) |
| 129 | + } |
| 130 | + if calls != 1 { |
| 131 | + t.Fatalf("expected 1 call before cancellation, got %d", calls) |
| 132 | + } |
| 133 | +} |
0 commit comments