-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisor_test.go
161 lines (129 loc) · 3.09 KB
/
supervisor_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
package supervisor
import (
"context"
"sync"
"testing"
"time"
"go.uber.org/goleak"
)
type mockSupervisable struct {
nCalls int
nPanics int
shouldPanic bool
ctxStopped bool
isRunning bool
}
func generateSupervisable(ms *mockSupervisable) Supervisable {
ms.nCalls = 0
ms.nPanics = 0
return func(ctx context.Context, done chan struct{}) {
defer func() {
if recover() != nil {
// test == nothing to do
}
close(done)
ms.isRunning = false
}()
ms.isRunning = true
ms.nCalls++
for {
select {
case <-ctx.Done():
ms.ctxStopped = true
return
case <-time.After(50 * time.Millisecond):
if ms.shouldPanic {
ms.nPanics++
panic("testing")
}
}
}
}
}
//
// These tests monitor the basic functionality, but there's also a little
// bit of magic behind the scenes in that we're also testing for leaking
// goroutines.
//
func Test_SupervisorMustTerminateWhenStopped(t *testing.T) {
defer goleak.VerifyNone(t)
ms := &mockSupervisable{}
s := NewSimpleSupervisor(context.Background(), generateSupervisable(ms))
isUnblocked := false
go func() {
s.Run()
isUnblocked = true
}()
<-time.After(time.Millisecond * 100)
s.Stop()
<-time.After(time.Millisecond * 100)
if !isUnblocked {
t.Error("call to Stop should prevent Run from blocking")
}
if !ms.ctxStopped {
t.Error("call to Stop should result in context cancellation")
}
if ms.isRunning {
t.Error("call to Stop should ensure goroutine has terminated")
}
if !(ms.nCalls >= 1) {
t.Error("supervisable not called")
}
if !s.HasStopped() {
t.Error("supervisor indicates it's still running")
}
}
func Test_SupervisorMustRestartWorkerFollowingPanic(t *testing.T) {
defer goleak.VerifyNone(t)
ms := &mockSupervisable{
shouldPanic: true,
}
s := NewSimpleSupervisor(context.Background(), generateSupervisable(ms))
s.Run()
<-time.After(time.Millisecond * 100)
s.Stop()
<-time.After(time.Millisecond * 100)
if !(ms.nCalls >= 1) {
t.Error("supervisable not called")
}
// ms.nCalls = ms.nPanics + initial call
if !((ms.nCalls - ms.nPanics) < 2) {
t.Error("supervisable did not restart after each panic", ms.nCalls, ms.nPanics)
}
}
func Test_SupervisorMustNotifyCallerWithWaitGroup(t *testing.T) {
defer goleak.VerifyNone(t)
ms := &mockSupervisable{}
wg := &sync.WaitGroup{}
s := NewSimpleSupervisor(context.Background(), generateSupervisable(ms))
s.WithWaitGroup(wg)
s.Run()
wgComplete := false
go func() {
wg.Wait()
wgComplete = true
}()
<-time.After(time.Millisecond * 100)
s.Stop()
<-time.After(time.Millisecond * 100)
if !(ms.nCalls >= 1) {
t.Error("supervisable not called")
}
if !wgComplete {
t.Error("waitgroup never completed")
}
}
func Test_SupervisorShouldRestartWhenRequested(t *testing.T) {
defer goleak.VerifyNone(t)
ms := &mockSupervisable{}
s := NewSimpleSupervisor(context.Background(), generateSupervisable(ms))
s.Run()
<-time.After(time.Millisecond * 100)
s.Restart()
<-time.After(time.Millisecond * 100)
s.Stop()
<-time.After(time.Millisecond * 100)
if !(ms.nCalls == 2) {
t.Error("supervisable not restarted", ms.nCalls)
}
}