Skip to content

Commit 92021e9

Browse files
committed
avoid method signature change
1 parent 2199266 commit 92021e9

File tree

8 files changed

+96
-36
lines changed

8 files changed

+96
-36
lines changed

akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorCompile.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,15 @@ public Behavior<MyMsg> receive(TypedActorContext<MyMsg> context, MyMsg message)
173173
SupervisorStrategy strategy7 = strategy6.withResetBackoffAfter(Duration.ofSeconds(2));
174174

175175
Behavior<MyMsg> behv =
176-
Behaviors.supervise(Behaviors.<MyMsg>ignore())
177-
.onFailure(IllegalStateException.class, strategy6)
176+
Behaviors.supervise(
177+
Behaviors.supervise(Behaviors.<MyMsg>ignore())
178+
.onFailure(IllegalStateException.class, strategy6))
178179
.onFailure(RuntimeException.class, strategy1);
180+
// or using flattern API:
181+
Behavior<MyMsg> flatternBehv =
182+
Behaviors.supervise(Behaviors.<MyMsg>ignore())
183+
.whenFailure(IllegalStateException.class, strategy6)
184+
.whenFailure(RuntimeException.class, strategy1);
179185
}
180186

181187
// actor context

akka-actor-typed-tests/src/test/java/jdocs/akka/typed/supervision/SupervisionCompileOnlyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ static Behavior<String> parent2() {
117117
final ActorRef<String> child2 = ctx.spawn(child(0), "child2");
118118

119119
// supervision strategy inside the setup to not recreate children on restart
120-
return Behaviors.supervise(
121-
Behaviors.<String>receiveMessage(
120+
return Behaviors.<String>supervise(
121+
Behaviors.receiveMessage(
122122
msg -> {
123123
// message handling that might throw an exception
124124
String[] parts = msg.split(" ");

akka-actor-typed-tests/src/test/scala/akka/actor/typed/SupervisionSpec.scala

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

akka-actor-typed-tests/src/test/scala/docs/akka/typed/supervision/SupervisionCompileOnly.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ object SupervisionCompileOnly {
3434

3535
//#multiple
3636
Behaviors
37-
.supervise(behavior)
38-
.onFailure[IllegalStateException](SupervisorStrategy.restart)
37+
.supervise(Behaviors.supervise(behavior).onFailure[IllegalStateException](SupervisorStrategy.restart))
3938
.onFailure[IllegalArgumentException](SupervisorStrategy.stop)
39+
40+
// or flatten ways
41+
Behaviors
42+
.supervise(behavior)
43+
.whenFailure[IllegalStateException](SupervisorStrategy.restart)
44+
.whenFailure[IllegalArgumentException](SupervisorStrategy.stop)
4045
//#multiple
4146

4247
//#wrap

akka-actor-typed/src/main/mima-filters/2.9.6.backwards.excludes/issue-32465.excludes

Lines changed: 0 additions & 4 deletions
This file was deleted.

akka-actor-typed/src/main/scala/akka/actor/typed/Behavior.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class SuperviseBehavior[T] private[akka] (val wrapped: Behavior[T])
124124
private final val ThrowableClassTag = ClassTag(classOf[Throwable])
125125

126126
/** Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws. */
127-
def onFailure[Thr <: Throwable](strategy: SupervisorStrategy)(
127+
def whenFailure[Thr <: Throwable](strategy: SupervisorStrategy)(
128128
implicit tag: ClassTag[Thr] = ThrowableClassTag): SuperviseBehavior[T] = {
129129
val effectiveTag = if (tag == ClassTag.Nothing) ThrowableClassTag else tag
130130
new SuperviseBehavior[T](Supervisor(Behavior.validateAsInitial(wrapped), strategy)(effectiveTag))
@@ -135,8 +135,8 @@ class SuperviseBehavior[T] private[akka] (val wrapped: Behavior[T])
135135
*
136136
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
137137
*/
138-
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
139-
onFailure(strategy)(ClassTag(clazz))
138+
def whenFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
139+
whenFailure(strategy)(ClassTag(clazz))
140140

141141
private[akka] def unwrap: Behavior[T] = wrapped
142142
}

akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Behaviors.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,24 @@ object Behaviors {
254254
*
255255
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
256256
*/
257-
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
258-
new SuperviseBehavior[T](wrapped).onFailure(clazz, strategy)
257+
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): Behavior[T] =
258+
new SuperviseBehavior[T](wrapped).whenFailure(clazz, strategy).unwrap
259259

260260
/**
261261
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws.
262262
*
263263
* All non-fatal (see [[scala.util.control.NonFatal]]) exceptions types will be handled using the given strategy.
264264
*/
265265
def onFailure(strategy: SupervisorStrategy): Behavior[T] =
266-
new SuperviseBehavior[T](wrapped).onFailure(strategy)
266+
onFailure(classOf[Exception], strategy)
267+
268+
/**
269+
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws by use flatten ways.
270+
*
271+
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
272+
*/
273+
def whenFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
274+
new SuperviseBehavior[T](wrapped).whenFailure(clazz, strategy)
267275
}
268276

269277
/**

akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/Behaviors.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ object Behaviors {
220220
final class Supervise[T] private[akka] (val wrapped: Behavior[T]) extends AnyVal {
221221

222222
/** Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws. */
223-
def onFailure[Thr <: Throwable](strategy: SupervisorStrategy)(implicit tag: ClassTag[Thr]): SuperviseBehavior[T] = {
224-
new SuperviseBehavior[T](wrapped).onFailure(strategy)(tag)
223+
def onFailure[Thr <: Throwable](strategy: SupervisorStrategy)(implicit tag: ClassTag[Thr]): Behavior[T] = {
224+
whenFailure(strategy)(tag).unwrap
225+
}
226+
227+
/** Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws by use flatten ways. */
228+
def whenFailure[Thr <: Throwable](strategy: SupervisorStrategy)(
229+
implicit tag: ClassTag[Thr]): SuperviseBehavior[T] = {
230+
new SuperviseBehavior[T](wrapped).whenFailure(strategy)(tag)
225231
}
226232
}
227233

0 commit comments

Comments
 (0)