Skip to content

Commit 96645a5

Browse files
yanpitanguiAarononthewebArkatufus
authored
Make InternalTestActor override its SupervisionStrategy (#7221)
* Make InternalTestActor override its SupervisionStrategy * Fix broken SupervisorStrategy handling --------- Co-authored-by: Aaron Stannard <[email protected]> Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent 7e85cac commit 96645a5

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/core/Akka.TestKit.Tests/TestActorRefTests/TestProbeSpec.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Threading.Tasks;
1011
using Akka.Actor;
1112
using Akka.TestKit.TestActors;
1213
using Akka.Util.Internal;
@@ -89,6 +90,17 @@ public void TestProbe_restart_a_failing_child_if_the_given_supervisor_says_so()
8990
});
9091
}
9192

93+
[Fact]
94+
public async Task TestProbe_kill_a_failing_child_if_the_given_supervisor_says_so()
95+
{
96+
var restarts = new AtomicCounter(0);
97+
var probe = CreateTestProbe();
98+
var child = await probe.ChildActorOfAsync(Props.Create(() => new FailingActor(restarts)), new FailOnExceptionStrategy());
99+
await WatchAsync(child);
100+
child.Tell("hello");
101+
await ExpectTerminatedAsync(child);
102+
}
103+
92104
class FailingActor : ActorBase
93105
{
94106
private AtomicCounter Restarts { get; }
@@ -108,5 +120,13 @@ protected override void PostRestart(Exception reason)
108120
Restarts.IncrementAndGet();
109121
}
110122
}
123+
124+
private class FailOnExceptionStrategy: OneForOneStrategy
125+
{
126+
protected override Directive Handle(IActorRef child, Exception exception)
127+
{
128+
return Directive.Stop;
129+
}
130+
}
111131
}
112132
}

src/core/Akka.TestKit/DelegatingSupervisorStrategy.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Reflection;
1011
using Akka.Actor;
1112
using Akka.Actor.Internal;
1213
using Akka.Util;
@@ -21,7 +22,15 @@ public class DelegatingSupervisorStrategy : SupervisorStrategy
2122

2223
protected override Directive Handle(IActorRef child, Exception exception)
2324
{
24-
throw new NotImplementedException();
25+
var childDelegate = Delegates[child];
26+
var handleMethod = typeof(SupervisorStrategy).GetMethod(
27+
name: "Handle",
28+
bindingAttr: BindingFlags.Instance | BindingFlags.NonPublic,
29+
binder: Type.DefaultBinder,
30+
types: new[] {typeof(IActorRef), typeof(Exception)},
31+
modifiers: null);
32+
var result = (Directive) handleMethod.Invoke(childDelegate, new object[]{ child, exception });
33+
return result;
2534
}
2635

2736
public override void ProcessFailure(IActorContext context, bool restart, IActorRef child, Exception cause, ChildRestartStats stats,

src/core/Akka.TestKit/Internal/InternalTestActor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,7 @@ protected override void OnReceive(object message)
102102
}
103103
}
104104
}
105+
106+
protected override SupervisorStrategy SupervisorStrategy() => _supervisorStrategy;
105107
}
106108
}

0 commit comments

Comments
 (0)