/
retoil_test.go
140 lines (92 loc) · 3.07 KB
/
retoil_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package retoil
import (
"testing"
"github.com/reiver/go-toil/toiltest"
"sync"
)
func TestNew(t *testing.T) {
toiler := toiltest.NewRecorder()
retoiler := New(toiler, NeverStrategy())
if nil == retoiler {
t.Errorf("After trying to create a new retoiler, received nil: %v", retoiler)
}
}
func TestPanickedNotice(t *testing.T) {
toiler := toiltest.NewRecorder()
numPanicked := 0
var waitGroup sync.WaitGroup
var caughtPanicValue interface{}
waitGroup.Add(1)
toiler.PanickedNoticeFunc(func(panicValue interface{}){
numPanicked++
caughtPanicValue = panicValue
waitGroup.Done()
})
retoiler := New(toiler, DelayedLimitedStrategy(2, 0))
if expected, actual := 0, numPanicked; expected != actual {
t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
}
go retoiler.Toil()
panicValue := "Panic panic, panic!!!!"
toiler.Panic(panicValue)
waitGroup.Wait()
if expected, actual := 1, numPanicked; expected != actual {
t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
}
if expected, actual := panicValue, caughtPanicValue; expected != actual {
t.Errorf("Expected caught panic value to be [%v], but actually was [%v].", expected, actual)
}
}
func TestReturnedNotice(t *testing.T) {
toiler := toiltest.NewRecorder()
numReturned := 0
var waitGroup sync.WaitGroup
waitGroup.Add(1)
toiler.ReturnedNoticeFunc(func(){
numReturned++
waitGroup.Done()
})
retoiler := New(toiler, DelayedLimitedStrategy(2, 0))
if expected, actual := 0, numReturned; expected != actual {
t.Errorf("Expected number of times returned to be %d, but actually was %d.", expected, actual)
}
go retoiler.Toil()
toiler.Terminate()
waitGroup.Wait()
if expected, actual := 1, numReturned; expected != actual {
t.Errorf("Expected number of times returned to be %d, but actually was %d.", expected, actual)
}
}
func TestToil(t *testing.T) {
toiler := toiltest.NewRecorder()
numToiled := 0
var waitGroup sync.WaitGroup
waitGroup.Add(1)
toiler.ToilFunc(func(){
numToiled++
waitGroup.Done()
})
retoiler := New(toiler, DelayedLimitedStrategy(2, 0)) // NOTE that the 2 means we can only panic() this toiler 2 times!
if expected, actual := 0, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
go retoiler.Toil()
waitGroup.Wait()
if expected, actual := 1, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
waitGroup.Add(1)
panicValue1 := "Panic panic, panic!!!! [1]"
toiler.Panic(panicValue1)
waitGroup.Wait()
if expected, actual := 2, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
waitGroup.Add(1)
panicValue2 := "Panic panic, panic!!!! [2]"
toiler.Panic(panicValue2)
waitGroup.Wait()
if expected, actual := 3, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
}