/
ActorContextSpec.scala
733 lines (662 loc) · 27.6 KB
/
ActorContextSpec.scala
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package akka.typed
import scala.concurrent.duration._
import scala.concurrent.Future
import com.typesafe.config.ConfigFactory
import akka.actor.DeadLetterSuppression
import akka.typed.scaladsl.Actor
import akka.typed.scaladsl.Actor.Stateful
object ActorContextSpec {
sealed trait Command
sealed trait Event
sealed trait Monitor extends Event
final case class GotSignal(signal: Signal) extends Monitor with DeadLetterSuppression
final case object GotReceiveTimeout extends Monitor
final case object ReceiveTimeout extends Command
final case class Ping(replyTo: ActorRef[Pong]) extends Command
sealed trait Pong extends Event
case object Pong1 extends Pong
case object Pong2 extends Pong
final case class Miss(replyTo: ActorRef[Missed.type]) extends Command
case object Missed extends Event
final case class Renew(replyTo: ActorRef[Renewed.type]) extends Command
case object Renewed extends Event
final case class Throw(ex: Exception) extends Command
final case class MkChild(name: Option[String], monitor: ActorRef[Monitor], replyTo: ActorRef[Created]) extends Command
final case class Created(ref: ActorRef[Command]) extends Event
final case class SetTimeout(duration: FiniteDuration, replyTo: ActorRef[TimeoutSet.type]) extends Command
case object TimeoutSet extends Event
final case class Schedule[T](delay: FiniteDuration, target: ActorRef[T], msg: T, replyTo: ActorRef[Scheduled.type]) extends Command
case object Scheduled extends Event
case object Stop extends Command
final case class Kill(ref: ActorRef[Nothing], replyTo: ActorRef[KillResult]) extends Command
sealed trait KillResult extends Event
case object Killed extends KillResult
case object NotKilled extends KillResult
final case class Watch(ref: ActorRef[Nothing], replyTo: ActorRef[Watched.type]) extends Command
case object Watched extends Event
final case class Unwatch(ref: ActorRef[Nothing], replyTo: ActorRef[Unwatched.type]) extends Command
case object Unwatched extends Event
final case class GetInfo(replyTo: ActorRef[Info]) extends Command
final case class Info(self: ActorRef[Command], system: ActorSystem[Nothing]) extends Event
final case class GetChild(name: String, replyTo: ActorRef[Child]) extends Command
final case class Child(c: Option[ActorRef[Nothing]]) extends Event
final case class GetChildren(replyTo: ActorRef[Children]) extends Command
final case class Children(c: Set[ActorRef[Nothing]]) extends Event
final case class ChildEvent(event: Event) extends Event
final case class BecomeInert(replyTo: ActorRef[BecameInert.type]) extends Command
case object BecameInert extends Event
final case class BecomeCareless(replyTo: ActorRef[BecameCareless.type]) extends Command
case object BecameCareless extends Event
final case class GetAdapter(replyTo: ActorRef[Adapter], name: String = "") extends Command
final case class Adapter(a: ActorRef[Command]) extends Event
def subject(monitor: ActorRef[Monitor]): Behavior[Command] =
Actor.Stateful(
(ctx, message) ⇒ message match {
case ReceiveTimeout ⇒
monitor ! GotReceiveTimeout
Actor.Same
case Ping(replyTo) ⇒
replyTo ! Pong1
Actor.Same
case Miss(replyTo) ⇒
replyTo ! Missed
Actor.Unhandled
case Renew(replyTo) ⇒
replyTo ! Renewed
subject(monitor)
case Throw(ex) ⇒
throw ex
case MkChild(name, mon, replyTo) ⇒
val child = name match {
case None ⇒ ctx.spawnAnonymous(Actor.Restarter[Throwable]().wrap(subject(mon)))
case Some(n) ⇒ ctx.spawn(Actor.Restarter[Throwable]().wrap(subject(mon)), n)
}
replyTo ! Created(child)
Actor.Same
case SetTimeout(d, replyTo) ⇒
d match {
case f: FiniteDuration ⇒ ctx.setReceiveTimeout(f, ReceiveTimeout)
case _ ⇒ ctx.cancelReceiveTimeout()
}
replyTo ! TimeoutSet
Actor.Same
case Schedule(delay, target, msg, replyTo) ⇒
replyTo ! Scheduled
ctx.schedule(delay, target, msg)
Actor.Same
case Stop ⇒ Actor.Stopped
case Kill(ref, replyTo) ⇒
if (ctx.stop(ref)) replyTo ! Killed
else replyTo ! NotKilled
Actor.Same
case Watch(ref, replyTo) ⇒
ctx.watch[Nothing](ref)
replyTo ! Watched
Actor.Same
case Unwatch(ref, replyTo) ⇒
ctx.unwatch[Nothing](ref)
replyTo ! Unwatched
Actor.Same
case GetInfo(replyTo) ⇒
replyTo ! Info(ctx.self, ctx.system)
Actor.Same
case GetChild(name, replyTo) ⇒
replyTo ! Child(ctx.child(name))
Actor.Same
case GetChildren(replyTo) ⇒
replyTo ! Children(ctx.children.toSet)
Actor.Same
case BecomeInert(replyTo) ⇒
replyTo ! BecameInert
Actor.Stateless {
case (_, Ping(replyTo)) ⇒
replyTo ! Pong2
case (_, Throw(ex)) ⇒
throw ex
case _ ⇒ ()
}
case BecomeCareless(replyTo) ⇒
replyTo ! BecameCareless
Actor.Stateful(
(ctx, message) ⇒ Actor.Unhandled,
(ctx, signal) ⇒ signal match {
case Terminated(_) ⇒ Actor.Unhandled
case sig ⇒
monitor ! GotSignal(sig)
Actor.Same
})
case GetAdapter(replyTo, name) ⇒
replyTo ! Adapter(ctx.spawnAdapter(identity, name))
Actor.Same
},
(ctx, signal) ⇒ { monitor ! GotSignal(signal); Actor.Same })
def oldSubject(monitor: ActorRef[Monitor]): Behavior[Command] = {
import ScalaDSL._
FullTotal {
case Sig(ctx, signal) ⇒
monitor ! GotSignal(signal)
Same
case Msg(ctx, message) ⇒ message match {
case ReceiveTimeout ⇒
monitor ! GotReceiveTimeout
Same
case Ping(replyTo) ⇒
replyTo ! Pong1
Same
case Miss(replyTo) ⇒
replyTo ! Missed
Unhandled
case Renew(replyTo) ⇒
replyTo ! Renewed
subject(monitor)
case Throw(ex) ⇒
throw ex
case MkChild(name, mon, replyTo) ⇒
val child = name match {
case None ⇒ ctx.spawnAnonymous(patterns.Restarter[Command, Throwable](subject(mon), false)())
case Some(n) ⇒ ctx.spawn(patterns.Restarter[Command, Throwable](subject(mon), false)(), n)
}
replyTo ! Created(child)
Same
case SetTimeout(d, replyTo) ⇒
d match {
case f: FiniteDuration ⇒ ctx.setReceiveTimeout(f, ReceiveTimeout)
case _ ⇒ ctx.cancelReceiveTimeout()
}
replyTo ! TimeoutSet
Same
case Schedule(delay, target, msg, replyTo) ⇒
replyTo ! Scheduled
ctx.schedule(delay, target, msg)
Same
case Stop ⇒ Stopped
case Kill(ref, replyTo) ⇒
if (ctx.stop(ref)) replyTo ! Killed
else replyTo ! NotKilled
Same
case Watch(ref, replyTo) ⇒
ctx.watch[Nothing](ref)
replyTo ! Watched
Same
case Unwatch(ref, replyTo) ⇒
ctx.unwatch[Nothing](ref)
replyTo ! Unwatched
Same
case GetInfo(replyTo) ⇒
replyTo ! Info(ctx.self, ctx.system)
Same
case GetChild(name, replyTo) ⇒
replyTo ! Child(ctx.child(name))
Same
case GetChildren(replyTo) ⇒
replyTo ! Children(ctx.children.toSet)
Same
case BecomeInert(replyTo) ⇒
replyTo ! BecameInert
Full {
case Msg(_, Ping(replyTo)) ⇒
replyTo ! Pong2
Same
case Msg(_, Throw(ex)) ⇒
throw ex
case _ ⇒ Same
}
case BecomeCareless(replyTo) ⇒
replyTo ! BecameCareless
Full {
case Sig(_, Terminated(_)) ⇒ Unhandled
case Sig(_, sig) ⇒
monitor ! GotSignal(sig)
Same
}
case GetAdapter(replyTo, name) ⇒
replyTo ! Adapter(ctx.spawnAdapter(identity, name))
Same
}
}
}
}
class ActorContextSpec extends TypedSpec(ConfigFactory.parseString(
"""|akka {
| loglevel = WARNING
| actor.debug {
| lifecycle = off
| autoreceive = off
| }
|}""".stripMargin)) {
import ActorContextSpec._
val expectTimeout = 3.seconds
trait Tests {
/**
* The name for the set of tests to be instantiated, used for keeping the test case actors’ names unique.
*/
def suite: String
/**
* The behavior against which to run all the tests.
*/
def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command]
implicit def system: ActorSystem[TypedSpec.Command]
private def mySuite: String =
if (system eq nativeSystem) suite + "Native"
else suite + "Adapted"
def setup(name: String, wrapper: Option[Actor.Restarter.Apply[_]] = None)(
proc: (scaladsl.ActorContext[Event], StepWise.Steps[Event, ActorRef[Command]]) ⇒ StepWise.Steps[Event, _]): Future[TypedSpec.Status] =
runTest(s"$mySuite-$name")(StepWise[Event] { (ctx, startWith) ⇒
val props = wrapper.map(_.wrap(behavior(ctx))).getOrElse(behavior(ctx))
val steps =
startWith.withKeepTraces(true)(ctx.spawn(props, "subject"))
.expectMessage(expectTimeout) { (msg, ref) ⇒
msg should ===(GotSignal(PreStart))
ref
}
proc(ctx, steps)
})
private implicit class MkC(val startWith: StepWise.Steps[Event, ActorRef[Command]]) {
/**
* Ask the subject to create a child actor, setting its behavior to “inert” if requested.
* The latter is very useful in order to avoid disturbances with GotSignal(PostStop) in
* test procedures that stop this child.
*/
def mkChild(
name: Option[String],
monitor: ActorRef[Event],
self: ActorRef[Event],
inert: Boolean = false): StepWise.Steps[Event, (ActorRef[Command], ActorRef[Command])] = {
val s =
startWith.keep { subj ⇒
subj ! MkChild(name, monitor, self)
}.expectMultipleMessages(expectTimeout, 2) { (msgs, subj) ⇒
val child = msgs match {
case Created(child) :: ChildEvent(GotSignal(PreStart)) :: Nil ⇒ child
case ChildEvent(GotSignal(PreStart)) :: Created(child) :: Nil ⇒ child
}
(subj, child)
}
if (!inert) s
else
s.keep {
case (subj, child) ⇒
child ! BecomeInert(self)
}.expectMessageKeep(expectTimeout) { (msg, _) ⇒
msg should ===(BecameInert)
}
}
}
private implicit class MessageStep[T](val startWith: StepWise.Steps[Event, T]) {
def stimulate(f: T ⇒ Unit, ev: T ⇒ Event, timeout: FiniteDuration = expectTimeout): StepWise.Steps[Event, T] =
startWith.keep(f).expectMessageKeep(timeout) { (msg, v) ⇒
msg should ===(ev(v))
}
}
protected def stop(ref: ActorRef[Command]) = ref ! Stop
def `00 must canonicalize behaviors`(): Unit = sync(setup("ctx00") { (ctx, startWith) ⇒
val self = ctx.self
startWith.keep { subj ⇒
subj ! Ping(self)
}.expectMessageKeep(expectTimeout) { (msg, subj) ⇒
msg should ===(Pong1)
subj ! Miss(self)
}.expectMessageKeep(expectTimeout) { (msg, subj) ⇒
msg should ===(Missed)
subj ! Renew(self)
}.expectMessage(expectTimeout) { (msg, subj) ⇒
msg should ===(Renewed)
subj ! Ping(self)
}.expectMessage(expectTimeout) { (msg, _) ⇒
msg should ===(Pong1)
}
})
def `01 must correctly wire the lifecycle hooks`(): Unit = sync(setup("ctx01", Some(Actor.Restarter[Throwable]())) { (ctx, startWith) ⇒
val self = ctx.self
val ex = new Exception("KABOOM1")
startWith { subj ⇒
val log = muteExpectedException[Exception]("KABOOM1", occurrences = 1)
subj ! Throw(ex)
(subj, log)
}.expectMessage(expectTimeout) {
case (msg, (subj, log)) ⇒
msg should ===(GotSignal(PreRestart))
log.assertDone(expectTimeout)
subj
}.expectMessage(expectTimeout) { (msg, subj) ⇒
msg should ===(GotSignal(PreStart))
ctx.stop(subj)
}.expectMessage(expectTimeout) { (msg, _) ⇒
msg should ===(GotSignal(PostStop))
}
})
def `02 must not signal PostStop after voluntary termination`(): Unit = sync(setup("ctx02") { (ctx, startWith) ⇒
startWith.keep { subj ⇒
ctx.watch(subj)
stop(subj)
}.expectTermination(expectTimeout) { (t, subj) ⇒
t.ref should ===(subj)
}
})
def `03 must restart and stop a child actor`(): Unit = sync(setup("ctx03") { (ctx, startWith) ⇒
val self = ctx.self
val ex = new Exception("KABOOM2")
startWith.mkChild(None, ctx.spawnAdapter(ChildEvent), self) {
case (subj, child) ⇒
val log = muteExpectedException[Exception]("KABOOM2", occurrences = 1)
child ! Throw(ex)
(subj, child, log)
}.expectMultipleMessages(expectTimeout, 2) {
case (msgs, (subj, child, log)) ⇒
msgs should ===(
ChildEvent(GotSignal(PreRestart)) ::
ChildEvent(GotSignal(PreStart)) :: Nil)
log.assertDone(expectTimeout)
child ! BecomeInert(self) // necessary to avoid PostStop/Terminated interference
(subj, child)
}.expectMessageKeep(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(BecameInert)
stop(subj)
ctx.watch(child)
ctx.watch(subj)
}.expectTermination(expectTimeout) {
case (t, (subj, child)) ⇒
if (t.ref === child) subj
else if (t.ref === subj) child
else fail(s"expected termination of either $subj or $child but got $t")
}.expectTermination(expectTimeout) { (t, subj) ⇒
t.ref should ===(subj)
}
})
def `04 must stop a child actor`(): Unit = sync(setup("ctx04") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(Some("A"), ctx.spawnAdapter(ChildEvent), self, inert = true) {
case (subj, child) ⇒
subj ! Kill(child, self)
child
}.expectMessageKeep(expectTimeout) { (msg, child) ⇒
msg should ===(Killed)
ctx.watch(child)
}.expectTermination(expectTimeout) { (t, child) ⇒
t.ref should ===(child)
}
})
def `05 must reset behavior upon Restart`(): Unit = sync(setup("ctx05", Some(Actor.Restarter[Exception]())) { (ctx, startWith) ⇒
val self = ctx.self
val ex = new Exception("KABOOM05")
startWith
.stimulate(_ ! BecomeInert(self), _ ⇒ BecameInert)
.stimulate(_ ! Ping(self), _ ⇒ Pong2) { subj ⇒
val log = muteExpectedException[Exception]("KABOOM05")
subj ! Throw(ex)
(subj, log)
}.expectMessage(expectTimeout) {
case (msg, (subj, log)) ⇒
msg should ===(GotSignal(PreStart))
log.assertDone(expectTimeout)
subj
}
.stimulate(_ ! Ping(self), _ ⇒ Pong1)
})
def `06 must not reset behavior upon Resume`(): Unit = sync(setup("ctx06", Some(Actor.Restarter[Exception](resume = true))) { (ctx, startWith) ⇒
val self = ctx.self
val ex = new Exception("KABOOM06")
startWith
.stimulate(_ ! BecomeInert(self), _ ⇒ BecameInert)
.stimulate(_ ! Ping(self), _ ⇒ Pong2).keep { subj ⇒
muteExpectedException[Exception]("KABOOM06", occurrences = 1)
subj ! Throw(ex)
}.stimulate(_ ! Ping(self), _ ⇒ Pong2)
})
def `07 must stop upon Stop`(): Unit = sync(setup("ctx07") { (ctx, startWith) ⇒
val self = ctx.self
val ex = new Exception("KABOOM07")
startWith
.stimulate(_ ! Ping(self), _ ⇒ Pong1).keep { subj ⇒
muteExpectedException[Exception]("KABOOM07", occurrences = 1)
subj ! Throw(ex)
ctx.watch(subj)
}.expectMulti(expectTimeout, 2) { (msgs, subj) ⇒
msgs.toSet should ===(Set(Left(Terminated(subj)(null)), Right(GotSignal(PostStop))))
}
})
def `08 must not stop non-child actor`(): Unit = sync(setup("ctx08") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(Some("A"), ctx.spawnAdapter(ChildEvent), self) { pair ⇒
(pair._1, pair._2, ctx.spawn(behavior(ctx), "A"))
}.expectMessage(expectTimeout) {
case (msg, (subj, child, other)) ⇒
msg should ===(GotSignal(PreStart))
subj ! Kill(other, ctx.self)
child
}.expectMessageKeep(expectTimeout) { (msg, _) ⇒
msg should ===(NotKilled)
}.stimulate(_ ! Ping(self), _ ⇒ Pong1)
})
def `10 must watch a child actor before its termination`(): Unit = sync(setup("ctx10") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(None, ctx.spawnAdapter(ChildEvent), self) {
case (subj, child) ⇒
subj ! Watch(child, self)
child
}.expectMessageKeep(expectTimeout) { (msg, child) ⇒
msg should ===(Watched)
child ! Stop
}.expectMessage(expectTimeout) { (msg, child) ⇒
msg should ===(GotSignal(Terminated(child)(null)))
}
})
def `11 must watch a child actor after its termination`(): Unit = sync(setup("ctx11") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(None, ctx.spawnAdapter(ChildEvent), self).keep {
case (subj, child) ⇒
ctx.watch(child)
child ! Stop
}.expectTermination(expectTimeout) {
case (t, (subj, child)) ⇒
t should ===(Terminated(child)(null))
subj ! Watch(child, blackhole)
child
}.expectMessage(expectTimeout) { (msg, child) ⇒
msg should ===(GotSignal(Terminated(child)(null)))
}
})
def `12 must unwatch a child actor before its termination`(): Unit = sync(setup("ctx12") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(None, ctx.spawnAdapter(ChildEvent), self).keep {
case (subj, child) ⇒
subj ! Watch(child, self)
}.expectMessageKeep(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(Watched)
subj ! Unwatch(child, self)
}.expectMessage(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(Unwatched)
ctx.watch(child)
child ! Stop
child
}.expectTermination(expectTimeout) { (t, child) ⇒
t should ===(Terminated(child)(null))
}
})
def `13 must terminate upon not handling Terminated`(): Unit = sync(setup("ctx13") { (ctx, startWith) ⇒
val self = ctx.self
startWith.mkChild(None, ctx.spawnAdapter(ChildEvent), self).keep {
case (subj, child) ⇒
muteExpectedException[DeathPactException]()
subj ! Watch(child, self)
}.expectMessageKeep(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(Watched)
subj ! BecomeCareless(self)
}.expectMessageKeep(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(BecameCareless)
child ! Stop
}.expectMessage(expectTimeout) {
case (msg, (subj, child)) ⇒
msg should ===(GotSignal(PostStop))
}
})
def `20 must return the right context info`(): Unit = sync(setup("ctx20") { (ctx, startWith) ⇒
startWith.keep(_ ! GetInfo(ctx.self))
.expectMessage(expectTimeout) {
case (msg: Info, subj) ⇒
msg.self should ===(subj)
msg.system should ===(system)
case (other, _) ⇒
fail(s"$other was not an Info(...)")
}
})
def `21 must return right info about children`(): Unit = sync(setup("ctx21") { (ctx, startWith) ⇒
val self = ctx.self
startWith
.mkChild(Some("B"), ctx.spawnAdapter(ChildEvent), self)
.stimulate(_._1 ! GetChild("A", self), _ ⇒ Child(None))
.stimulate(_._1 ! GetChild("B", self), x ⇒ Child(Some(x._2)))
.stimulate(_._1 ! GetChildren(self), x ⇒ Children(Set(x._2)))
})
def `30 must set small receive timeout`(): Unit = sync(setup("ctx30") { (ctx, startWith) ⇒
val self = ctx.self
startWith
.stimulate(_ ! SetTimeout(1.nano, self), _ ⇒ TimeoutSet)
.expectMessage(expectTimeout) { (msg, _) ⇒
msg should ===(GotReceiveTimeout)
}
})
def `31 must set large receive timeout`(): Unit = sync(setup("ctx31") { (ctx, startWith) ⇒
val self = ctx.self
startWith
.stimulate(_ ! SetTimeout(1.minute, self), _ ⇒ TimeoutSet)
.stimulate(_ ⇒ ctx.schedule(1.second, self, Pong2), _ ⇒ Pong2)
.stimulate(_ ! Ping(self), _ ⇒ Pong1)
})
def `32 must schedule a message`(): Unit = sync(setup("ctx32") { (ctx, startWith) ⇒
startWith(_ ! Schedule(1.nano, ctx.self, Pong2, ctx.self))
.expectMultipleMessages(expectTimeout, 2) { (msgs, _) ⇒
msgs should ===(Scheduled :: Pong2 :: Nil)
}
})
def `40 must create a working adapter`(): Unit = sync(setup("ctx40") { (ctx, startWith) ⇒
startWith.keep { subj ⇒
subj ! GetAdapter(ctx.self)
}.expectMessage(expectTimeout) { (msg, subj) ⇒
val Adapter(adapter) = msg
ctx.watch(adapter)
adapter ! Ping(ctx.self)
(subj, adapter)
}.expectMessage(expectTimeout) {
case (msg, (subj, adapter)) ⇒
msg should ===(Pong1)
ctx.stop(subj)
adapter
}.expectMulti(expectTimeout, 2) { (msgs, adapter) ⇒
msgs.toSet should ===(Set(Left(Terminated(adapter)(null)), Right(GotSignal(PostStop))))
}
})
def `41 must create a named adapter`(): Unit = sync(setup("ctx41") { (ctx, startWith) ⇒
startWith.keep { subj ⇒
subj ! GetAdapter(ctx.self, "named")
}.expectMessage(expectTimeout) { (msg, subj) ⇒
val Adapter(adapter) = msg
adapter.path.name should include("named")
}
})
}
trait Normal extends Tests {
override def suite = "normal"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
subject(ctx.self)
}
object `An ActorContext (native)` extends Normal with NativeSystem
object `An ActorContext (adapted)` extends Normal with AdaptedSystem
trait Widened extends Tests {
import Actor._
override def suite = "widened"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
subject(ctx.self).widen { case x ⇒ x }
}
object `An ActorContext with widened Behavior (native)` extends Widened with NativeSystem
object `An ActorContext with widened Behavior (adapted)` extends Widened with AdaptedSystem
trait Deferred extends Tests {
override def suite = "deferred"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
Actor.Deferred(_ ⇒ subject(ctx.self))
}
object `An ActorContext with deferred Behavior (native)` extends Deferred with NativeSystem
object `An ActorContext with deferred Behavior (adapted)` extends Deferred with AdaptedSystem
trait Tap extends Tests {
override def suite = "tap"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
Actor.Tap((_, _) ⇒ (), (_, _) ⇒ (), subject(ctx.self))
}
object `An ActorContext with Tap (old-native)` extends Tap with NativeSystem
object `An ActorContext with Tap (old-adapted)` extends Tap with AdaptedSystem
trait NormalOld extends Tests {
override def suite = "basic"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
oldSubject(ctx.self)
}
object `An ActorContext (old-native)` extends NormalOld with NativeSystem
object `An ActorContext (old-adapted)` extends NormalOld with AdaptedSystem
trait WidenedOld extends Tests {
import ScalaDSL._
override def suite = "widened"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
oldSubject(ctx.self).widen { case x ⇒ x }
}
object `An ActorContext with widened Behavior (old-native)` extends WidenedOld with NativeSystem
object `An ActorContext with widened Behavior (old-adapted)` extends WidenedOld with AdaptedSystem
trait SynchronousSelfOld extends Tests {
override def suite = "synchronous"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] = ScalaDSL.SynchronousSelf(self ⇒ oldSubject(ctx.self))
}
object `An ActorContext with SynchronousSelf (old-native)` extends SynchronousSelfOld with NativeSystem
object `An ActorContext with SynchronousSelf (old-adapted)` extends SynchronousSelfOld with AdaptedSystem
trait NonMatchingTapOld extends Tests {
override def suite = "TapNonMatch"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] = ScalaDSL.Tap({ case null ⇒ }, oldSubject(ctx.self))
}
object `An ActorContext with non-matching Tap (old-native)` extends NonMatchingTapOld with NativeSystem
object `An ActorContext with non-matching Tap (old-adapted)` extends NonMatchingTapOld with AdaptedSystem
trait MatchingTapOld extends Tests {
override def suite = "TapMatch"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] = ScalaDSL.Tap({ case _ ⇒ }, oldSubject(ctx.self))
}
object `An ActorContext with matching Tap (old-native)` extends MatchingTapOld with NativeSystem
object `An ActorContext with matching Tap (old-adapted)` extends MatchingTapOld with AdaptedSystem
private val stoppingBehavior = ScalaDSL.Full[Command] { case ScalaDSL.Msg(_, Stop) ⇒ ScalaDSL.Stopped }
trait AndLeftOld extends Tests {
override def suite = "andLeft"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
ScalaDSL.And(oldSubject(ctx.self), stoppingBehavior)
}
object `An ActorContext with And (left, native)` extends AndLeftOld with NativeSystem
object `An ActorContext with And (left, adapted)` extends AndLeftOld with AdaptedSystem
trait AndRightOld extends Tests {
override def suite = "andRight"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
ScalaDSL.And(stoppingBehavior, oldSubject(ctx.self))
}
object `An ActorContext with And (right, native)` extends AndRightOld with NativeSystem
object `An ActorContext with And (right, adapted)` extends AndRightOld with AdaptedSystem
trait OrLeftOld extends Tests {
override def suite = "orLeft"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
ScalaDSL.Or(oldSubject(ctx.self), stoppingBehavior)
override def stop(ref: ActorRef[Command]) = {
ref ! Stop
ref ! Stop
}
}
object `An ActorContext with Or (left, native)` extends OrLeftOld with NativeSystem
object `An ActorContext with Or (left, adapted)` extends OrLeftOld with AdaptedSystem
trait OrRightOld extends Tests {
override def suite = "orRight"
override def behavior(ctx: scaladsl.ActorContext[Event]): Behavior[Command] =
ScalaDSL.Or(stoppingBehavior, oldSubject(ctx.self))
override def stop(ref: ActorRef[Command]) = {
ref ! Stop
ref ! Stop
}
}
object `An ActorContext with Or (right, native)` extends OrRightOld with NativeSystem
object `An ActorContext with Or (right, adapted)` extends OrRightOld with AdaptedSystem
}