-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
123 lines (111 loc) · 2.12 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package routines
import (
"errors"
"testing"
)
func TestRecorded(t *testing.T) {
idx := uint64(0)
f := func(i uint64) error {
idx = i
return nil
}
g := Recorded(f)
for x := uint64(0); x < 10; x++ {
g()
if idx != x {
t.Fatalf("expected %d, got %d", x, idx)
}
}
}
func TestRetry(t *testing.T) {
theErr := errors.New("the error")
f := func(i uint64) error {
if i >= 5 {
return nil
}
return theErr
}
ch := Retry(Recorded(f))
cnt := 0
for err := range ch {
cnt++
if err != theErr {
t.Fatalf("unexpected error: %v", err)
}
}
if cnt != 5 {
t.Fatalf("expected to failed 5 times, got %d", cnt)
}
}
func TestTriesAtMostFailed(t *testing.T) {
theErr := errors.New("the error")
f := func(i uint64) error {
if i >= 5 {
return nil
}
return theErr
}
ch := TriesAtMost(3, Recorded(f))
cnt := 0
for err := range ch {
cnt++
if err != theErr {
t.Fatalf("unexpected error: %v", err)
}
}
if cnt != 3 {
t.Fatalf("expected to failed 3 times, got %d", cnt)
}
}
func TestTriesAtMostDone(t *testing.T) {
theErr := errors.New("the error")
f := func(i uint64) error {
if i >= 3 {
return nil
}
return theErr
}
ch := TriesAtMost(5, Recorded(f))
cnt := 0
for err := range ch {
cnt++
if err != theErr {
t.Fatalf("unexpected error: %v", err)
}
}
if cnt != 3 {
t.Fatalf("expected to failed 3 times, got %d", cnt)
}
}
func TestTryAtMostFailed(t *testing.T) {
theErr := errors.New("the error")
f := func(i uint64) error {
if i >= 3 {
return nil
}
return theErr
}
err := TryAtMost(3, Recorded(f))
if err == nil {
t.Fatal("expected error, got nothing")
}
if err != theErr {
t.Fatal("unexpected error: ", err)
}
}
func TestTryAtMostDone(t *testing.T) {
theErr := errors.New("the error")
f := func(i uint64) error {
if i >= 3 {
return nil
}
return theErr
}
err := TryAtMost(5, Recorded(f))
if err != nil {
t.Fatal("unexpected error: ", err)
}
}