Skip to content

Commit 9e387e2

Browse files
committed
Making test less flacky
Signed-off-by: alanprot <[email protected]>
1 parent c3e268a commit 9e387e2

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

pkg/util/time_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,27 @@ func TestSlottedTicker(t *testing.T) {
219219
slotNumber: 4,
220220
},
221221
}
222-
for name, tc := range testCases {
222+
for name, c := range testCases {
223+
tc := c
223224
t.Run(name, func(t *testing.T) {
224225
infoFunc := func() (int, int) {
225226
return tc.slotNumber, tc.totalSlots
226227
}
227228
ticker := NewSlottedTicker(infoFunc, tc.duration)
228-
for i := 0; i < 15; i++ {
229-
tTime := <-ticker.C
230-
slotSize := tc.duration.Milliseconds() / int64(tc.totalSlots)
231-
slotShiftInMs := tTime.UnixMilli() % tc.duration.Milliseconds()
232-
slot := slotShiftInMs / slotSize
233-
require.GreaterOrEqual(t, slot, int64(tc.slotNumber))
234-
require.LessOrEqual(t, slot, int64(tc.slotNumber+1))
235-
}
229+
tTime := <-ticker.C
230+
slotSize := tc.duration.Milliseconds() / int64(tc.totalSlots)
231+
slotShiftInMs := tTime.UnixMilli() % tc.duration.Milliseconds()
232+
slot := slotShiftInMs / slotSize
233+
successCount := 0
234+
test.Poll(t, 2*time.Second, true, func() interface{} {
235+
if slot == int64(tc.slotNumber) {
236+
successCount++
237+
} else {
238+
successCount--
239+
}
240+
241+
return successCount == 50
242+
})
236243
ticker.Stop()
237244
})
238245
}

0 commit comments

Comments
 (0)