-
Notifications
You must be signed in to change notification settings - Fork 3
/
should_test.go
494 lines (378 loc) · 13.1 KB
/
should_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
490
491
492
493
494
package gopactor
import (
"testing"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/meamidos/gopactor/options"
"github.com/stretchr/testify/assert"
)
func TestShouldReceive(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldReceive(nil), "not an actor PID")
a.Contains(ShouldReceive(receiver), "One parameter with a message is required")
// Failure: Timeout
a.Contains(ShouldReceive(receiver, "Welcome"), "Timeout")
// Failure: Message mismatch
receiver.Tell("Hello, world!")
a.Contains(ShouldReceive(receiver, "Welcome"), "do not match")
// Success: Massage match
receiver.Tell("Hello, world!")
a.Empty(ShouldReceive(receiver, "Hello, world!"))
// Success: Any message
receiver.Tell("Hello, world!")
a.Empty(ShouldReceive(receiver, nil))
// Cleanup
PactReset()
}
func TestShouldReceiveSomething(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldReceiveSomething(nil), "not an actor PID")
// Failure: Timeout
a.Contains(ShouldReceiveSomething(receiver), "Timeout")
// Success: Any message
receiver.Tell("Hello, world!")
a.Empty(ShouldReceiveSomething(receiver))
// Cleanup
PactReset()
}
func TestShouldReceiveFrom(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("rcv"))
teller, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("tel"))
requestor, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("req"))
// Wrong params
a.Contains(ShouldReceiveFrom(nil), "not an actor PID")
a.Contains(ShouldReceiveFrom(receiver, teller), "Two parameters are required")
a.Contains(ShouldReceiveFrom(receiver, nil, nil), "Sender should be an actor PID")
// Failure: Timeout
a.Contains(ShouldReceiveFrom(receiver, requestor, "from requestor"), "Timeout")
// Failure: Sender unknown
// When tell is used the receiver does not know who is the sender
// NB: This protoactor behaviour might change in the future.
// teller.Tell("ping")
receiver.Tell("from teller")
a.Contains(ShouldReceiveFrom(receiver, teller, "from teller"), "Sender is unknown")
// Failure: Message mismatch
receiver.Request("from requestor", requestor)
a.Contains(ShouldReceiveFrom(receiver, requestor, "from teller"), "Messages do not match")
// Failure: Sender mismatch
receiver.Request("from requestor", requestor)
a.Contains(ShouldReceiveFrom(receiver, teller, "from requestor"), "Sender does not match")
// Success: everything matches
receiver.Request("from requestor", requestor)
a.Empty(ShouldReceiveFrom(receiver, requestor, "from requestor"))
// Success: any message
receiver.Request("from requestor", requestor)
a.Empty(ShouldReceiveFrom(receiver, requestor, nil))
// Cleanup
PactReset()
}
func TestShouldReceiveN(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldReceiveN(nil), "not an actor PID")
a.Contains(ShouldReceiveN(receiver), "the number of expected messages is required")
a.Contains(ShouldReceiveN(receiver, 0), "should be a positive integer")
a.Contains(ShouldReceiveN(receiver, "abc"), "should be a positive integer")
// Failure: Nothing received at all
a.Contains(ShouldReceiveN(receiver, 1), "got 0")
// Failure: Not enough messages received
receiver.Tell("Something")
a.Contains(ShouldReceiveN(receiver, 2), "got 1")
// Success: One message
receiver.Tell("Something")
a.Empty(ShouldReceiveN(receiver, 1))
// Success: Many messages
many := 30
for i := 0; i < many; i++ {
receiver.Tell(i)
}
a.Empty(ShouldReceiveN(receiver, many))
// Cleanup
PactReset()
}
func TestShouldSend(t *testing.T) {
a := assert.New(t)
receiver, _ := actor.SpawnPrefix(actor.FromFunc(func(ctx actor.Context) {}), "rcv")
sender, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "tell" {
ctx.Tell(receiver, "tell from sender")
} else if m == "request" {
ctx.Request(receiver, "request from sender")
}
}
}, options.OptOutboundInterceptionOnly.WithPrefix("snd"))
// Wrong params
a.Contains(ShouldSend(nil), "not an actor PID")
a.Contains(ShouldSend(sender), "One parameter with a message is required")
// Failure: Timeout
a.Contains(ShouldSend(sender, "from sender"), "Timeout")
// Failure: Message mismatch
sender.Tell("tell")
a.Contains(ShouldSend(sender, "foobar"), "do not match")
// Success: Tell: Massage match
sender.Tell("tell")
a.Empty(ShouldSend(sender, "tell from sender"))
// Success: Tell: Any message
sender.Tell("tell")
a.Empty(ShouldSend(sender, nil))
// Success: Request: Massage match
sender.Tell("request")
a.Empty(ShouldSend(sender, "request from sender"))
// Success: Request: Any message
sender.Tell("request")
a.Empty(ShouldSend(sender, nil))
// Cleanup
PactReset()
}
func TestShouldSendTo(t *testing.T) {
a := assert.New(t)
receiver, _ := actor.SpawnPrefix(actor.FromFunc(func(ctx actor.Context) {}), "rcv")
sender, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "tell" {
ctx.Tell(receiver, "tell from sender")
} else if m == "request" {
ctx.Request(receiver, "request from sender")
}
}
}, options.OptOutboundInterceptionOnly.WithPrefix("snd"))
// Wrong params
a.Contains(ShouldSendTo(nil), "not an actor PID")
a.Contains(ShouldSendTo(sender), "Two parameters are required")
a.Contains(ShouldSendTo(sender, nil), "Two parameters are required")
a.Contains(ShouldSendTo(sender, nil, nil), "Receiver should be an actor PID")
// Failure: Timeout
a.Contains(ShouldSendTo(sender, receiver, "from sender"), "Timeout")
// Failure: Message mismatch
sender.Tell("tell")
a.Contains(ShouldSendTo(sender, receiver, "foobar"), "do not match")
// Failure: Receiver mismatch
sender.Tell("tell")
a.Contains(ShouldSendTo(sender, sender, "tell from sender"), "Receiver does not match")
// Success: Tell: Massage match
sender.Tell("tell")
a.Empty(ShouldSendTo(sender, receiver, "tell from sender"))
// Success: Tell: Any message
sender.Tell("tell")
a.Empty(ShouldSendTo(sender, receiver, nil))
// Success: Request: Massage match
sender.Tell("request")
a.Empty(ShouldSendTo(sender, receiver, "request from sender"))
// Success: Request: Any message
sender.Tell("request")
a.Empty(ShouldSendTo(sender, receiver, nil))
// Cleanup
PactReset()
}
func TestShouldSendSomething(t *testing.T) {
a := assert.New(t)
receiver, _ := actor.SpawnPrefix(actor.FromFunc(func(ctx actor.Context) {}), "rcv")
sender, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "tell" {
ctx.Tell(receiver, "tell from sender")
} else if m == "request" {
ctx.Request(receiver, "request from sender")
}
}
}, options.OptOutboundInterceptionOnly.WithPrefix("snd"))
// Wrong params
a.Contains(ShouldSendSomething(nil), "not an actor PID")
// Failure: Timeout
a.Contains(ShouldSendSomething(sender), "Timeout")
// Success: Tell: Any message
sender.Tell("tell")
a.Empty(ShouldSendSomething(sender))
// Success: Request: Any message
sender.Tell("request")
a.Empty(ShouldSendSomething(sender))
// Cleanup
PactReset()
}
func TestShouldSendN(t *testing.T) {
a := assert.New(t)
receiver, _ := actor.SpawnPrefix(actor.FromFunc(func(ctx actor.Context) {}), "rcv")
sender, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "tell" {
ctx.Tell(receiver, "tell from sender")
} else if m == "request" {
ctx.Request(receiver, "request from sender")
}
}
}, options.OptOutboundInterceptionOnly.WithPrefix("snd"))
// Wrong params
a.Contains(ShouldSendN(nil), "not an actor PID")
a.Contains(ShouldSendN(sender), "number of expected messages is required")
a.Contains(ShouldSendN(sender, 0), "should be a positive integer")
a.Contains(ShouldSendN(sender, "abc"), "should be a positive integer")
// Failure: Not sending at all
a.Contains(ShouldSendN(sender, 1), "got 0")
// Failure: Tell: Not sending enough
sender.Tell("tell")
a.Contains(ShouldSendN(sender, 2), "got 1")
// Success: Tell: one message
sender.Tell("tell")
a.Empty(ShouldSendN(sender, 1))
// Success: Request: one message
sender.Tell("request")
a.Empty(ShouldSendN(sender, 1))
// Success: Many messages
many := 30
for i := 0; i < many; i++ {
sender.Tell("tell")
}
a.Empty(ShouldSendN(sender, many))
// Cleanup
PactReset()
}
func TestShouldNotSendOrReceive(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptDefault.WithPrefix("rcv"))
sender, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "tell" {
ctx.Tell(receiver, "tell from sender")
} else if m == "request" {
ctx.Request(receiver, "request from sender")
}
}
}, options.OptOutboundInterceptionOnly.WithPrefix("snd"))
// Wrong params
a.Contains(ShouldNotSendOrReceive(nil), "not an actor PID")
// Success: neither send, nor receive
a.Empty(ShouldNotSendOrReceive(sender))
// Failure: receive
receiver.Tell("foobar")
a.Contains(ShouldNotSendOrReceive(receiver), "Got inbound message")
// Failure: tell
sender.Tell("tell")
a.Contains(ShouldNotSendOrReceive(sender), "Got outbound message")
// Failure: request
sender.Tell("request")
a.Contains(ShouldNotSendOrReceive(sender), "Got outbound message")
// Cleanup
PactReset()
}
func TestShouldStart(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptNoInterception.WithSystemInterception().WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldStart(nil), "not an actor PID")
// Success
a.Empty(ShouldStart(receiver))
// Cleanup
PactReset()
}
func TestShouldStop(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptNoInterception.WithSystemInterception().WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldStop(nil), "not an actor PID")
// Failure: Timeout
a.Contains(ShouldStop(receiver), "Timeout")
// Success
receiver.Stop()
a.Empty(ShouldStop(receiver))
// Cleanup
PactReset()
}
func TestShouldBeRestarting(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "panic" {
panic("I am panicing!")
}
}
}, OptNoInterception.WithSystemInterception().WithPrefix("rcv"))
// Wrong params
a.Contains(ShouldBeRestarting(nil), "not an actor PID")
// Failure: Timeout
a.Contains(ShouldBeRestarting(receiver), "Timeout")
// Success
receiver.Tell("panic")
a.Empty(ShouldBeRestarting(receiver))
a.Empty(ShouldStart(receiver))
// Cleanup
PactReset()
}
func TestShouldObserveTermination(t *testing.T) {
a := assert.New(t)
childProps := actor.FromFunc(func(ctx actor.Context) {})
getParentChildSet := func() (*actor.PID, *actor.PID) {
var child *actor.PID
wait := make(chan bool)
parent, _ := SpawnFromFunc(func(ctx actor.Context) {
switch ctx.Message().(type) {
case *actor.Started:
child = ctx.SpawnPrefix(childProps, "child")
wait <- true
}
}, options.OptNoInterception.WithSystemInterception().WithPrefix("parent"))
<-wait
return parent, child
}
someActor, _ := SpawnNullActor()
// Wrong params
parent1, child1 := getParentChildSet()
a.Contains(ShouldObserveTermination(nil), "not an actor PID")
a.Contains(ShouldObserveTermination(parent1, nil), "should be an actor PID")
// Failure: Timeout
a.Contains(ShouldObserveTermination(parent1, child1), "Timeout")
// Failure: pid mismatch
child1.Stop()
a.Contains(ShouldObserveTermination(parent1, someActor), "Timeout")
// Success: Termination of the child
parent2, child2 := getParentChildSet()
child2.Stop()
a.Empty(ShouldObserveTermination(parent2, child2))
// Success: Any termination
parent3, child3 := getParentChildSet()
child3.Stop()
a.Empty(ShouldObserveTermination(parent3))
// Cleanup
PactReset()
}
func TestShouldSpawn(t *testing.T) {
a := assert.New(t)
receiver, _ := SpawnFromFunc(func(ctx actor.Context) {}, OptNoInterception.WithPrefix("rcv"))
childProps := actor.FromFunc(func(ctx actor.Context) {})
parent, _ := SpawnFromFunc(func(ctx actor.Context) {
switch m := ctx.Message().(type) {
case string:
if m == "spawn" && ctx.Sender() != nil {
child := ctx.SpawnPrefix(childProps, "my-dear-dummy")
ctx.Respond(child)
}
}
}, options.OptNoInterception.WithSpawnInterception().WithPrefix("parent"))
// Wrong params
a.Contains(ShouldSpawn(nil), "not an actor PID")
a.Contains(ShouldSpawn(parent, 123), "should be a string")
// Failure: Timeout
a.Contains(ShouldSpawn(parent), "Timeout")
// Failure: child PID mismatch
parent.Request("spawn", receiver)
a.Contains(ShouldSpawn(parent, "foobar"), "does not match")
// Success: child PID match
parent.Request("spawn", receiver)
a.Empty(ShouldSpawn(parent, "my-dear-dummy"))
// Success: any child PID is ok
parent.Request("spawn", receiver)
a.Empty(ShouldSpawn(parent))
// Cleanup
PactReset()
}