Skip to content

Commit

Permalink
Implemented graceful shutdown (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Jul 25, 2024
1 parent 541bab1 commit b8f0510
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 23 additions & 0 deletions Nixie.Tests/TestShutdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,27 @@ public async Task TestSpawnFireAndForgetSlowActorAndGracefulShutdownShortDelayBy
IActorRef<ShutdownSlowActor, string>? actor2 = asx.Get<ShutdownSlowActor, string>("my-actor");
Assert.Null(actor2);
}

[Fact]
public async Task TestSpawnFireAndForgetSlowActorAndGracefulShutdownShortDelayByName2()
{
using ActorSystem asx = new();

IActorRef<ShutdownSlowActor, string> actor = asx.Spawn<ShutdownSlowActor, string>("my-actor");

Assert.IsAssignableFrom<ShutdownSlowActor>(actor.Runner.Actor);

actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2");
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2");
actor.Send("TestSpawnFireAndForgetActorAndShutdownByName2");

Assert.False(await asx.GracefulShutdown<ShutdownSlowActor, string>("my-actor", TimeSpan.FromMilliseconds(2000)));

await asx.Wait();

Assert.Equal(2, ((ShutdownSlowActor)actor.Runner.Actor!).GetMessages());

IActorRef<ShutdownSlowActor, string>? actor2 = asx.Get<ShutdownSlowActor, string>("my-actor");
Assert.Null(actor2);
}
}
8 changes: 3 additions & 5 deletions Nixie/ActorRepositoryReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,9 @@ public async Task<bool> GracefulShutdown(string name, TimeSpan maxWait)

if (actors.TryGetValue(name, out Lazy<(ActorRunner<TActor, TRequest, TResponse> runner, ActorRef<TActor, TRequest, TResponse> actorRef)>? actor))
{
if (await actor.Value.runner.GracefulShutdown(maxWait))
{
actors.TryRemove(name, out _);
return true;
}
bool success = await actor.Value.runner.GracefulShutdown(maxWait);
actors.TryRemove(name, out _);
return success;
}

return true;
Expand Down

0 comments on commit b8f0510

Please sign in to comment.