Skip to content

Commit 7b3e2b0

Browse files
committed
test: use fast timeout to avoid flaky tests
1 parent 3dbbe20 commit 7b3e2b0

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

internal/deliverymq/retry_test.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type RetryDeliveryMQSuite struct {
2222
ctx context.Context
2323
mqConfig *mqs.QueueConfig
2424
retryMaxCount int
25+
retryBackoff backoff.Backoff
2526
publisher deliverymq.Publisher
2627
eventGetter deliverymq.EventGetter
2728
logPublisher deliverymq.LogPublisher
@@ -52,6 +53,11 @@ func (s *RetryDeliveryMQSuite) SetupTest(t *testing.T) {
5253
go retryScheduler.Monitor(s.ctx)
5354

5455
// Setup message handler
56+
// Use provided backoff or default to 1 second
57+
retryBackoff := s.retryBackoff
58+
if retryBackoff == nil {
59+
retryBackoff = &backoff.ConstantBackoff{Interval: 1 * time.Second}
60+
}
5561
handler := deliverymq.NewMessageHandler(
5662
testutil.CreateTestLogger(t),
5763
s.logPublisher,
@@ -60,7 +66,7 @@ func (s *RetryDeliveryMQSuite) SetupTest(t *testing.T) {
6066
s.publisher,
6167
testutil.NewMockEventTracer(nil),
6268
retryScheduler,
63-
&backoff.ConstantBackoff{Interval: 1 * time.Second},
69+
retryBackoff,
6470
s.retryMaxCount,
6571
s.alertMonitor,
6672
idempotence.New(testutil.CreateTestRedisClient(t), idempotence.WithSuccessfulTTL(24*time.Hour)),
@@ -132,6 +138,7 @@ func TestDeliveryMQRetry_EligibleForRetryFalse(t *testing.T) {
132138
destGetter: &mockDestinationGetter{dest: &destination},
133139
alertMonitor: newMockAlertMonitor(),
134140
retryMaxCount: 10,
141+
retryBackoff: &backoff.ConstantBackoff{Interval: 50 * time.Millisecond},
135142
}
136143
suite.SetupTest(t)
137144
defer suite.TeardownTest(t)
@@ -193,6 +200,7 @@ func TestDeliveryMQRetry_EligibleForRetryTrue(t *testing.T) {
193200
destGetter: &mockDestinationGetter{dest: &destination},
194201
alertMonitor: newMockAlertMonitor(),
195202
retryMaxCount: 10,
203+
retryBackoff: &backoff.ConstantBackoff{Interval: 50 * time.Millisecond},
196204
}
197205
suite.SetupTest(t)
198206
defer suite.TeardownTest(t)
@@ -204,24 +212,10 @@ func TestDeliveryMQRetry_EligibleForRetryTrue(t *testing.T) {
204212
}
205213
require.NoError(t, suite.deliveryMQ.Publish(ctx, deliveryEvent))
206214

207-
// Wait for all attempts to complete
208-
done := make(chan struct{})
209-
go func() {
210-
for {
211-
if publisher.Current() >= 3 {
212-
close(done)
213-
return
214-
}
215-
time.Sleep(100 * time.Millisecond)
216-
}
217-
}()
218-
219-
select {
220-
case <-ctx.Done():
221-
t.Fatal("test timed out waiting for attempts to complete")
222-
case <-done:
223-
// Continue with assertions
224-
}
215+
// Wait for all attempts to complete (3 attempts with 50ms backoff = ~150ms)
216+
require.Eventually(t, func() bool {
217+
return publisher.Current() >= 3
218+
}, 3*time.Second, 10*time.Millisecond, "should complete 3 attempts (2 failures + 1 success)")
225219

226220
assert.Equal(t, 3, publisher.Current(), "should retry until success (2 failures + 1 success)")
227221
}
@@ -262,6 +256,7 @@ func TestDeliveryMQRetry_SystemError(t *testing.T) {
262256
destGetter: destGetter,
263257
alertMonitor: newMockAlertMonitor(),
264258
retryMaxCount: 10,
259+
retryBackoff: &backoff.ConstantBackoff{Interval: 50 * time.Millisecond},
265260
}
266261
suite.SetupTest(t)
267262
defer suite.TeardownTest(t)
@@ -330,6 +325,7 @@ func TestDeliveryMQRetry_RetryMaxCount(t *testing.T) {
330325
destGetter: &mockDestinationGetter{dest: &destination},
331326
alertMonitor: newMockAlertMonitor(),
332327
retryMaxCount: 2, // 1 initial + 2 retries = 3 total attempts
328+
retryBackoff: &backoff.ConstantBackoff{Interval: 50 * time.Millisecond}, // Fast backoff for testing
333329
}
334330
suite.SetupTest(t)
335331
defer suite.TeardownTest(t)
@@ -342,10 +338,10 @@ func TestDeliveryMQRetry_RetryMaxCount(t *testing.T) {
342338
require.NoError(t, suite.deliveryMQ.Publish(ctx, deliveryEvent))
343339

344340
// Poll until we get 3 attempts or timeout
345-
// Need to wait for: initial attempt + 1s backoff + retry + 1s backoff + retry = ~2.5s minimum
341+
// With 50ms backoff: initial + 50ms + retry + 50ms + retry = ~150ms minimum
346342
require.Eventually(t, func() bool {
347343
return publisher.Current() >= 3
348-
}, 10*time.Second, 100*time.Millisecond, "should complete 3 attempts (1 initial + 2 retries)")
344+
}, 3*time.Second, 10*time.Millisecond, "should complete 3 attempts (1 initial + 2 retries)")
349345

350346
assert.Equal(t, 3, publisher.Current(), "should stop after max retries (1 initial + 2 retries = 3 total attempts)")
351347
}

0 commit comments

Comments
 (0)