-
Notifications
You must be signed in to change notification settings - Fork 443
/
retry_test.go
46 lines (41 loc) · 1.18 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package retry
import (
"testing"
"time"
)
// delta defines the time band a test run should complete in.
var delta = 25 * time.Millisecond
func TestRetryer(t *testing.T) {
tests := []struct {
desc string
r Retryer
}{
{"counter", &Counter{Count: 3, Wait: 100 * time.Millisecond}},
{"timer", &Timer{Timeout: 200 * time.Millisecond, Wait: 100 * time.Millisecond}},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var iters, fails int
fail := func() { fails++ }
start := time.Now()
for tt.r.NextOr(fail) {
iters++
}
dur := time.Since(start)
if got, want := iters, 3; got != want {
t.Fatalf("got %d retries want %d", got, want)
}
if got, want := fails, 1; got != want {
t.Fatalf("got %d FailNow calls want %d", got, want)
}
// since the first iteration happens immediately
// the retryer waits only twice for three iterations.
// order of events: (true, (wait) true, (wait) true, false)
if got, want := dur, 200*time.Millisecond; got < (want-delta) || got > (want+delta) {
t.Fatalf("loop took %v want %v (+/- %v)", got, want, delta)
}
})
}
}