-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to shutdown actors by name/ref
- Loading branch information
1 parent
fc795df
commit 5c0300c
Showing
10 changed files
with
468 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
| ||
namespace Nixie.Tests.Actors; | ||
|
||
public class ShutdownActor : IActor<string> | ||
{ | ||
private int receivedMessages; | ||
|
||
public ShutdownActor(IActorContext<ShutdownActor, string> _) | ||
{ | ||
|
||
} | ||
|
||
public int GetMessages() | ||
{ | ||
return receivedMessages; | ||
} | ||
|
||
public void IncrMessage() | ||
{ | ||
receivedMessages++; | ||
} | ||
|
||
public async Task Receive(string message) | ||
{ | ||
await Task.Yield(); | ||
|
||
IncrMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
| ||
namespace Nixie.Tests.Actors; | ||
|
||
public class ShutdownInsideActor : IActor<string> | ||
{ | ||
private int receivedMessages; | ||
|
||
private readonly IActorContext<ShutdownInsideActor, string> context; | ||
|
||
public ShutdownInsideActor(IActorContext<ShutdownInsideActor, string> context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
public int GetMessages() | ||
{ | ||
return receivedMessages; | ||
} | ||
|
||
public void IncrMessage() | ||
{ | ||
receivedMessages++; | ||
} | ||
|
||
public async Task Receive(string message) | ||
{ | ||
await Task.Yield(); | ||
|
||
if (message == "shutdown") | ||
context.ActorSystem.Shutdown(context.Self); | ||
else | ||
IncrMessage(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
| ||
using Nixie.Tests.Actors; | ||
|
||
namespace Nixie.Tests; | ||
|
||
public sealed class TestShutdown | ||
{ | ||
[Fact] | ||
public async Task TestSpawnFireAndForgetActorAndShutdownByName() | ||
{ | ||
using ActorSystem asx = new(); | ||
|
||
IActorRef<ShutdownActor, string> actor = asx.Spawn<ShutdownActor, string>("my-actor"); | ||
|
||
Assert.IsAssignableFrom<ShutdownActor>(actor.Runner.Actor); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.True(asx.Shutdown<ShutdownActor, string>("my-actor")); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(1, ((ShutdownActor)actor.Runner.Actor!).GetMessages()); | ||
|
||
IActorRef<ShutdownActor, string>? actor2 = asx.Get<ShutdownActor, string>("my-actor"); | ||
Assert.Null(actor2); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSpawnFireAndForgetActorAndShutdownByName2() | ||
{ | ||
using ActorSystem asx = new(); | ||
|
||
IActorRef<ShutdownActor, string> actor = asx.Spawn<ShutdownActor, string>("my-actor"); | ||
|
||
Assert.IsAssignableFrom<ShutdownActor>(actor.Runner.Actor); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.True(asx.Shutdown<ShutdownActor, string>("my-actor")); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(3, ((ShutdownActor)actor.Runner.Actor!).GetMessages()); | ||
|
||
IActorRef<ShutdownActor, string>? actor2 = asx.Get<ShutdownActor, string>("my-actor"); | ||
Assert.Null(actor2); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSpawnFireAndForgetActorAndShutdownByRef() | ||
{ | ||
using ActorSystem asx = new(); | ||
|
||
IActorRef<ShutdownActor, string> actor = asx.Spawn<ShutdownActor, string>("my-actor"); | ||
|
||
Assert.IsAssignableFrom<ShutdownActor>(actor.Runner.Actor); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.True(asx.Shutdown(actor)); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(1, ((ShutdownActor)actor.Runner.Actor!).GetMessages()); | ||
|
||
IActorRef<ShutdownActor, string>? actor2 = asx.Get<ShutdownActor, string>("my-actor"); | ||
Assert.Null(actor2); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSpawnFireAndForgetActorAndShutdownByRef2() | ||
{ | ||
using ActorSystem asx = new(); | ||
|
||
IActorRef<ShutdownActor, string> actor = asx.Spawn<ShutdownActor, string>("my-actor"); | ||
|
||
Assert.IsAssignableFrom<ShutdownActor>(actor.Runner.Actor); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.True(asx.Shutdown(actor)); | ||
|
||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
actor.Send("TestSpawnFireAndForgetActorAndShutdownByRef2"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(3, ((ShutdownActor)actor.Runner.Actor!).GetMessages()); | ||
|
||
IActorRef<ShutdownActor, string>? actor2 = asx.Get<ShutdownActor, string>("my-actor"); | ||
Assert.Null(actor2); | ||
} | ||
|
||
[Fact] | ||
public async Task TestSpawnActorAndShutdownInside() | ||
{ | ||
using ActorSystem asx = new(); | ||
|
||
IActorRef<ShutdownInsideActor, string> actor = asx.Spawn<ShutdownInsideActor, string>("my-actor"); | ||
|
||
Assert.IsAssignableFrom<ShutdownInsideActor>(actor.Runner.Actor); | ||
|
||
actor.Send("TestSpawnActorAndShutdownInside"); | ||
|
||
await asx.Wait(); | ||
|
||
actor.Send("shutdown"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(1, ((ShutdownInsideActor)actor.Runner.Actor!).GetMessages()); | ||
|
||
IActorRef<ShutdownInsideActor, string>? actor2 = asx.Get<ShutdownInsideActor, string>("my-actor"); | ||
Assert.Null(actor2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.