-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlife_test.go
236 lines (201 loc) · 5.33 KB
/
life_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package life
import (
"errors"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"
)
func TestLife(t *testing.T) {
t.Run("simple life", func(t *testing.T) {
v := NewSingleLife()
started := waitOnChan(v.started, 5*time.Millisecond)
if started == nil {
t.Fatalf("SingleLife started when it wasn't supposed to")
}
terminated := waitOnChan(v.terminated, 5*time.Millisecond)
if terminated == nil {
t.Fatalf("SingleLife terminated when it wasn't supposed to")
}
v.Start()
started = waitOnChan(v.started, 5*time.Millisecond)
ok(t, started)
terminated = waitOnChan(v.terminated, 5*time.Millisecond)
if terminated == nil {
t.Fatalf("SingleLife terminated when it wasn't supposed to")
}
// Set up a maximum wait time before failing
timer := time.NewTimer(50 * time.Millisecond)
defer timer.Stop()
errChan := make(chan error, 1)
go func() {
errChan <- v.Close()
}()
select {
case <-timer.C:
t.Fatalf("Timed out waiting for close to finish")
case err := <-errChan:
if err != nil {
t.Fatalf("Error received from Close call: %s", err)
}
}
terminated = waitOnChan(v.terminated, 5*time.Millisecond)
ok(t, terminated)
})
t.Run("close multiple times", func(t *testing.T) {
l := NewSingleLife()
err := l.Close()
ok(t, err)
err = l.Close() // This shouldn't panic
ok(t, err)
})
}
func TestLife_multiRoutine(t *testing.T) {
p := NewLifeWithChildren()
started := waitOnChan(p.started, 5*time.Millisecond)
if started == nil {
t.Fatalf("LifeWithChildren started when it wasn't supposed to")
}
terminated := waitOnChan(p.terminated, 5*time.Millisecond)
if terminated == nil {
t.Fatalf("LifeWithChildren terminated when it wasn't supposed to")
}
if len(p.childrenStarted) > 0 {
t.Fatalf("Subroutines have started when they weren't supposed to")
}
if len(p.childrenTerminated) > 0 {
t.Fatalf("Subroutines have started when they weren't supposed to")
}
// Start LifeWithChildren and make sure that both the main goroutine and its subroutines are running
p.Start()
started = waitOnChan(p.started, 5*time.Millisecond)
ok(t, started)
terminated = waitOnChan(p.terminated, 5*time.Millisecond)
if terminated == nil {
t.Fatalf("LifeWithChildren terminated when it wasn't supposed to")
}
for i := 0; i < p.numChildren; i++ {
err := waitOnChan(p.childrenStarted, 5*time.Millisecond)
ok(t, err)
}
if len(p.childrenStarted) > 0 {
t.Fatalf("Too many subroutines started")
}
terminated = waitOnChan(p.childrenTerminated, 5*time.Millisecond)
if terminated == nil {
t.Fatalf("Subroutines terminated when they weren't supposed to")
}
// Set up a maximum wait time before failing
timer := time.NewTimer(50 * time.Millisecond)
defer timer.Stop()
errChan := make(chan error, 1)
go func() {
errChan <- p.Close()
}()
select {
case <-timer.C:
t.Fatalf("Timed out waiting for close to finish")
case err := <-errChan:
if err != nil {
t.Fatalf("Error received from Close call: %s", err)
}
}
terminated = waitOnChan(p.terminated, 5*time.Millisecond)
ok(t, terminated)
// Check on the subroutines
for i := 0; i < p.numChildren; i++ {
err := waitOnChan(p.childrenTerminated, 5*time.Millisecond)
ok(t, err)
}
if len(p.childrenTerminated) > 0 {
t.Fatalf("Too many subroutines terminated")
}
if len(p.childrenStarted) > 0 {
t.Fatalf("Subroutine has started when it shouldn't have")
}
}
type SingleLife struct {
*Life
started chan struct{}
terminated chan struct{}
}
func NewSingleLife() SingleLife {
l := SingleLife{
Life: NewLife(),
started: make(chan struct{}, 0),
terminated: make(chan struct{}, 0),
}
l.SetRun(l.run)
return l
}
func (v SingleLife) run() {
close(v.started)
select {
case <-v.Life.Done:
// Sleep to make sure that life waits for this to finish rather than returning immediately
time.Sleep(5 * time.Millisecond)
close(v.terminated)
}
}
type LifeWithChildren struct {
*Life
started chan struct{}
terminated chan struct{}
numChildren int
childrenStarted chan struct{}
childrenTerminated chan struct{}
}
func NewLifeWithChildren() LifeWithChildren {
numSubRoutines := 5
p := LifeWithChildren{
Life: NewLife(),
started: make(chan struct{}, 0),
terminated: make(chan struct{}, 0),
numChildren: numSubRoutines,
childrenStarted: make(chan struct{}, numSubRoutines),
childrenTerminated: make(chan struct{}, numSubRoutines),
}
p.SetRun(p.run)
return p
}
func (p LifeWithChildren) run() {
defer close(p.terminated)
close(p.started)
for i := 0; i < p.numChildren; i++ {
p.Life.WGAdd(1)
go p.subRoutine()
}
select {
case <-p.Life.Done:
return
}
}
func (p LifeWithChildren) subRoutine() {
defer p.Life.WGDone()
p.childrenStarted <- struct{}{}
select {
case <-p.Life.Done:
// Same as above: make sure that life waits for this to finish
time.Sleep(5 * time.Millisecond)
p.childrenTerminated <- struct{}{}
}
}
func waitOnChan(c chan struct{}, wait time.Duration) (err error) {
timer := time.NewTimer(wait)
defer timer.Stop()
select {
case <-timer.C:
return errors.New("timed out")
case <-c:
return nil
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}