|
4 | 4 | package helpers |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "errors" |
8 | 9 | "reflect" |
9 | 10 | "strings" |
10 | 11 | "testing" |
| 12 | + "time" |
11 | 13 |
|
12 | 14 | "github.com/stretchr/testify/require" |
13 | 15 | "github.com/stretchr/testify/suite" |
@@ -46,33 +48,109 @@ func (suite *TestMiscSuite) SetupSuite() { |
46 | 48 | } |
47 | 49 | } |
48 | 50 |
|
49 | | -func (suite *TestMiscSuite) TestRetry() { |
50 | | - var count int |
51 | | - countFn := func() error { |
52 | | - count++ |
53 | | - if count < 4 { |
54 | | - return errors.New("count exceeded") |
| 51 | +func TestRetry(t *testing.T) { |
| 52 | + t.Run("RetriesWhenThereAreFailures", func(t *testing.T) { |
| 53 | + count := 0 |
| 54 | + logCount := 0 |
| 55 | + returnedErr := errors.New("always fail") |
| 56 | + countFn := func() error { |
| 57 | + count++ |
| 58 | + return returnedErr |
55 | 59 | } |
56 | | - return nil |
57 | | - } |
58 | | - var logCount int |
59 | | - loggerFn := func(_ string, _ ...any) { |
60 | | - logCount++ |
61 | | - } |
| 60 | + loggerFn := func(_ string, _ ...any) { |
| 61 | + logCount++ |
| 62 | + } |
| 63 | + |
| 64 | + err := Retry(countFn, 3, 0, loggerFn) |
| 65 | + require.ErrorIs(t, err, returnedErr) |
| 66 | + require.Equal(t, 3, count) |
| 67 | + require.Equal(t, 5, logCount) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("ContextCancellationBeforeStart", func(t *testing.T) { |
| 71 | + ctx, cancel := context.WithCancel(context.Background()) |
| 72 | + cancel() |
| 73 | + count := 0 |
| 74 | + fn := func() error { |
| 75 | + count++ |
| 76 | + return errors.New("Never here since context got cancelled") |
| 77 | + } |
| 78 | + logger := func(_ string, _ ...any) {} |
| 79 | + |
| 80 | + waitThatsNotCalled := 1000000 * time.Minute |
| 81 | + err := RetryWithContext(ctx, fn, 5, waitThatsNotCalled, logger) |
| 82 | + require.Equal(t, 0, count) |
| 83 | + require.ErrorIs(t, err, context.Canceled) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("ContextCancellationDuringExecution", func(t *testing.T) { |
| 87 | + ctx, cancel := context.WithCancel(context.Background()) |
| 88 | + |
| 89 | + count := 0 |
| 90 | + fn := func() error { |
| 91 | + count++ |
| 92 | + if count < 2 { |
| 93 | + return errors.New("fail") |
| 94 | + } |
| 95 | + cancel() |
| 96 | + return errors.New("don't care about this error since we've cancelled and there is still another retry") |
| 97 | + } |
| 98 | + |
| 99 | + logger := func(_ string, _ ...any) {} |
| 100 | + |
| 101 | + err := RetryWithContext(ctx, fn, 3, 0, logger) |
| 102 | + require.Equal(t, 2, count) |
| 103 | + require.ErrorIs(t, err, context.Canceled) |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("NoErr", func(t *testing.T) { |
| 107 | + count := 0 |
| 108 | + fn := func() error { |
| 109 | + count++ |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + logger := func(_ string, _ ...any) {} |
| 114 | + |
| 115 | + err := RetryWithContext(context.TODO(), fn, 3, 0, logger) |
| 116 | + require.ErrorIs(t, err, nil) |
| 117 | + require.Equal(t, 1, count) |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("InvalidAttempts", func(t *testing.T) { |
| 121 | + count := 0 |
| 122 | + fn := func() error { |
| 123 | + count++ |
| 124 | + return nil |
| 125 | + } |
| 126 | + |
| 127 | + logger := func(_ string, _ ...any) {} |
| 128 | + |
| 129 | + err := RetryWithContext(context.TODO(), fn, 0, 0, logger) |
| 130 | + require.Error(t, err) |
| 131 | + require.Equal(t, 0, count) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("ContextCancellationDeadline", func(t *testing.T) { |
| 135 | + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(2*time.Second)) |
| 136 | + defer cancel() |
| 137 | + |
| 138 | + count := 0 |
| 139 | + fn := func() error { |
| 140 | + count++ |
| 141 | + return errors.New("Always fail") |
| 142 | + } |
| 143 | + |
| 144 | + logger := func(_ string, _ ...any) {} |
| 145 | + |
| 146 | + err := RetryWithContext(ctx, fn, 3, 1*time.Second, logger) |
| 147 | + // fn should be called twice, it will wait one second after the first attempt |
| 148 | + // and tries to wait two seconds after the second attempt |
| 149 | + // but the context will cancel before the third attempt is called |
| 150 | + require.Equal(t, 2, count) |
| 151 | + require.ErrorIs(t, err, context.DeadlineExceeded) |
| 152 | + }) |
62 | 153 |
|
63 | | - count = 0 |
64 | | - logCount = 0 |
65 | | - err := Retry(countFn, 3, 0, loggerFn) |
66 | | - suite.Error(err) |
67 | | - suite.Equal(3, count) |
68 | | - suite.Equal(3, logCount) |
69 | | - |
70 | | - count = 0 |
71 | | - logCount = 0 |
72 | | - err = Retry(countFn, 4, 0, loggerFn) |
73 | | - suite.NoError(err) |
74 | | - suite.Equal(4, count) |
75 | | - suite.Equal(3, logCount) |
76 | 154 | } |
77 | 155 |
|
78 | 156 | func (suite *TestMiscSuite) TestTransformMapKeys() { |
|
0 commit comments