forked from bsm/redislock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_strategy_test.go
62 lines (56 loc) · 1.32 KB
/
retry_strategy_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
package redislock_test
import (
"testing"
"time"
. "github.com/bsm/redislock"
)
func TestNoRetry(t *testing.T) {
retry := NoRetry()
for i, exp := range []time.Duration{0, 0, 0} {
if got := retry.NextBackoff(); exp != got {
t.Fatalf("expected %d to be %v, got %v", i, exp, got)
}
}
}
func TestLinearBackoff(t *testing.T) {
retry := LinearBackoff(time.Second)
for i, exp := range []time.Duration{
time.Second,
time.Second,
time.Second,
} {
if got := retry.NextBackoff(); exp != got {
t.Fatalf("expected %d to be %v, got %v", i, exp, got)
}
}
}
func TestExponentialBackoff(t *testing.T) {
retry := ExponentialBackoff(10*time.Millisecond, 300*time.Millisecond)
for i, exp := range []time.Duration{
10 * time.Millisecond,
10 * time.Millisecond,
16 * time.Millisecond,
32 * time.Millisecond,
64 * time.Millisecond,
128 * time.Millisecond,
256 * time.Millisecond,
300 * time.Millisecond,
300 * time.Millisecond,
} {
if got := retry.NextBackoff(); exp != got {
t.Fatalf("expected %d to be %v, got %v", i, exp, got)
}
}
}
func TestLimitRetry(t *testing.T) {
retry := LimitRetry(LinearBackoff(time.Second), 2)
for i, exp := range []time.Duration{
time.Second,
time.Second,
0,
} {
if got := retry.NextBackoff(); exp != got {
t.Fatalf("expected %d to be %v, got %v", i, exp, got)
}
}
}