-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancellable_retry_test.go
66 lines (63 loc) · 1.35 KB
/
cancellable_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
package gentle_test
import (
"errors"
"testing"
"time"
"github.com/gigawattio/errorlib"
"github.com/gigawattio/gentle"
)
func TestCancellableRetry(t *testing.T) {
testCases := []struct {
Action gentle.Action
InvokeCancel bool
}{
{
Action: gentle.Action{
Name: "Will never work",
Func: func() error {
return errors.New("told ya")
},
},
InvokeCancel: true,
},
{
Action: gentle.Action{
Name: "Will eventually work",
Func: func() error {
time.Sleep(1 * time.Millisecond)
return nil
},
},
InvokeCancel: true,
},
{
Action: gentle.Action{
Name: "Will eventually work",
Func: func() error {
return nil
},
},
InvokeCancel: false,
},
}
for i, testCase := range testCases {
doneChan := make(chan struct{}, 1)
cancelFunc := gentle.CancellableRetry(gentle.CancellableRetryConfig{
Actions: []gentle.Action{
testCase.Action,
},
DoneChan: doneChan,
Debug: true,
})
if testCase.InvokeCancel {
if err := cancelFunc(); err != nil {
t.Fatalf("[i=%v/testCase=%+v] %s", i, testCase, err)
}
} else {
<-doneChan
}
if expected, actual := errorlib.NotRunningError, cancelFunc(); actual != expected {
t.Fatalf("[i=%v/testCase=%+v] Expected late invocation of cancelFunc() to produce err=%v but actual=%v", i, testCase, expected, actual)
}
}
}