Skip to content

Commit

Permalink
Fix flaky test by increasing tolerance to process pauses (#5400)
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr authored Oct 9, 2023
1 parent 2ed7500 commit 9a53c48
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions component/common/loki/wal/timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ import (
)

const (
delta = time.Millisecond * 20
maxOvershoot = 200 * time.Millisecond
)

func TestBackoffTimer(t *testing.T) {
var min = time.Millisecond * 300
var max = time.Second
timer := newBackoffTimer(min, max)

now := time.Now()
start := time.Now()
<-timer.C
require.WithinDuration(t, now.Add(min), time.Now(), delta, "expected backing off timer to fire in the minimum")
verifyElapsedTime(t, min, time.Since(start))

// backoff, and expect it will take twice the time
now = time.Now()
start = time.Now()
timer.backoff()
<-timer.C
require.WithinDuration(t, now.Add(min*2), time.Now(), delta, "expected backing off timer to fire in the twice the minimum")
verifyElapsedTime(t, 2*min, time.Since(start))

// backoff capped, backoff will actually be 1200ms, but capped at 1000
now = time.Now()
start = time.Now()
timer.backoff()
<-timer.C
require.WithinDuration(t, now.Add(max), time.Now(), delta, "expected backing off timer to fire in the max")
verifyElapsedTime(t, max, time.Since(start))
}

func verifyElapsedTime(t *testing.T, expected time.Duration, elapsed time.Duration) {
require.GreaterOrEqual(t, elapsed, expected, "elapsed time should be greater or equal to the expected value")
require.Less(t, elapsed, expected+maxOvershoot, "elapsed time should be less than the expected value plus the max overshoot")
}

0 comments on commit 9a53c48

Please sign in to comment.