/
group_daemon_test.go
164 lines (114 loc) · 4.02 KB
/
group_daemon_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
package toil
import (
"testing"
"github.com/reiver/go-toil/toiltest"
"math/rand"
"sync"
"time"
)
func TestNewGroupDaemon(t *testing.T) {
panicCh := make(chan interface{})
daemon := newGroupDaemon(panicCh)
if nil == daemon {
t.Errorf("After creating a new daemon, expected it to not be nil, but it was: %v", daemon)
return
}
if nil == daemon.pingCh {
t.Errorf("After creating a new daemon, expected daemon.pingCh to not be nil, but it was: %v", daemon.pingCh)
return
}
if nil == daemon.toilCh {
t.Errorf("After creating a new daemon, expected daemon.toilCh to not be nil, but it was: %v", daemon.toilCh)
return
}
if nil == daemon.registerCh {
t.Errorf("After creating a new daemon, expected daemon.registerCh to not be nil, but it was: %v", daemon.registerCh)
return
}
}
func TestPingCh(t *testing.T) {
panicCh := make(chan interface{})
daemon := newGroupDaemon(panicCh)
const NUM_PING_TESTS = 20
doneCh := make(chan struct{})
for i:=0; i<NUM_PING_TESTS; i++ {
daemon.PingCh() <- struct{doneCh chan struct{}}{
doneCh:doneCh,
}
<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.
}
}
func TestLengthCh(t *testing.T) {
panicCh := make(chan interface{})
daemon := newGroupDaemon(panicCh)
lengthReturnCh := make(chan int)
daemon.LengthCh() <- struct{returnCh chan int}{
returnCh:lengthReturnCh,
}
length := <-lengthReturnCh
if expected, actual := 0, length; expected != actual {
t.Errorf("After creating a new daemon, expected the number of registered toilers to be %d, but actually was %d.", expected, actual)
return
}
}
func TestRegisterCh(t *testing.T) {
toiler := toiltest.NewRecorder()
panicCh := make(chan interface{})
daemon := newGroupDaemon(panicCh)
const NUM_REGISTER_TESTS = 20
doneCh := make(chan struct{})
for testNumber:=0; testNumber<NUM_REGISTER_TESTS; testNumber++ {
daemon.RegisterCh() <- struct{doneCh chan struct{}; toiler Toiler}{
doneCh:doneCh,
toiler:toiler,
}
<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.
lengthReturnCh := make(chan int)
daemon.LengthCh() <- struct{returnCh chan int}{
returnCh:lengthReturnCh,
}
length := <-lengthReturnCh
if expected, actual := 1+testNumber, length; expected != actual {
t.Errorf("For test #%d, after registering toilers with new daemon, expected the number of registered toilers to be %d, but actually was %d.", testNumber, expected, actual)
continue
}
}
}
func TestToilCh(t *testing.T) {
// Initialize.
randomness := rand.New( rand.NewSource( time.Now().UTC().UnixNano() ) )
// Do tests.
const NUM_TOIL_TESTS = 20
doneCh := make(chan struct{})
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()
})
panicCh := make(chan interface{})
daemon := newGroupDaemon(panicCh)
for i:=0; i<numberOfTimesToToil; i++ {
daemon.RegisterCh() <- struct{doneCh chan struct{}; toiler Toiler}{
doneCh:doneCh,
toiler:toiler,
}
<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.
}
daemon.ToilCh() <- struct{doneCh chan struct{}}{
doneCh:doneCh,
}
<-doneCh // THE TEST WE ARE DOING IS MAKING SURE THIS DOES NOT RESULT IN A DEADLOCK.
// THUS WE ARE NOT CALLING ANYTHING LIKE t.Errorf() IN THIS CASE.
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
}
}
}