-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediator_test.go
469 lines (366 loc) · 11.3 KB
/
mediator_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
package mediator
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
)
// Handler tests
type TestRequest struct {
value string
}
type TestHandler[TRequest any, TResponse any] struct {
executed bool
}
func (h *TestHandler[TRequest, TResponse]) Handle(_ context.Context, request TRequest) (TRequest, error) {
h.executed = true
return request, nil
}
func Test_RegisterRequestHandler_Stores_Registered_Handler(t *testing.T) {
// Arrange
testHandler := TestHandler[string, string]{}
// Act
err := RegisterRequestHandler[string, string](&testHandler)
// Assert
if err != nil {
t.Errorf("failed to register handler: %s", err.Error())
}
if len(requestHandlers) != 1 {
t.Errorf(
"mediator.requestHandlers length '%d' does not match the expected length '%d' after registering a handler",
len(requestHandlers),
1,
)
}
requestHandlers = make(map[reflect.Type]any)
}
func Test_RegisterRequestHandler_Stores_Registered_Handler_Of_Struct_Type(t *testing.T) {
// Arrange
testHandler := TestHandler[TestRequest, TestRequest]{}
// Act
err := RegisterRequestHandler[TestRequest, TestRequest](&testHandler)
// Assert
if err != nil {
t.Errorf("failed to register handler: %s", err.Error())
}
if len(requestHandlers) != 1 {
t.Errorf(
"mediator.requestHandlers length '%d' does not match the expected length '%d' after registering a handler",
len(requestHandlers),
1,
)
}
requestHandlers = make(map[reflect.Type]any)
}
func Test_RegisterRequestHandler_Returns_Error_When_Registering_Handler_For_Already_Registered_Request_Type(t *testing.T) {
// Arrange
testHandler := TestHandler[string, string]{}
err := RegisterRequestHandler[string, string](&testHandler)
if err != nil {
t.Errorf("failed to register handler: %s", err.Error())
}
testHandler2 := TestHandler[string, string]{}
// Act
err = RegisterRequestHandler[string, string](&testHandler2)
// Assert
if err == nil {
t.Errorf(
"did not receive expected error when registering a handler with request type that already has a registered handler",
)
}
if len(requestHandlers) != 1 {
t.Errorf(
"mediator.pipelineBehaviors length '%d' does not match the expected value '%d' after trying to register a second pipeline behavior with same request type",
len(pipelineBehaviors),
1,
)
}
requestHandlers = make(map[reflect.Type]any)
}
func Test_Send_Executes_Registered_Handler_For_Given_Request_Type(t *testing.T) {
// Arrange
testHandler := TestHandler[string, string]{}
_ = RegisterRequestHandler[string, string](&testHandler)
// Act
_, err := Send[string, string](context.Background(), "value")
// Assert
if err != nil {
t.Errorf("unexpected error occurred on Send: %s", err.Error())
}
if !testHandler.executed {
t.Error("failed to execute registered handler")
}
requestHandlers = make(map[reflect.Type]any)
}
func Test_Send_Executes_Registered_Handler_For_Given_Request_Type_With_Struct_Request_Type(t *testing.T) {
// Arrange
testHandler := TestHandler[TestRequest, TestRequest]{}
_ = RegisterRequestHandler[TestRequest, TestRequest](&testHandler)
// Act
_, err := Send[TestRequest, TestRequest](context.Background(), TestRequest{value: "value"})
// Assert
if err != nil {
t.Errorf("unexpected error occurred on Send: %s", err.Error())
}
if !testHandler.executed {
t.Error("failed to execute registered handler")
}
requestHandlers = make(map[reflect.Type]any)
}
func Test_Send_Returns_Error_When_Sending_Request_Type_Without_A_Matching_Registered_Handler(t *testing.T) {
// Arrange
testHandler := TestHandler[TestRequest, TestRequest]{}
_ = RegisterRequestHandler[TestRequest, TestRequest](&testHandler)
// Act
_, err := Send[string, string](context.Background(), "value")
// Assert
if err == nil {
t.Error("did not receive expected error on sending a request without registering a matching handler")
}
requestHandlers = make(map[reflect.Type]any)
}
// Pipeline tests
type TestPipelineBehavior struct {
valueToAppend string
}
func (b *TestPipelineBehavior) Handle(
ctx context.Context,
request interface{},
next RequestHandlerFunc,
) (interface{}, error) {
value := request.(string)
value += b.valueToAppend
return next(ctx, value)
}
func Test_RegisterPipelineBehavior_Appends_Behavior_Instance_To_Mediator(t *testing.T) {
// Arrange
testHandler := TestHandler[string, string]{}
err := RegisterRequestHandler[string, string](&testHandler)
if err != nil {
t.Errorf("failed to register handler %s", err.Error())
}
pipelineBehaviorInner := TestPipelineBehavior{}
RegisterPipelineBehavior(&pipelineBehaviorInner)
if len(pipelineBehaviors) != 1 {
t.Errorf(
"mediator.pipelineBehaviors length '%d' does not match the expected value '%d' after registering a pipeline behavior",
len(pipelineBehaviors),
1,
)
}
requestHandlers = make(map[reflect.Type]any)
pipelineBehaviors = make([]PipelineBehavior, 0)
}
func Test_PipelineBehavior_First_Registered_Gets_Executed_First(t *testing.T) {
// Arrange
testHandler := TestHandler[string, string]{}
err := RegisterRequestHandler[string, string](&testHandler)
if err != nil {
t.Errorf("failed to register handler %s", err.Error())
}
appendValueInner := "append1"
pipelineBehaviorInner := TestPipelineBehavior{valueToAppend: appendValueInner}
RegisterPipelineBehavior(&pipelineBehaviorInner)
appendValueOuter := "append2"
pipelineBehaviorOuter := TestPipelineBehavior{valueToAppend: appendValueOuter}
RegisterPipelineBehavior(&pipelineBehaviorOuter)
ctx := context.Background()
request := "Hello, World!"
// Act
response, err := Send[string, string](ctx, request)
// Assert
if err != nil {
t.Errorf("failed to send")
}
if !testHandler.executed {
t.Errorf("failed to execute handler")
}
expected := request + appendValueInner + appendValueOuter
if response != expected {
t.Errorf("unexpected response %s", response)
}
requestHandlers = make(map[reflect.Type]any)
pipelineBehaviors = make([]PipelineBehavior, 0)
}
type TestContextPipelineBehavior struct {
onHandle func(ctx context.Context, request interface{}) (context.Context, interface{})
}
func (h *TestContextPipelineBehavior) Handle(ctx context.Context, request interface{}, next RequestHandlerFunc) (interface{}, error) {
if h.onHandle != nil {
ctx, request = h.onHandle(ctx, request)
}
return next(ctx, request)
}
type TestContextHandler[TRequest any, TResponse any] struct {
onHandle func(ctx context.Context, request TRequest)
receivedMessage TRequest
}
func (h *TestContextHandler[TRequest, TResponse]) Handle(ctx context.Context, request TRequest) (TRequest, error) {
h.receivedMessage = request
if h.onHandle != nil {
h.onHandle(ctx, request)
}
return request, nil
}
type CTXKey string
func Test_PipelineBehavior_Context_Preserved_Over_Execution(t *testing.T) {
// Arrange
const (
contextKey CTXKey = "key"
contextVal string = "value"
)
testHandler := TestContextHandler[string, string]{onHandle: func(ctx context.Context, _ string) {
if val := ctx.Value(contextKey); val != contextVal {
t.Errorf(
"failed to receive value from context expected %s found %s",
contextVal,
val,
)
}
}}
err := RegisterRequestHandler[string, string](&testHandler)
if err != nil {
t.Errorf("failed to register handler %s", err.Error())
}
onPipeline := func(ctx context.Context, request interface{}) (context.Context, interface{}) {
return context.WithValue(ctx, contextKey, "value"), request
}
pipelineBehaviorInner := TestContextPipelineBehavior{onHandle: onPipeline}
RegisterPipelineBehavior(&pipelineBehaviorInner)
pipelineBehaviorOuter := TestContextPipelineBehavior{}
RegisterPipelineBehavior(&pipelineBehaviorOuter)
ctx := context.Background()
request := "Hello, World!"
// Act
_, err = Send[string, string](ctx, request)
// Assert
if err != nil {
t.Errorf("failed to send")
}
pipelineBehaviors = make([]PipelineBehavior, 0)
}
// Notification tests
type TestNotificationHandler[TNotification any] struct {
executedWithNotification TNotification
err error
}
func (h *TestNotificationHandler[TNotification]) Handle(_ context.Context, notification TNotification) error {
if h.err != nil {
return h.err
}
h.executedWithNotification = notification
return nil
}
func Test_RegisterNotificationHandler_Appends_Handler_To_Mediator(t *testing.T) {
// Arrange
handler := TestNotificationHandler[string]{}
// Act
RegisterNotificationHandler[string](&handler)
// Assert
var notification string
notificationHandlersNum := len(notificationHandlers[reflect.TypeOf(notification)])
if notificationHandlersNum != 1 {
t.Errorf("expected '%d' handlers, found '%d'", 1, notificationHandlersNum)
}
notificationHandlers = make(map[reflect.Type][]any)
}
func Test_RegisterNotificationHandler_Appends_Multiple_Handlers_For_Same_Notification_Type_To_Mediator(t *testing.T) {
// Arrange
handlers := []TestNotificationHandler[string]{
{},
{},
{},
{},
{},
}
// Act
for _, handler := range handlers {
RegisterNotificationHandler[string](&handler)
}
// Assert
var notification string
notificationHandlersNum := len(notificationHandlers[reflect.TypeOf(notification)])
if notificationHandlersNum != len(handlers) {
t.Errorf("expected '%d' handlers, found '%d'", 1, notificationHandlersNum)
}
notificationHandlers = make(map[reflect.Type][]any)
}
func Test_Publish_Publishes_To_All_Registered_Notification_Handlers(t *testing.T) {
// Arrange
handlers := []TestNotificationHandler[string]{
{},
{},
{},
{},
{},
}
for i := 0; i < len(handlers); i++ {
RegisterNotificationHandler[string](&handlers[i])
}
notification := "value"
// Act
err := Publish[string](context.Background(), notification)
// Assert
if err != nil {
t.Errorf("received unexpected error on publish: %s", err.Error())
}
for _, handler := range handlers {
if handler.executedWithNotification != notification {
t.Errorf(
"failed to execute all registered notification handlers expected '%s' found '%s'",
notification,
handler.executedWithNotification,
)
}
}
notificationHandlers = make(map[reflect.Type][]any)
}
func Test_Publish_Continues_Publishing_After_Receiving_Error_From_Handler(t *testing.T) {
// Arrange
expectedErr := fmt.Errorf("EXPLOSIONS, MAYHEM, OTHER BAD STUFF")
handlers := []TestNotificationHandler[string]{
{},
{err: expectedErr},
{},
{},
{},
}
for i := 0; i < len(handlers); i++ {
RegisterNotificationHandler[string](&handlers[i])
}
notification := "value"
// Act
err := Publish[string](context.Background(), notification)
// Assert
if err == nil {
t.Error("did not receive expected error on publish")
}
if !errors.Is(err, expectedErr) {
t.Errorf(
"received error of unexpected type expected '%s' received '%s'",
expectedErr.Error(),
err.Error(),
)
}
for _, handler := range handlers {
if handler.executedWithNotification != notification && handler.err == nil {
t.Errorf(
"failed to execute all registered notification handlers expected '%s' found '%s'",
notification,
handler.executedWithNotification,
)
}
}
notificationHandlers = make(map[reflect.Type][]any)
}
func Test_Publish_Does_Not_Return_Error_If_No_Handlers_Found(t *testing.T) {
// Arrange
notification := "value"
// Act
err := Publish[string](context.Background(), notification)
// Assert
if err != nil {
t.Errorf("unexpected error occurred on Publish: %s", err.Error())
}
}