-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry_test.go
49 lines (43 loc) · 1019 Bytes
/
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
47
48
49
package retry
import (
"testing"
"time"
)
func TestSplay(t *testing.T) {
const s = 5
backoff := Intervals(0)
for i := 0; i < 1e5; i++ {
b := backoff(0)
if b < -s || b > +s {
t.Errorf("Splay should be in the range [0, ±%d), got %d", s, b)
}
}
}
func TestExponentialBackoff(t *testing.T) {
var next, prev time.Duration
var bk = Exponential(time.Second)
for i := 0; i < 100; i++ {
next = bk(i)
t.Logf("exponential backoff(%d) = %s", i, next)
if next == prev {
break
}
if next <= prev {
t.Errorf("bk(%d)[%s] <= bk(%d)[%s], exponential backoff should always increase",
i, next, i-1, prev)
}
prev = next
}
}
func TestIntervalBackoff(t *testing.T) {
x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
const jitter = 50
backoff := Seconds(x...)
for i, v := range x {
t.Logf("fixed backoff(%d) = %s", i, backoff(i))
if backoff(i) != time.Duration(v)*time.Second {
t.Errorf("fixed backoff(%d) = %s, should be > %s", i, backoff(i),
time.Duration(v)*time.Second)
}
}
}