Skip to content

Commit

Permalink
add WatchAysnc methods for monitoring actor lifecycles outside of A…
Browse files Browse the repository at this point in the history
…kka.NET (#6102)

* prototyping `WatchAysnc` methods

* cleaned up `GracefulStop` and `PromiseActorRef` internals

* implemented `WatchAsync`
  • Loading branch information
Aaronontheweb committed Feb 9, 2023
1 parent 192e9a8 commit ba142bc
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 135 deletions.
Expand Up @@ -1882,6 +1882,10 @@ namespace Akka.Actor
public Akka.Actor.IStash Stash { get; set; }
}
public delegate void UntypedReceive(object message);
public class static WatchAsyncSupport
{
public static System.Threading.Tasks.Task<bool> WatchAsync(this Akka.Actor.IActorRef target, System.Threading.CancellationToken token = null) { }
}
public class static WrappedMessage
{
public static object Unwrap(object message) { }
Expand Down
Expand Up @@ -1884,6 +1884,10 @@ namespace Akka.Actor
public Akka.Actor.IStash Stash { get; set; }
}
public delegate void UntypedReceive(object message);
public class static WatchAsyncSupport
{
public static System.Threading.Tasks.Task<bool> WatchAsync(this Akka.Actor.IActorRef target, System.Threading.CancellationToken token = null) { }
}
public class static WrappedMessage
{
public static object Unwrap(object message) { }
Expand Down
Expand Up @@ -1882,6 +1882,10 @@ namespace Akka.Actor
public Akka.Actor.IStash Stash { get; set; }
}
public delegate void UntypedReceive(object message);
public class static WatchAsyncSupport
{
public static System.Threading.Tasks.Task<bool> WatchAsync(this Akka.Actor.IActorRef target, System.Threading.CancellationToken token = null) { }
}
public class static WrappedMessage
{
public static object Unwrap(object message) { }
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Tests/Actor/GracefulStopSpecs.cs
Expand Up @@ -51,7 +51,7 @@ public async Task GracefulStopShouldReturnTrueForAlreadyDeadActor()

private class CustomShutdown{}

[Fact(DisplayName = "GracefulStop should return false if shutdown goes overtime", Skip = "GracefulStop currently throws a TaskCancellationException, which seems wrong")]
[Fact(DisplayName = "GracefulStop should return false if shutdown goes overtime")]
public async Task GracefulStopShouldThrowIfShutdownGoesOvertime()
{
// arrange
Expand Down
71 changes: 71 additions & 0 deletions src/core/Akka.Tests/Actor/WatchAsyncSpecs.cs
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------
// <copyright file="WatchAsyncSpecs.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using Akka.TestKit.TestActors;
using FluentAssertions;
using Xunit;

namespace Akka.Tests.Actor
{
public class WatchAsyncSpecs : AkkaSpec
{
[Fact(DisplayName = "WatchAsync should return true when actor is terminated")]
public async Task WatchAsync_should_return_true_when_actor_is_terminated()
{
// arrange
var actor = Sys.ActorOf(BlackHoleActor.Props);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
var terminatedTask = actor.WatchAsync(cts.Token);

// act
Sys.Stop(actor);
var terminated = await terminatedTask;

// assert
terminated.Should().BeTrue();
}

[Fact(DisplayName = "WatchAsync should return true when called on actor that is already terminated")]
public async Task WatchAsync_should_return_true_when_actor_is_already_terminated()
{
// arrange
var actor = Sys.ActorOf(BlackHoleActor.Props);
Watch(actor);
Sys.Stop(actor);
await ExpectTerminatedAsync(actor);

// act

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
var terminated = await actor.WatchAsync(cts.Token);

// assert
terminated.Should().BeTrue();
}

[Fact(DisplayName = "WatchAsync should return false when cancellation token is cancelled")]
public async Task WatchAsync_should_return_true_when_cancelled()
{
// arrange
var actor = Sys.ActorOf(BlackHoleActor.Props);
using var cts = new CancellationTokenSource();
var terminatedTask = actor.WatchAsync(cts.Token);

// act
cts.Cancel();
var terminated = await terminatedTask;

// assert
terminated.Should().BeFalse();
}
}
}

0 comments on commit ba142bc

Please sign in to comment.