-
Notifications
You must be signed in to change notification settings - Fork 4
/
supervisor_test.go
229 lines (209 loc) · 5.92 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
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
package supervisor
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/go-logr/logr"
testr "github.com/go-logr/logr/testing"
"go.einride.tech/clock"
"go.einride.tech/clock/systemclock"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestSupervisor_New(t *testing.T) {
// given
var bs bytes.Buffer
cfg := Config{
RestartInterval: 100 * time.Millisecond,
Clock: systemclock.New(),
Logger: testr.NewTestLogger(t),
}
supervisor := New(&cfg)
// when the supervisor is started
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
assert.NilError(t, supervisor.Run(ctx))
cancel()
// then nothing is logged since no services are added
assert.Equal(t, "", bs.String())
}
func TestSupervisor_SingleService(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
cfg.Services = append(cfg.Services, NewService("service1", func(ctx context.Context) error {
<-ctx.Done()
return nil
}))
_, done := newTestFixture(t, cfg)
defer done()
}
func TestSupervisor_Logger(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
cfg.Services = append(cfg.Services, NewService("service1", func(ctx context.Context) error {
logger, err := logr.FromContext(ctx)
assert.NilError(t, err)
logger.Info("running")
defer logger.Info("returning")
<-ctx.Done()
return nil
}))
_, done := newTestFixture(t, cfg)
defer done()
}
func TestSupervisor_IgnoreNilService(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
cfg.Services = append(cfg.Services, nil)
cfg.Services = append(cfg.Services, NewService("service1", func(ctx context.Context) error {
<-ctx.Done()
return nil
}))
_, done := newTestFixture(t, cfg)
defer done()
}
func TestSupervisor_RestartOnError(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
rendezvousChan := make(chan struct{})
cfg.Services = append(cfg.Services, NewService("service1", func(_ context.Context) error {
rendezvousChan <- struct{}{}
return errors.New("boom")
}))
statusUpdateChan := make(chan StatusUpdate, 6)
cfg.StatusUpdateListeners = append(cfg.StatusUpdateListeners, func(statusUpdates []StatusUpdate) {
assert.Assert(t, is.Len(statusUpdates, 1))
assert.Equal(t, "service1", statusUpdates[0].ServiceName)
statusUpdateChan <- statusUpdates[0]
})
f, done := newTestFixture(t, cfg)
assert.Equal(t, StatusIdle, (<-statusUpdateChan).Status)
assert.Equal(t, StatusRunning, (<-statusUpdateChan).Status)
select {
case <-rendezvousChan:
case <-time.After(time.Second):
t.Fatal("timed out waiting for first run")
}
assert.Equal(t, StatusError, (<-statusUpdateChan).Status)
f.restartTickChan <- time.Unix(0, 0)
assert.Equal(t, StatusIdle, (<-statusUpdateChan).Status)
assert.Equal(t, StatusRunning, (<-statusUpdateChan).Status)
select {
case <-rendezvousChan:
case <-time.After(time.Second):
t.Fatal("timed out waiting for second run")
}
done()
assert.Equal(t, StatusError, (<-statusUpdateChan).Status)
}
func TestSupervisor_RestartOnPanic(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
rendezvousChan := make(chan struct{})
cfg.Services = append(cfg.Services, NewService("service1", func(_ context.Context) error {
rendezvousChan <- struct{}{}
panic("boom")
}))
statusUpdateChan := make(chan StatusUpdate, 6)
cfg.StatusUpdateListeners = append(cfg.StatusUpdateListeners, func(statusUpdates []StatusUpdate) {
assert.Assert(t, is.Len(statusUpdates, 1))
assert.Equal(t, "service1", statusUpdates[0].ServiceName)
statusUpdateChan <- statusUpdates[0]
})
f, done := newTestFixture(t, cfg)
assert.Equal(t, StatusIdle, (<-statusUpdateChan).Status)
assert.Equal(t, StatusRunning, (<-statusUpdateChan).Status)
select {
case <-rendezvousChan:
case <-time.After(time.Second):
t.Fatal("timed out waiting for first run")
}
assert.Equal(t, StatusPanic, (<-statusUpdateChan).Status)
f.restartTickChan <- time.Unix(0, 0)
assert.Equal(t, StatusIdle, (<-statusUpdateChan).Status)
assert.Equal(t, StatusRunning, (<-statusUpdateChan).Status)
select {
case <-rendezvousChan:
case <-time.After(time.Second):
t.Fatal("timed out waiting for second run")
}
done()
assert.Equal(t, StatusPanic, (<-statusUpdateChan).Status)
}
func TestSupervisor_MultipleServices(t *testing.T) {
cfg := &Config{
Logger: testr.NewTestLogger(t),
}
const numServices = 10
serviceChan := make(chan struct{})
for i := 0; i < numServices; i++ {
cfg.Services = append(cfg.Services, NewService(fmt.Sprintf("service%d", i), func(ctx context.Context) error {
serviceChan <- struct{}{}
<-ctx.Done()
return nil
}))
}
_, done := newTestFixture(t, cfg)
defer done()
for i := 0; i < numServices; i++ {
select {
case <-serviceChan:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for service %d to run", i)
}
}
}
type mockClock struct {
clock.Clock
now time.Time
tickChan chan time.Time
}
var _ clock.Clock = &mockClock{}
func (m *mockClock) Now() time.Time {
return m.now
}
func (m *mockClock) NewTicker(time.Duration) clock.Ticker {
return &mockTicker{timeChan: m.tickChan}
}
type mockTicker struct {
timeChan chan time.Time
}
var _ clock.Ticker = &mockTicker{}
func (m *mockTicker) C() <-chan time.Time {
return m.timeChan
}
func (m *mockTicker) Stop() {}
func (m *mockTicker) Reset(_ time.Duration) {}
type testFixture struct {
clock *mockClock
restartTickChan chan time.Time
}
func newTestFixture(t *testing.T, cfg *Config) (*testFixture, func()) {
t.Helper()
restartTickChan := make(chan time.Time)
f := &testFixture{
restartTickChan: restartTickChan,
clock: &mockClock{tickChan: restartTickChan},
}
cfg.RestartInterval = time.Second
cfg.Clock = f.clock
s := New(cfg)
var g errgroup.Group
ctx, cancel := context.WithCancel(context.Background())
g.Go(func() error {
return s.Run(ctx)
})
done := func() {
cancel()
assert.NilError(t, g.Wait())
}
return f, done
}