@@ -164,11 +164,9 @@ class StubbedSupervisionSpec extends AnyWordSpec with Matchers with LogCapturing
164164 inbox.receiveMessage() should === (State (1 , Map .empty))
165165 }
166166
167- " support nesting to handle different exceptions " in {
167+ def testNestedSupervision [ T ]( supervisedBehavior : Behavior [ Command ] => Behavior [ Command ]) : Unit = {
168168 val inbox = TestInbox [Event ](" evt" )
169- val behv = supervise(targetBehavior(inbox.ref))
170- .onFailure[Exc2 ](SupervisorStrategy .resume)
171- .onFailure[Exc3 ](SupervisorStrategy .restart)
169+ val behv = supervisedBehavior(targetBehavior(inbox.ref))
172170 val testkit = BehaviorTestKit (behv)
173171 testkit.run(IncrementState )
174172 testkit.run(GetState )
@@ -192,6 +190,14 @@ class StubbedSupervisionSpec extends AnyWordSpec with Matchers with LogCapturing
192190 inbox.receiveMessage() should === (ReceivedSignal (PostStop ))
193191 }
194192
193+ " support nesting to handle different exceptions" in testNestedSupervision { behv =>
194+ supervise(supervise(behv).onFailure[Exc2 ](SupervisorStrategy .resume)).onFailure[Exc3 ](SupervisorStrategy .restart)
195+ }
196+
197+ " flatten support nesting to handle different exceptions" in testNestedSupervision { behv =>
198+ supervise(behv).whenFailure[Exc2 ](SupervisorStrategy .resume).whenFailure[Exc3 ](SupervisorStrategy .restart)
199+ }
200+
195201 " not catch fatal error" in {
196202 val inbox = TestInbox [Event ]()
197203 val behv = Behaviors .supervise(targetBehavior(inbox.ref)).onFailure[Throwable ](SupervisorStrategy .restart)
@@ -397,11 +403,9 @@ class SupervisionSpec extends ScalaTestWithActorTestKit("""
397403 }
398404 }
399405
400- " support nesting exceptions with different strategies " in {
406+ def testNestedSupervision [ T ]( supervisedBehavior : Behavior [ Command ] => Behavior [ Command ]) : Unit = {
401407 val probe = TestProbe [Event ](" evt" )
402- val behv =
403- supervise(supervise(targetBehavior(probe.ref)).onFailure[RuntimeException ](SupervisorStrategy .stop))
404- .onFailure[Exception ](SupervisorStrategy .restart)
408+ val behv = supervisedBehavior(targetBehavior(probe.ref))
405409
406410 val ref = spawn(behv)
407411
@@ -416,13 +420,21 @@ class SupervisionSpec extends ScalaTestWithActorTestKit("""
416420 }
417421 }
418422
419- " support nesting exceptions with outer restart and inner backoff strategies" in {
423+ " support nesting exceptions with different strategies" in testNestedSupervision { behv =>
424+ supervise(supervise(behv).onFailure[RuntimeException ](SupervisorStrategy .stop))
425+ .onFailure[Exception ](SupervisorStrategy .restart)
426+ }
427+
428+ " flatten support nesting exceptions with different strategies" in testNestedSupervision { behv =>
429+ supervise(behv)
430+ .whenFailure[RuntimeException ](SupervisorStrategy .stop)
431+ .whenFailure[Exception ](SupervisorStrategy .restart)
432+ }
433+
434+ def testNestedSupervisionWithRestartThenBackoff [T ](
435+ supervisedBehavior : Behavior [Command ] => Behavior [Command ]): Unit = {
420436 val probe = TestProbe [Event ](" evt" )
421- val behv =
422- supervise(
423- supervise(targetBehavior(probe.ref))
424- .onFailure[IllegalArgumentException ](SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 )))
425- .onFailure[IOException ](SupervisorStrategy .restart)
437+ val behv = supervisedBehavior(targetBehavior(probe.ref))
426438
427439 val ref = spawn(behv)
428440
@@ -444,11 +456,25 @@ class SupervisionSpec extends ScalaTestWithActorTestKit("""
444456 probe.expectMessage(Pong (2 ))
445457 }
446458
447- " support nesting exceptions with inner restart and outer backoff strategies" in {
459+ " support nesting exceptions with outer restart and inner backoff strategies" in testNestedSupervisionWithRestartThenBackoff {
460+ behv =>
461+ supervise(
462+ supervise(behv).onFailure[IllegalArgumentException ](
463+ SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 )))
464+ .onFailure[IOException ](SupervisorStrategy .restart)
465+ }
466+
467+ " flatten support nesting exceptions with outer restart and inner backoff strategies" in testNestedSupervisionWithRestartThenBackoff {
468+ behv =>
469+ supervise(behv)
470+ .whenFailure[IllegalArgumentException ](SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 ))
471+ .whenFailure[IOException ](SupervisorStrategy .restart)
472+ }
473+
474+ def testNestedSupervisionWithBackoffThenRestart [T ](
475+ supervisedBehavior : Behavior [Command ] => Behavior [Command ]): Unit = {
448476 val probe = TestProbe [Event ](" evt" )
449- val behv =
450- supervise(supervise(targetBehavior(probe.ref)).onFailure[IllegalArgumentException ](SupervisorStrategy .restart))
451- .onFailure[IOException ](SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 ))
477+ val behv = supervisedBehavior(targetBehavior(probe.ref))
452478
453479 val ref = spawn(behv)
454480
@@ -470,6 +496,19 @@ class SupervisionSpec extends ScalaTestWithActorTestKit("""
470496 probe.expectMessage(Pong (2 ))
471497 }
472498
499+ " support nesting exceptions with inner restart and outer backoff strategies" in testNestedSupervisionWithBackoffThenRestart {
500+ behv =>
501+ supervise(supervise(behv).onFailure[IllegalArgumentException ](SupervisorStrategy .restart))
502+ .onFailure[IOException ](SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 ))
503+ }
504+
505+ " flatten support nesting exceptions with inner restart and outer backoff strategies" in testNestedSupervisionWithBackoffThenRestart {
506+ behv =>
507+ supervise(behv)
508+ .whenFailure[IllegalArgumentException ](SupervisorStrategy .restart)
509+ .whenFailure[IOException ](SupervisorStrategy .restartWithBackoff(10 .millis, 10 .millis, 0.0 ))
510+ }
511+
473512 " stop when not supervised" in {
474513 val probe = TestProbe [Event ](" evt" )
475514 val behv = targetBehavior(probe.ref)
0 commit comments