-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfibonacci_test.go
86 lines (70 loc) · 1.7 KB
/
fibonacci_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
76
77
78
79
80
81
82
83
84
85
86
package backoff
import (
"errors"
"testing"
"time"
)
func TestNextFibonacciBackoff(t *testing.T) {
f := Fibonacci()
f.Interval = 1 * time.Second
f.MaxRetries = 7
expectedRetries := []int{1, 2, 3, 4, 5, 6, 7, 7, 7}
expectedDelays := []time.Duration{0, 1, 1, 2, 3, 5, 8, 8, 8}
for i, v := range expectedDelays {
expectedDelays[i] = v * time.Second
}
for i, expected := range expectedRetries {
f.Next()
assertEquals(t, expected, f.Retries)
assertEquals(t, expectedDelays[i], f.Delay)
}
}
func TestRetryFibonacci(t *testing.T) {
f := Fibonacci()
f.Interval = 1 * time.Millisecond
f.MaxRetries = 5
test := func() error {
return errors.New("an error occurred")
}
f.Retry(test)
if f.Retries != f.MaxRetries {
t.Errorf("f.Retries does not match f.MaxRetries: got %d, expected %d", f.Retries, f.MaxRetries)
}
if f.Retries > f.MaxRetries {
t.Errorf("overflow: retries %d greater than maximum retries %d", f.Retries, f.MaxRetries)
}
test = func() error {
return nil
}
f.Reset()
err := f.Retry(test)
if err != nil {
t.Errorf("failure in retry logic. expected success but got a failure: %+v", err)
}
retries := 0
f.Reset()
test = func() error {
if retries == 0 {
retries++
return errors.New("an error occurred")
}
return nil
}
f.Retry(test)
if err != nil {
t.Errorf("failure in retry logic. expected success but got a failure: %+v", err)
}
}
func TestResetFibonacci(t *testing.T) {
f := Fibonacci()
f.Interval = 1 * time.Second
f.MaxRetries = 5
for i := 0; i < 4; i++ {
f.Next()
}
assertEquals(t, f.Retries, 4)
assertEquals(t, f.Delay, time.Duration(2*time.Second))
f.Reset()
assertEquals(t, f.Retries, 0)
assertEquals(t, f.Delay, time.Duration(0*time.Second))
}