Skip to content

Commit

Permalink
Make InternalTestActor override its SupervisionStrategy (#7221)
Browse files Browse the repository at this point in the history
* Make InternalTestActor override its SupervisionStrategy

* Fix broken SupervisorStrategy handling

---------

Co-authored-by: Aaron Stannard <[email protected]>
Co-authored-by: Gregorius Soedharmo <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent 7e85cac commit 96645a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/core/Akka.TestKit.Tests/TestActorRefTests/TestProbeSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit.TestActors;
using Akka.Util.Internal;
Expand Down Expand Up @@ -89,6 +90,17 @@ public void TestProbe_restart_a_failing_child_if_the_given_supervisor_says_so()
});
}

[Fact]
public async Task TestProbe_kill_a_failing_child_if_the_given_supervisor_says_so()
{
var restarts = new AtomicCounter(0);
var probe = CreateTestProbe();
var child = await probe.ChildActorOfAsync(Props.Create(() => new FailingActor(restarts)), new FailOnExceptionStrategy());
await WatchAsync(child);
child.Tell("hello");
await ExpectTerminatedAsync(child);
}

class FailingActor : ActorBase
{
private AtomicCounter Restarts { get; }
Expand All @@ -108,5 +120,13 @@ protected override void PostRestart(Exception reason)
Restarts.IncrementAndGet();
}
}

private class FailOnExceptionStrategy: OneForOneStrategy
{
protected override Directive Handle(IActorRef child, Exception exception)
{
return Directive.Stop;
}
}
}
}
11 changes: 10 additions & 1 deletion src/core/Akka.TestKit/DelegatingSupervisorStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using Akka.Actor;
using Akka.Actor.Internal;
using Akka.Util;
Expand All @@ -21,7 +22,15 @@ public class DelegatingSupervisorStrategy : SupervisorStrategy

protected override Directive Handle(IActorRef child, Exception exception)
{
throw new NotImplementedException();
var childDelegate = Delegates[child];
var handleMethod = typeof(SupervisorStrategy).GetMethod(
name: "Handle",
bindingAttr: BindingFlags.Instance | BindingFlags.NonPublic,
binder: Type.DefaultBinder,
types: new[] {typeof(IActorRef), typeof(Exception)},
modifiers: null);
var result = (Directive) handleMethod.Invoke(childDelegate, new object[]{ child, exception });
return result;
}

public override void ProcessFailure(IActorContext context, bool restart, IActorRef child, Exception cause, ChildRestartStats stats,
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka.TestKit/Internal/InternalTestActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ protected override void OnReceive(object message)
}
}
}

protected override SupervisorStrategy SupervisorStrategy() => _supervisorStrategy;
}
}

0 comments on commit 96645a5

Please sign in to comment.