forked from go-joe/joe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain_test.go
489 lines (409 loc) · 11.7 KB
/
brain_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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package joe
import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zaptest/observer"
)
var ctx = context.Background() // default background context
func TestBrain_RegisterHandler(t *testing.T) {
type TestEvent struct {
EventHandled *sync.WaitGroup
}
cases := map[string]struct {
fun interface{}
err string
}{
"err_no_arg": {
fun: func() {},
err: "event handler needs one or two arguments",
},
"err_pointer": {
fun: func(evt *TestEvent) {},
err: "event handler argument cannot be a pointer",
},
"err_too_many_args": {
fun: func(evt1, evt2, evt3, evt4 TestEvent) {},
err: "event handler needs one or two arguments",
},
"err_too_many_events": {
fun: func(evt1, evt2 TestEvent) {},
err: "event handler has two arguments but the first is not a context.Context",
},
"err_context": {
fun: func(TestEvent, context.Context) {},
err: "event handler context must be the first argument",
},
"err_too_many_results": {
fun: func(TestEvent) (err1, err2 error) { return nil, nil },
err: "event handler has more than one return value",
},
"err_wrong_result": {
fun: func(TestEvent) int { return 42 },
err: "if the event handler has a return value it must implement the error interface",
},
"ok_simple": {
fun: func(evt TestEvent) {
evt.EventHandled.Done()
},
},
"ok_with_error": {
fun: func(evt TestEvent) error {
evt.EventHandled.Done()
return nil
},
},
"ok_with_context": {
fun: func(ctx context.Context, evt TestEvent) {
evt.EventHandled.Done()
},
},
"ok_with_context_and_error": {
fun: func(ctx context.Context, evt TestEvent) error {
evt.EventHandled.Done()
return nil
},
},
"ok_interface": {
fun: func(evt interface{}) {
evt.(TestEvent).EventHandled.Done()
},
},
"ok_interface_with_context": {
fun: func(ctx context.Context, evt interface{}) {
evt.(TestEvent).EventHandled.Done()
},
},
"ok_interface_with_error": {
fun: func(evt interface{}) error {
evt.(TestEvent).EventHandled.Done()
return nil
},
},
"ok_interface_with_context_and_error": {
fun: func(ctx context.Context, evt interface{}) error {
evt.(TestEvent).EventHandled.Done()
return nil
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
b.RegisterHandler(c.fun)
if c.err != "" {
require.Len(t, b.registrationErrs, 1)
err := b.registrationErrs[0].Error()
if !strings.HasSuffix(err, c.err) {
t.Errorf("unexpected registration error\nexpected: %q\nactual : %q", c.err, err)
}
return
}
require.Empty(t, b.registrationErrs, "unexpected registration errors")
// Start the brains event handler loop.
go b.HandleEvents()
defer b.Shutdown(ctx)
// Emit our test event.
wg := new(sync.WaitGroup)
wg.Add(1)
evt := TestEvent{EventHandled: wg}
b.Emit(evt)
// Wait until the handler marks the event as handled
done := make(chan bool)
go func() {
wg.Wait()
done <- true
}()
select {
case <-done:
// ok cool
case <-time.After(time.Second):
t.Error("Event handler was not executed within one second")
}
})
}
}
func TestBrain_HandlerErrors(t *testing.T) {
type TestEvent struct{}
// In this test we actually want to check if the handler errors get logged.
// This is achieved by using go.uber.org/zap/zaptest/observer
obs, logs := observer.New(zap.DebugLevel)
logger := zap.New(obs)
b := NewBrain(logger)
handlerErr := errors.New("test error")
b.RegisterHandler(func(TestEvent) error {
return handlerErr
})
go b.HandleEvents()
defer b.Shutdown(ctx)
EmitSync(b, TestEvent{})
expectedLog := observer.LoggedEntry{
Entry: zapcore.Entry{Level: zap.ErrorLevel, Message: "Event handler failed"},
Context: []zapcore.Field{zap.Error(handlerErr)},
}
handlerErrLogs := logs.FilterMessage(expectedLog.Message).AllUntimed()
require.Equal(t, 1, len(handlerErrLogs))
assert.Equal(t, expectedLog, handlerErrLogs[0])
}
func TestBrain_Emit_PassAllEventData(t *testing.T) {
type TestEvent struct {
Test bool
unexported string
}
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
var seen TestEvent
b.RegisterHandler(func(evt TestEvent) {
seen = evt
})
go b.HandleEvents()
defer b.Shutdown(ctx)
event := TestEvent{Test: true, unexported: "hello"}
EmitSync(b, event)
assert.Equal(t, event, seen)
}
func TestBrain_Emit_ImmutableEvent(t *testing.T) {
type TestEvent struct {
String string
}
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
b.RegisterHandler(func(evt TestEvent) {
evt.String = "bar"
})
go b.HandleEvents()
defer b.Shutdown(ctx)
event := TestEvent{String: "foo"}
EmitSync(b, event)
assert.Equal(t, "foo", event.String)
}
func TestBrain_Emit_Scalar(t *testing.T) {
type TestEvent int
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
var handlerExecuted bool
b.RegisterHandler(func(evt TestEvent) {
handlerExecuted = true
assert.Equal(t, TestEvent(42), evt)
})
require.Empty(t, b.registrationErrs)
go b.HandleEvents()
defer b.Shutdown(ctx)
event := TestEvent(42)
EmitSync(b, event)
assert.True(t, handlerExecuted)
}
func TestBrain_HandlerPanics(t *testing.T) {
type TestEvent struct{}
// In this test we actually want to check if the handler panic gets logged.
// This is achieved by using go.uber.org/zap/zaptest/observer
obs, logs := observer.New(zap.DebugLevel)
logger := zap.New(obs)
b := NewBrain(logger)
var handlerCalled bool
b.RegisterHandler(func(TestEvent) {
handlerCalled = true
panic("something went horribly wrong")
})
go b.HandleEvents()
defer b.Shutdown(ctx)
EmitSync(b, TestEvent{})
assert.True(t, handlerCalled)
handlerErrLogs := logs.FilterMessage("Event handler failed")
require.Equal(t, 1, handlerErrLogs.Len())
logEntry := handlerErrLogs.All()[0]
assert.Equal(t, "error", logEntry.Level.String())
assert.NotEmpty(t, logEntry.Context, "expected log entry to have at least one field")
for _, field := range logEntry.Context {
switch field.Key {
case "error":
assert.Equal(t, zapcore.ErrorType, field.Type)
err := field.Interface.(error)
assert.EqualError(t, err, "handler panic: something went horribly wrong")
default:
t.Errorf("unexpected field %q in log entry", field.Key)
}
}
}
func TestBrain_Shutdown_WithoutStart(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
done := make(chan bool)
go func() {
b.Shutdown(ctx)
done <- true
}()
select {
case <-done:
// hurray!
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
func TestBrain_Shutdown_MultipleTimes(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
n := 100
done := make(chan bool, n)
for i := 0; i < n; i++ {
go func() {
b.Shutdown(ctx)
done <- true
}()
}
// All shutdown functions should return and nothing should deadlock or cause
// a panic (e.g. closing channels twice).
for i := 0; i < n; i++ {
select {
case <-done:
// hurray!
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
}
func TestBrain_EmitAfterShutdown(t *testing.T) {
obs, logs := observer.New(zap.DebugLevel)
logger := zap.New(obs)
b := NewBrain(logger)
b.Shutdown(ctx)
// Emitting new events after shutdown should not block or panic
type TestEvent struct{}
b.Emit(ReceiveMessageEvent{})
b.Emit(UserTypingEvent{})
b.Emit(TestEvent{})
all := logs.AllUntimed()
require.Len(t, all, 3)
for i, logEvent := range all {
assert.Equal(t, "Ignoring new event because brain is currently shutting down or is already closed", logEvent.Message)
require.Len(t, logEvent.Context, 1)
assert.Equal(t, "type", logEvent.Context[0].Key)
switch i {
case 0:
assert.Equal(t, "joe.ReceiveMessageEvent", logEvent.Context[0].String)
case 1:
assert.Equal(t, "joe.UserTypingEvent", logEvent.Context[0].String)
case 2:
assert.Equal(t, "joe.TestEvent", logEvent.Context[0].String)
}
}
}
func TestBrain_ShutdownContext(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
// This test uses a chan chan to communicate synchronously with the handler below.
shutdownHandlerCallback := make(chan chan bool)
b.RegisterHandler(func(ShutdownEvent) {
t.Log("ShutdownEvent handler started and blocking until further notice")
ok := <-shutdownHandlerCallback
t.Log("ShutdownEvent received signal and exits now")
ok <- true
})
started := make(chan bool)
go func() {
t.Log("Event handler goroutine started")
started <- true
b.HandleEvents()
}()
<-started // wait until the HandleEvents goroutine is running
shutdownCtx, cancel := context.WithCancel(ctx)
shutdownDone := make(chan bool, 1)
go func() {
t.Log("Starting shutdown")
b.Shutdown(shutdownCtx)
t.Log("Starting completed")
shutdownDone <- true
}()
// At this point the shutdown should be in progress but block in the handler
select {
case <-shutdownDone:
t.Fatal("Shutdown function exited without calling ShutdownEvent handler")
case <-time.After(10 * time.Millisecond):
// ok, seems like shutdown is actually blocked and we can move on
}
t.Log("Canceling shutdown context")
cancel()
select {
case <-shutdownDone:
// ok great, lets move on
case <-time.After(10 * time.Millisecond):
t.Error("Shutdown function did not return even though the context was canceled")
}
// Finally lets release the shutdown event handler and finish the test
callback := make(chan bool)
shutdownHandlerCallback <- callback
<-callback
}
// TestBrain_RegisterMultiple registers multiple handlers for the same event and
// checks they are executed in the order in which they have been registered.
func TestBrain_RegisterMultiple(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
type TestEvent struct{}
var execSequence []string // tracks order of handler execution
h1 := func(TestEvent) {
execSequence = append(execSequence, "h1")
}
h2 := func(TestEvent) error {
execSequence = append(execSequence, "h2")
return nil
}
h3 := func(context.Context, TestEvent) {
execSequence = append(execSequence, "h3")
}
h4 := func(context.Context, TestEvent) error {
execSequence = append(execSequence, "h4")
return nil
}
b.RegisterHandler(h1)
b.RegisterHandler(h2)
b.RegisterHandler(h3)
b.RegisterHandler(h4)
require.Empty(t, b.registrationErrs, "unexpected registration errors")
go b.HandleEvents()
defer b.Shutdown(ctx)
EmitSync(b, TestEvent{})
assert.Equal(t, []string{"h1", "h2", "h3", "h4"}, execSequence)
}
// TestFinishEventContent tests that handlers can mark an event as processed to
// avoid later handlers to be executed on the given event.
func TestFinishEventContent(t *testing.T) {
logger := zaptest.NewLogger(t)
b := NewBrain(logger)
type TestEvent struct{}
var h1Executed bool
h1 := func(ctx context.Context, evt TestEvent) {
h1Executed = true
FinishEventContent(ctx)
}
var h2Executed bool
h2 := func(TestEvent) {
h2Executed = true
}
b.RegisterHandler(h1)
b.RegisterHandler(h2)
require.Empty(t, b.registrationErrs, "unexpected registration errors")
go b.HandleEvents()
defer b.Shutdown(ctx)
EmitSync(b, TestEvent{})
assert.True(t, h1Executed, "first handler should have been executed")
assert.False(t, h2Executed, "second handler should not have been executed")
}
// EmitSync emits the given event on the brain and blocks until it has received
// the context which indicates that the event was fully processed by all
// matching handlers.
func EmitSync(brain EventEmitter, event interface{}) {
done := make(chan bool)
callback := func(Event) { done <- true }
brain.Emit(event, callback)
<-done
}