Skip to content

Commit

Permalink
TestTaper is flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
LasTshaMAN authored and chappjc committed Feb 6, 2023
1 parent 08292f9 commit 7e7fc7a
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions dex/wait/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,21 @@ import (
"time"
)

func TestTaper(t *testing.T) {
func TestTaperingQueueExpiration(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

q := NewTaperingTickerQueue(time.Millisecond, time.Millisecond*10)
go q.Run(ctx)

var last time.Time
intervals := make([]time.Duration, 0, 10)
var expirationTime time.Time
var expiration sync.WaitGroup
expiration.Add(1)

wantExpirationTime := time.Now().Add(time.Millisecond * 30)
wantExpirationTime := time.Now().Add(time.Millisecond * 10)
q.Wait(&Waiter{
Expiration: wantExpirationTime,
TryFunc: func() TryDirective {
if last.IsZero() {
last = time.Now()
return TryAgain
}
intervals = append(intervals, time.Since(last))
last = time.Now()
return TryAgain
},
ExpireFunc: func() {
Expand All @@ -41,29 +33,6 @@ func TestTaper(t *testing.T) {

expiration.Wait()

if len(intervals) < 3 {
t.Fatalf("only %d intervals", len(intervals))
}

// check that the first interval was close to the expected value.
var sum time.Duration
for _, i := range intervals[:fullSpeedTicks] {
sum += i
}
avg := sum / fullSpeedTicks

// It'd be nice to check this tighter than *10, but with the race detector,
// there are some unexpectedly long times.
if avg < time.Millisecond || avg > time.Millisecond*10 {
t.Fatalf("first intervals are out of bound: %s", avg)
}

// Make sure it tapered. Can't use the actual last interval, since that
// might be truncated. Use the second from the last.
lastInterval := intervals[len(intervals)-2]
if lastInterval < time.Millisecond*2 {
t.Fatalf("last interval wasn't ~5 ms: %s", lastInterval)
}
if expirationTime.Before(wantExpirationTime) {
t.Fatalf("expired at: %v - sooner than expected: %v", expirationTime, wantExpirationTime)
}
Expand Down

0 comments on commit 7e7fc7a

Please sign in to comment.