Skip to content
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
99 changes: 42 additions & 57 deletions src/core/Akka.Remote.Tests/RemotingTerminatorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
//-----------------------------------------------------------------------

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

Expand All @@ -29,54 +32,53 @@ public class RemotingTerminatorSpecs : AkkaSpec
}
");

private ActorSystem _sys2;

public RemotingTerminatorSpecs(ITestOutputHelper output) : base(RemoteConfig, output) { }

protected override async Task AfterAllAsync()
{
await base.AfterAllAsync();
if (_sys2 != null)
await ShutdownAsync(_sys2);
}

[Fact]
public void RemotingTerminator_should_shutdown_promptly_with_no_associations()
public async Task RemotingTerminator_should_shutdown_promptly_with_no_associations()
{
Within(TimeSpan.FromSeconds(10), () =>
await WithinAsync(TimeSpan.FromSeconds(10), async () =>
{
Sys.EventStream.Subscribe(TestActor, typeof (RemotingShutdownEvent));
Sys.EventStream.Subscribe(TestActor, typeof (RemotingErrorEvent));
var terminationTask = Sys.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
Assert.True(await Sys.Terminate().AwaitWithTimeout(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
});
}

[Fact]
public void RemotingTerminator_should_shutdown_promptly_with_some_associations()
public async Task RemotingTerminator_should_shutdown_promptly_with_some_associations()
{
var sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(sys2).Provider.DefaultAddress;
_sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(_sys2).Provider.DefaultAddress;

// open an association
var associated =
Sys.ActorSelection(new RootActorPath(sys2Address)/"system"/"remote-watcher")
.ResolveOne(TimeSpan.FromSeconds(4))
.Result;
var associated = await Sys.ActorSelection(new RootActorPath(sys2Address)/"system"/"remote-watcher")
.ResolveOne(TimeSpan.FromSeconds(4));

Within(TimeSpan.FromSeconds(10), () =>
{
Sys.EventStream.Subscribe(TestActor, typeof(RemotingShutdownEvent));
Sys.EventStream.Subscribe(TestActor, typeof(RemotingErrorEvent));
var terminationTask = Sys.Terminate();
ExpectMsg<RemotingShutdownEvent>();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
});
Sys.EventStream.Subscribe(TestActor, typeof(RemotingShutdownEvent));
Sys.EventStream.Subscribe(TestActor, typeof(RemotingErrorEvent));
var terminationTask = Sys.Terminate();
await ExpectMsgAsync<RemotingShutdownEvent>();
Assert.True(await terminationTask.AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");

// now terminate the second system
Within(TimeSpan.FromSeconds(10), () =>
{
var terminationTask = sys2.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
});
Assert.True(await _sys2.Terminate().AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");
}

[Fact]
public void RemotingTerminator_should_shutdown_properly_with_remotely_deployed_actor()
public async Task RemotingTerminator_should_shutdown_properly_with_remotely_deployed_actor()
{
var sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(sys2).Provider.DefaultAddress;
_sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(_sys2).Provider.DefaultAddress;

// open an association via remote deployment
var associated =
Expand All @@ -87,31 +89,23 @@ public void RemotingTerminator_should_shutdown_properly_with_remotely_deployed_a
// verify that the association is open (don't terminate until handshake is finished)
associated.Ask<ActorIdentity>(new Identify("foo"), RemainingOrDefault).Result.MessageId.ShouldBe("foo");


// terminate the DEPLOYED system
Within(TimeSpan.FromSeconds(20), () =>
{
var terminationTask = sys2.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");

ExpectTerminated(associated); // expect that the remote deployed actor is dead
});
Assert.True(await _sys2.Terminate().AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");
await ExpectTerminatedAsync(associated); // expect that the remote deployed actor is dead

// now terminate the DEPLOYER system
Within(TimeSpan.FromSeconds(10), () =>
{
var terminationTask = Sys.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
});
Assert.True(await Sys.Terminate().AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");
}

[Fact]
public void RemotingTerminator_should_shutdown_properly_without_exception_logging_while_graceful_shutdown()
public async Task RemotingTerminator_should_shutdown_properly_without_exception_logging_while_graceful_shutdown()
{
EventFilter.Exception<ShutDownAssociation>().Expect(0,
() =>
await EventFilter.Exception<ShutDownAssociation>().ExpectAsync(0,
async () =>
{
var sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(sys2).Provider.DefaultAddress;
_sys2 = ActorSystem.Create("System2", RemoteConfig);
var sys2Address = RARP.For(_sys2).Provider.DefaultAddress;

// open an association via remote deployment
var associated = Sys.ActorOf(BlackHoleActor.Props.WithDeploy(Deploy.None.WithScope(new RemoteScope(sys2Address))), "remote");
Expand All @@ -122,20 +116,11 @@ public void RemotingTerminator_should_shutdown_properly_without_exception_loggin
associated.Ask<ActorIdentity>(new Identify("foo"), RemainingOrDefault).Result.MessageId.ShouldBe("foo");

// terminate the DEPLOYED system
Within(TimeSpan.FromSeconds(10), () =>
{
var terminationTask = sys2.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");

ExpectTerminated(associated); // expect that the remote deployed actor is dead
});

Assert.True(await _sys2.Terminate().AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");
await ExpectTerminatedAsync(associated); // expect that the remote deployed actor is dead

// now terminate the DEPLOYER system
Within(TimeSpan.FromSeconds(10), () =>
{
var terminationTask = Sys.Terminate();
Assert.True(terminationTask.Wait(RemainingOrDefault), "Expected to terminate within 10 seconds, but didn't.");
});
Assert.True(await Sys.Terminate().AwaitWithTimeout(10.Seconds()), "Expected to terminate within 10 seconds, but didn't.");
});
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/Akka.TestKit/TestKitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,10 @@ protected virtual async Task ShutdownAsync(
var wasShutdownDuringWait = await system.Terminate().AwaitWithTimeout(durationValue, cancellationToken);
if(!wasShutdownDuringWait)
{
const string msg = "Failed to stop [{0}] within [{1}] \n{2}";
// Forcefully close the ActorSystem to make sure we exit the test cleanly
((ExtendedActorSystem) system).Guardian.Stop();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forcefully shut down the ActorSystem to attempt to exit the unit test cleanly


const string msg = "Failed to stop [{0}] within [{1}]. ActorSystem is being forcefully shut down.\n{2}";
if(verifySystemShutdown)
throw new TimeoutException(string.Format(msg, system.Name, durationValue, ((ExtendedActorSystem) system).PrintTree()));
system.Log.Warning(msg, system.Name, durationValue, ((ExtendedActorSystem) system).PrintTree());
Expand Down