/
group_test.go
200 lines (132 loc) · 4.51 KB
/
group_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package toil
import (
"testing"
"github.com/reiver/go-toil/toiltest"
"fmt"
"math/rand"
"sync"
"time"
)
func TestNewGroup(t *testing.T) {
group := NewGroup()
if nil == group {
t.Errorf("After creating a new group, expected it to not be nil, but it was: %v", group)
return
}
}
func TestLen(t *testing.T) {
group := NewGroup()
{
testNumber := 0
length := group.Len()
if expected, actual := 0, length; expected != actual {
t.Errorf("For test #%d, after creating new groupp, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
return
}
}
const limit = 30
for testNumber := 1; testNumber <= limit; testNumber++ {
group.Register( ToilerFunc(func(){}) )
{
length := group.Len()
if expected, actual := testNumber, length; expected != actual {
t.Errorf("For test #%d, after creating new groupp, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
return
}
}
}
}
func TestRegister(t *testing.T) {
toiler := toiltest.NewRecorder()
group := NewGroup()
const NUM_REGISTER_TESTS = 20
for testNumber:=0; testNumber<NUM_REGISTER_TESTS; testNumber++ {
group.Register(toiler)
length := group.Len()
if expected, actual := 1+testNumber, length; expected != actual {
t.Errorf("For test #%d, after registering toilers with new group, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
continue
}
}
}
func TestToil(t *testing.T) {
// Initialize.
randomness := rand.New( rand.NewSource( time.Now().UTC().UnixNano() ) )
// Do tests.
const NUM_TOIL_TESTS = 20
for testNumber:=0; testNumber<NUM_TOIL_TESTS; testNumber++ {
numberOfTimesToToil := randomness.Intn(44)
var waitGroup sync.WaitGroup
waitGroup.Add(numberOfTimesToToil)
toiler := toiltest.NewRecorder()
toiler.ToilFunc(func(){
waitGroup.Done()
})
group := NewGroup()
for i:=0; i<numberOfTimesToToil; i++ {
group.Register(toiler)
}
go group.Toil()
waitGroup.Wait() // Make sure all the calls on the Toil() method are done before continuing.
if expected, actual := numberOfTimesToToil, toiler.NumToiling(); expected != actual {
t.Errorf("For test #%d with, expected the number of toiling toilers to be %d, but actually was %d.", testNumber, expected, actual)
continue
}
}
}
func TestToilPanicked(t *testing.T) {
// Initialize.
randomness := rand.New( rand.NewSource( time.Now().UTC().UnixNano() ) )
// Do test.
toiler := toiltest.NewRecorder()
numToiled := 0
var toiledWaitGroup sync.WaitGroup
toiledWaitGroup.Add(1)
toiler.ToilFunc(func(){
numToiled++
toiledWaitGroup.Done()
})
numPanicked := 0
var panickedWaitGroup sync.WaitGroup
toiler.PanickedNoticeFunc(func(panicValue interface{}){
numPanicked++
panickedWaitGroup.Done()
})
group := NewGroup()
group.Register(toiler)
var expectedPanicValue interface{} = nil
go func() {
defer func() {
if expected, actual := 1, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
if panicValue := recover(); nil != panicValue {
if expected, actual := panicValue, expectedPanicValue; expected != actual {
t.Errorf("Expected caught panic value to be [%v], but actually was [%v].", expected, actual)
}
} else {
t.Errorf("This should NOT get to this part of the code either!!")
}
}()
group.Toil()
}()
toiledWaitGroup.Wait() // Make sure all the calls on the Toil() method are done before continuing.
if expected, actual := 1, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
if expected, actual := 0, numPanicked; expected != actual {
t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
}
panickedWaitGroup.Add(1)
panicValue := fmt.Sprintf("Panic Value with some random stuff: %d", randomness.Intn(999999999))
expectedPanicValue = panicValue // <----------------- NOTE we set the expectedPanicValue
toiler.Panic(panicValue)
panickedWaitGroup.Wait()
// V---------- NOTE that sayed as 1.
if expected, actual := 1, numToiled; expected != actual {
t.Errorf("Expected number of times toiled to be %d, but actually was %d.", expected, actual)
}
if expected, actual := 1, numPanicked; expected != actual {
t.Errorf("Expected number of times panicked to be %d, but actually was %d.", expected, actual)
}
}