Skip to content

Commit bfeaa34

Browse files
committed
TUN-7624: Fix flaky TestBackoffGracePeriod test in cloudflared
1 parent 9584adc commit bfeaa34

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

retry/backoffhandler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ func (b *BackoffHandler) Backoff(ctx context.Context) bool {
9191

9292
// Sets a grace period within which the the backoff timer is maintained. After the grace
9393
// period expires, the number of retries & backoff duration is reset.
94-
func (b *BackoffHandler) SetGracePeriod() {
94+
func (b *BackoffHandler) SetGracePeriod() time.Duration {
9595
maxTimeToWait := b.GetBaseTime() * 2 << (b.retries + 1)
9696
timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds()))
9797
b.resetDeadline = Clock.Now().Add(timeToWait)
98+
99+
return timeToWait
98100
}
99101

100102
func (b BackoffHandler) GetBaseTime() time.Duration {

retry/backoffhandler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ func TestBackoffGracePeriod(t *testing.T) {
5757
t.Fatalf("backoff failed immediately")
5858
}
5959
// the next call to Backoff would fail unless it's after the grace period
60-
backoff.SetGracePeriod()
61-
// advance time to after the grace period (~4 seconds) and see what happens
62-
currentTime = currentTime.Add(time.Second * 5)
60+
gracePeriod := backoff.SetGracePeriod()
61+
// advance time to after the grace period, which at most will be 8 seconds, but we will advance +1 second.
62+
currentTime = currentTime.Add(gracePeriod + time.Second)
6363
if !backoff.Backoff(ctx) {
6464
t.Fatalf("backoff failed after the grace period expired")
6565
}

0 commit comments

Comments
 (0)