-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils_for_test.go
75 lines (59 loc) · 1.78 KB
/
utils_for_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package retry
import (
"testing"
"time"
)
// Override time for test purposes
var testTime time.Time
func init() {
TimeFunc = func() time.Time {
return testTime
}
SleepFunc = func(d time.Duration) {
testTime = testTime.Add(d)
}
}
type testCase struct {
name interface{}
attempts int
minimum int
maximum int
minDuration time.Duration
maxDuration time.Duration
step time.Duration // how far to step time forward after each iteration
}
func tryCase(t *testing.T, strategy Strategy, test testCase) {
// Time
start := time.Now()
testTime = start
timeStep := time.Microsecond
if test.step > 0 {
timeStep = test.step
}
var i int
for i = 0; i < test.attempts; i++ {
if d := TimeFunc().Sub(start); test.maxDuration > 0 && d >= test.maxDuration && strategy.HasNext() {
t.Errorf("[%v] Unexpected HasNext after time: %v max: %v", test.name, d, test.maxDuration)
}
if test.maximum > 0 && i >= test.maximum && strategy.HasNext() {
t.Errorf("[%v] Unexpected HasNext after attempts: %v maximum: %v", test.name, i, test.maximum)
}
next := strategy.Next()
if d := TimeFunc().Sub(start); test.maxDuration > 0 && d >= test.maxDuration && next {
t.Errorf("[%v] Unexpected Next after time: %v max: %v", test.name, d, test.maxDuration)
}
if test.maximum > 0 && i > test.maximum && next {
t.Errorf("[%v] Unexpected Next after attempts: %v maximum: %v", test.name, i, test.maximum)
}
if !next {
break
}
testTime = testTime.Add(timeStep)
}
if i < test.minimum {
t.Errorf("[%v] Did not reach minimum attempts: %v minimum: %v", test.name, i, test.minimum)
}
if d := TimeFunc().Sub(start); test.minDuration > 0 && d < test.minDuration {
t.Errorf("[%v] Test took less time: %v than the minimum: %v", test.name, d, test.minDuration)
}
}