-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackoff_test.go
More file actions
47 lines (42 loc) · 839 Bytes
/
backoff_test.go
File metadata and controls
47 lines (42 loc) · 839 Bytes
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
package backoff
import (
"errors"
"testing"
"time"
)
func TestBackoff(t *testing.T) {
okAfter := 5
calls := 0
b := NewDoubleTimeBackoff(100*time.Millisecond, 1*time.Second, 10)
err := b.Do(func() error {
calls++
if calls > okAfter {
return nil
}
return errors.New("not ok")
})
if err != nil {
t.Fatalf("Error should be nil but is: %v", err)
}
if calls > 10 {
t.Fatalf("Calls should be < 10 but is: %v", calls)
}
}
func TestBackoffMaxCalls(t *testing.T) {
okAfter := 5
calls := 0
b := NewDoubleTimeBackoff(100*time.Millisecond, 1*time.Second, 2)
err := b.Do(func() error {
calls++
if calls > okAfter {
return nil
}
return errors.New("not ok")
})
if err == nil {
t.Fatalf("Error should be not nil but is: %v", err)
}
if calls != 2 {
t.Fatalf("Calls should be 2 but is: %v", calls)
}
}