Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WatchAysnc methods for monitoring actor lifecycles outside of Akka.NET #6102

Merged
merged 7 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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) { }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a new extension method for working with IActorRef

}
public class static WrappedMessage
{
public static object Unwrap(object message) { }
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabled this spec as I went back and addressed this behavior

[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
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy path use case test.

{
// 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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idempotent use case - watching an already dead actor.

{
// 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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative case - cancellation.

{
// 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();
}
}
}
Loading