Repository navigation
Expand file tree
/
Copy pathiris_builder_worker_test.go
More file actions
403 lines (319 loc) · 9.54 KB
/
Copy pathiris_builder_worker_test.go
File metadata and controls
403 lines (319 loc) · 9.54 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package iris_test
import (
"bytes"
stdContext "context"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/kataras/iris/v14"
)
const workerTestTimeout = 5 * time.Second
// testWorker records what the builder did to it: how many times Stop was
// called, which error Run saw from its context and, for the ordering test,
// whether the initializer had already run.
type testWorker struct {
runErr error
// runLinger delays the return of Run after its context is done, so a test
// can tell whether Shutdown waits for it.
runLinger time.Duration
// blockRun keeps Run from returning at all until the channel is closed.
blockRun chan struct{}
initialized bool
mu sync.Mutex
runErrIn error
stopCtx stdContext.Context
stops int
closes int
returnedAt time.Time
started chan struct{}
ran chan struct{}
}
func newTestWorker(runErr error) *testWorker {
return &testWorker{
runErr: runErr,
started: make(chan struct{}, 1),
ran: make(chan struct{}),
}
}
func (w *testWorker) Init(stdContext.Context) error {
w.initialized = true
return nil
}
func (w *testWorker) Run(ctx stdContext.Context) error {
w.started <- struct{}{}
if w.runErr != nil {
close(w.ran)
return w.runErr
}
<-ctx.Done()
w.mu.Lock()
w.runErrIn = ctx.Err()
w.mu.Unlock()
if w.blockRun != nil {
<-w.blockRun
}
if w.runLinger > 0 {
time.Sleep(w.runLinger)
}
w.mu.Lock()
w.returnedAt = time.Now()
w.mu.Unlock()
close(w.ran)
return nil
}
func (w *testWorker) Stop(ctx stdContext.Context) error {
w.mu.Lock()
w.stops++
w.stopCtx = ctx
w.mu.Unlock()
return nil
}
func (w *testWorker) Close() error {
w.mu.Lock()
w.closes++
w.mu.Unlock()
return nil
}
func (w *testWorker) counts() (stops, closes int) {
w.mu.Lock()
defer w.mu.Unlock()
return w.stops, w.closes
}
func (w *testWorker) contextError() error {
w.mu.Lock()
defer w.mu.Unlock()
return w.runErrIn
}
func (w *testWorker) stopDeadline() (time.Time, bool) {
w.mu.Lock()
defer w.mu.Unlock()
if w.stopCtx == nil {
return time.Time{}, false
}
return w.stopCtx.Deadline()
}
func (w *testWorker) runReturnedAt() time.Time {
w.mu.Lock()
defer w.mu.Unlock()
return w.returnedAt
}
func workerBuilder(configurators ...iris.Configurator) iris.ServiceBuilder {
return iris.NewBuilder(configurators...).
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth()
}
func buildWithWorker(t *testing.T, configurators []iris.Configurator, deps ...any) *iris.Application {
t.Helper()
return workerBuilder(configurators...).
Services(deps...).
RouterMiddlewares().
Middlewares().
Build()
}
func waitFor(t *testing.T, ch <-chan struct{}, what string) {
t.Helper()
select {
case <-ch:
case <-time.After(workerTestTimeout):
t.Fatalf("timeout waiting for %s", what)
}
}
// Run gets a context which is cancelled at shutdown, and Stop runs once even
// when Shutdown is called twice.
func TestBuilderWorkerRunAndStop(t *testing.T) {
w := newTestWorker(nil)
app := buildWithWorker(t, nil, w)
waitFor(t, w.started, "Run to start")
if err := app.Shutdown(stdContext.Background()); err != nil {
t.Fatal(err)
}
waitFor(t, w.ran, "Run to return")
if err := w.contextError(); err != stdContext.Canceled {
t.Fatalf("expected Run's context to be cancelled, got: %v", err)
}
// A value which is both a Worker and a Closer gets both, Stop then Close.
if stops, closes := w.counts(); stops != 1 || closes != 1 {
t.Fatalf("expected one Stop and one Close, got: %d, %d", stops, closes)
}
if err := app.Shutdown(stdContext.Background()); err != nil {
t.Fatal(err)
}
if stops, _ := w.counts(); stops != 1 {
t.Fatalf("expected Stop to run once across shutdowns, got: %d", stops)
}
}
// Shutdown does not return while Run is still running: a worker which is still
// draining must not have the process exit from under it.
func TestBuilderWorkerShutdownWaitsForRun(t *testing.T) {
w := newTestWorker(nil)
w.runLinger = 300 * time.Millisecond
app := buildWithWorker(t, nil, w)
waitFor(t, w.started, "Run to start")
if err := app.Shutdown(stdContext.Background()); err != nil {
t.Fatal(err)
}
returnedAt := w.runReturnedAt()
if returnedAt.IsZero() {
t.Fatal("expected Shutdown to wait for Run to return")
}
select {
case <-w.ran:
default:
t.Fatal("expected Run to have returned by the time Shutdown returned")
}
}
// A Run which ignores its cancellation is bounded by the stop context: the
// shutdown reports the failure instead of hanging forever.
func TestBuilderWorkerShutdownReportsRunThatDoesNotReturn(t *testing.T) {
w := newTestWorker(nil)
w.blockRun = make(chan struct{})
t.Cleanup(func() { close(w.blockRun) })
app := buildWithWorker(t, nil, w)
waitFor(t, w.started, "Run to start")
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 100*time.Millisecond)
defer cancel()
err := app.Shutdown(ctx)
if err == nil {
t.Fatal("expected a shutdown error for a Run which did not return")
}
if !strings.Contains(err.Error(), "iris_test.testWorker") ||
!strings.Contains(err.Error(), "Run did not return") {
t.Fatalf("unexpected shutdown error: %v", err)
}
}
// A shutdown before the application is ever built cancels the worker and keeps
// the later build from starting it: nothing is left running with a context
// which will never be cancelled.
func TestBuilderWorkerStoppedBeforeStart(t *testing.T) {
w := newTestWorker(nil)
var app *iris.Application
b := workerBuilder(iris.ConfiguratorFunc(func(a *iris.Application) { app = a })).
Services(w).
RouterMiddlewares().
Middlewares()
if err := app.Shutdown(stdContext.Background()); err != nil {
t.Fatal(err)
}
b.Build() // must not start a worker which was already stopped.
select {
case <-w.started:
t.Fatal("expected a stopped worker not to be started by a later Build")
case <-time.After(200 * time.Millisecond):
}
if stops, closes := w.counts(); stops != 1 || closes != 1 {
t.Fatalf("expected one Stop and one Close, got: %d, %d", stops, closes)
}
}
// A Worker which is also an Initializer runs after its own Init: the builder
// starts the workers once every initializer has returned.
func TestBuilderWorkerStartsAfterInitializer(t *testing.T) {
w := newTestWorker(nil)
app := buildWithWorker(t, nil, w)
t.Cleanup(func() { _ = app.Shutdown(stdContext.Background()) })
waitFor(t, w.started, "Run to start")
if !w.initialized {
t.Fatal("expected Init to have run before Run")
}
}
// A Worker is registered as a dependency too, so a controller can take it.
func TestBuilderWorkerIsInjectable(t *testing.T) {
w := newTestWorker(nil)
var injected *testWorker
ctor := func(dep *testWorker) string {
injected = dep
return "ok"
}
app := buildWithWorker(t, nil, w, ctor)
t.Cleanup(func() { _ = app.Shutdown(stdContext.Background()) })
if injected != w {
t.Fatalf("expected the worker to be injectable as a dependency, got: %v", injected)
}
}
// Stop gets the Application.Shutdown context when that one carries a deadline,
// and a ten second budget when it does not.
func TestBuilderWorkerStopContextBudget(t *testing.T) {
t.Run("shutdown deadline is passed through", func(t *testing.T) {
w := newTestWorker(nil)
app := buildWithWorker(t, nil, w)
waitFor(t, w.started, "Run to start")
deadline := time.Now().Add(2 * time.Second)
ctx, cancel := stdContext.WithDeadline(stdContext.Background(), deadline)
defer cancel()
if err := app.Shutdown(ctx); err != nil {
t.Fatal(err)
}
got, ok := w.stopDeadline()
if !ok {
t.Fatal("expected Stop to get a bounded context")
}
if !got.Equal(deadline) {
t.Fatalf("expected the shutdown deadline %v but got: %v", deadline, got)
}
})
t.Run("a context without a deadline gets the ten second budget", func(t *testing.T) {
w := newTestWorker(nil)
app := buildWithWorker(t, nil, w)
waitFor(t, w.started, "Run to start")
before := time.Now()
if err := app.Shutdown(stdContext.Background()); err != nil {
t.Fatal(err)
}
got, ok := w.stopDeadline()
if !ok {
t.Fatal("expected Stop to get a bounded context")
}
if budget := got.Sub(before); budget < 9*time.Second || budget > 11*time.Second {
t.Fatalf("expected a ten second stop budget, got: %v", budget)
}
})
}
// A Run error before shutdown is logged with the worker's type name and does
// not stop the application.
func TestBuilderWorkerRunErrorIsLogged(t *testing.T) {
w := newTestWorker(fmt.Errorf("consumer lost its connection"))
logs := newLogCapture("consumer lost its connection")
configurators := []iris.Configurator{
iris.ConfiguratorFunc(func(app *iris.Application) {
app.Logger().SetOutput(logs)
}),
}
app := buildWithWorker(t, configurators, w)
t.Cleanup(func() { _ = app.Shutdown(stdContext.Background()) })
waitFor(t, w.ran, "Run to return")
waitFor(t, logs.matched, "the Run error to be logged")
output := logs.String()
if !strings.Contains(output, "iris_test.testWorker") {
t.Fatalf("expected the worker's type name in the log output, got: %s", output)
}
}
// logCapture is a logger output which signals when the wanted text is written.
type logCapture struct {
want string
mu sync.Mutex
buf bytes.Buffer
once sync.Once
matched chan struct{}
}
func newLogCapture(want string) *logCapture {
return &logCapture{want: want, matched: make(chan struct{})}
}
func (w *logCapture) Write(p []byte) (int, error) {
w.mu.Lock()
n, err := w.buf.Write(p)
found := strings.Contains(w.buf.String(), w.want)
w.mu.Unlock()
if found {
w.once.Do(func() { close(w.matched) })
}
return n, err
}
func (w *logCapture) String() string {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.String()
}