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

Convert Xunit2.TestKit and AkkaSpec from IDisposable to IAsyncLifetime #5865

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 @@ -96,10 +96,11 @@ public CoordinatedShutdownShardingSpec(ITestOutputHelper helper) : base(SpecConf
});
}

protected override void BeforeTermination()
protected override async Task AfterAllAsync()
Copy link
Member

Choose a reason for hiding this comment

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

LGTM

{
Shutdown(_sys1);
Shutdown(_sys2);
await base.AfterAllAsync();
await ShutdownAsync(_sys1);
await ShutdownAsync(_sys2);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ await AwaitAssertAsync(() =>
}
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
await base.AfterAllAsync();
foreach (var s in _systems)
Shutdown(s);
await ShutdownAsync(s);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,14 @@ public void Restarting_cluster_node_during_hand_over_must_restart_singletons_in_
});
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
Shutdown(_sys1);
Shutdown(_sys2);
Shutdown(_sys3);
await base.AfterAllAsync();
await ShutdownAsync(_sys1);
await ShutdownAsync(_sys2);
await ShutdownAsync(_sys3);
if (_sys4 != null)
Shutdown(_sys4);
await ShutdownAsync(_sys4);
}

public class Singleton : ReceiveActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Cluster.Tools.Singleton;
using Akka.Configuration;
Expand Down Expand Up @@ -128,12 +129,13 @@ public void Restarting_cluster_node_with_same_hostname_and_port_must_handover_to
});
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
Shutdown(_sys1);
Shutdown(_sys2);
await base.AfterAllAsync();
await ShutdownAsync(_sys1);
await ShutdownAsync(_sys2);
if(_sys3 != null)
Shutdown(_sys3);
await ShutdownAsync(_sys3);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,12 @@ await AwaitAssertAsync(async () =>
});
}


protected override void BeforeTermination()
protected override async Task AfterAllAsync()
{
Shutdown(_sys1);
Shutdown(_sys2);
Shutdown(_sys3);
await base.AfterAllAsync();
await ShutdownAsync(_sys1);
await ShutdownAsync(_sys2);
await ShutdownAsync(_sys3);
GC.Collect();
}

Expand Down
49 changes: 10 additions & 39 deletions src/contrib/testkits/Akka.TestKit.Xunit2/TestKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Setup;
using Akka.Configuration;
using Akka.Event;
using Akka.TestKit.Xunit2.Internals;
using Xunit;
using Xunit.Abstractions;

namespace Akka.TestKit.Xunit2
Expand All @@ -19,10 +21,8 @@ namespace Akka.TestKit.Xunit2
/// This class represents an Akka.NET TestKit that uses <a href="https://xunit.github.io/">xUnit</a>
/// as its testing framework.
/// </summary>
public class TestKit : TestKitBase , IDisposable
public class TestKit : TestKitBase, IAsyncLifetime
{
private bool _isDisposed; //Automatically initialized to false;

/// <summary>
/// The provider used to write test output.
/// </summary>
Expand Down Expand Up @@ -104,14 +104,13 @@ public TestKit(string config, ITestOutputHelper output = null)
/// This method is called when a test ends.
///
/// <remarks>
/// If you override this, then make sure you either call base.AfterTest() or
/// <see cref="TestKitBase.Shutdown(System.Nullable{System.TimeSpan},bool)">TestKitBase.Shutdown</see>
/// If you override this, then make sure you either call base.AfterAllAsync()
/// to shut down the system. Otherwise a memory leak will occur.
/// </remarks>
/// </summary>
protected virtual void AfterAll()
protected virtual async Task AfterAllAsync()
{
Shutdown();
await ShutdownAsync();
}

/// <summary>
Expand All @@ -128,42 +127,14 @@ protected void InitializeLogger(ActorSystem system)
}
}


public void Dispose()
public virtual Task InitializeAsync()
{
Dispose(true);
//Take this object off the finalization queue and prevent finalization code for this object
//from executing a second time.
GC.SuppressFinalize(this);
return Task.CompletedTask;
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <param name="disposing">
/// if set to <c>true</c> the method has been called directly or indirectly by a user's code.
/// Managed and unmanaged resources will be disposed.<br /> if set to <c>false</c> the method
/// has been called by the runtime from inside the finalizer and only unmanaged resources can
/// be disposed.
/// </param>
protected virtual void Dispose(bool disposing)
public virtual async Task DisposeAsync()
{
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.

try
{
//Make sure Dispose does not get called more than once, by checking the disposed field
if(!_isDisposed && disposing)
{
AfterAll();
}
_isDisposed = true;
}
finally
{
}
await AfterAllAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ public ActorRefIgnoreSerializationSpec(ITestOutputHelper output)
system2 = ActorSystem.Create("sys2", Config);
}


protected override void AfterAll()
protected override async Task AfterAllAsync()
{
base.AfterAll();
system1.Terminate();
system2.Terminate();
await base.AfterAllAsync();
await ShutdownAsync(system1);
await ShutdownAsync(system2);
}

[Fact]
Expand Down
11 changes: 6 additions & 5 deletions src/core/Akka.Cluster.Tests/ShutdownAfterJoinSeedNodesSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.TestKit;
Expand Down Expand Up @@ -37,12 +38,12 @@ public ShutdownAfterJoinSeedNodesSpec() : base(Config)
_ordinary1 = ActorSystem.Create(Sys.Name, Sys.Settings.Config);
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
base.AfterTermination();
Shutdown(_seed1);
Shutdown(_seed2);
Shutdown(_ordinary1);
await base.AfterAllAsync();
await ShutdownAsync(_seed1);
await ShutdownAsync(_seed2);
await ShutdownAsync(_ordinary1);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Persistence.Query;
Expand Down Expand Up @@ -203,10 +204,10 @@ private IActorRef SetupEmpty(string persistenceId)
return Sys.ActorOf(Query.TestActor.Props(persistenceId));
}

protected override void Dispose(bool disposing)
public override Task DisposeAsync()
{
Materializer.Dispose();
base.Dispose(disposing);
return base.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Persistence.Query;
Expand Down Expand Up @@ -105,10 +106,10 @@ private IActorRef Setup(string persistenceId, int n)
return pref;
}

protected override void Dispose(bool disposing)
public override Task DisposeAsync()
{
Materializer.Dispose();
base.Dispose(disposing);
return base.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Persistence.Query;
Expand Down Expand Up @@ -126,10 +127,10 @@ private IActorRef SetupEmpty(string persistenceId)
return Sys.ActorOf(Query.TestActor.Props(persistenceId));
}

protected override void Dispose(bool disposing)
public override Task DisposeAsync()
{
Materializer.Dispose();
base.Dispose(disposing);
return base.DisposeAsync();
}
}
}
5 changes: 2 additions & 3 deletions src/core/Akka.Persistence.TCK/Query/PersistenceIdsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ protected IActorRef WriteSnapshot(string persistenceId, int n)
return pref;
}


protected override void Dispose(bool disposing)
public override Task DisposeAsync()
{
Materializer.Dispose();
base.Dispose(disposing);
return base.DisposeAsync();
}
}
}
5 changes: 3 additions & 2 deletions src/core/Akka.Persistence.Tests/PersistenceSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Akka.Configuration;
using Akka.TestKit;
using Akka.Util.Internal;
Expand Down Expand Up @@ -61,9 +62,9 @@ protected PersistenceSpec(Config config = null, ITestOutputHelper output = null)
public string NamePrefix { get { return Sys.Name; } }
public string Name { get { return _name; } }

protected override void AfterAll()
protected override async Task AfterAllAsync()
{
base.AfterAll();
await base.AfterAllAsync();
Clean.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.TestKit;
Expand Down Expand Up @@ -176,10 +177,10 @@ private Address Address(ActorSystem system)
return ((ExtendedActorSystem) system).Provider.DefaultAddress;
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
_remoteSystem.Terminate().Wait(TimeSpan.FromSeconds(2));
base.AfterTermination();
await base.AfterAllAsync();
await ShutdownAsync(_remoteSystem);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System.IO;
using System.Threading.Tasks;
using Akka.Actor;
using Xunit;

Expand Down Expand Up @@ -66,10 +67,10 @@ protected override void AtStartup()
using (_file.Create()) {}
}

protected override void AfterTermination()
protected override async Task AfterTerminationAsync()
{
_file.Delete();
base.AfterTermination();
await base.AfterTerminationAsync();
}

[Fact]
Expand Down
9 changes: 6 additions & 3 deletions src/core/Akka.Remote.Tests/RemoteDeathWatchSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Actor.Dsl;
using Akka.Configuration;
Expand Down Expand Up @@ -44,15 +45,17 @@ public RemoteDeathWatchSpec() : base(_config)
ConfigurationFactory.ParseString(@"akka.remote.dot-netty.tcp.port=2666").WithFallback(_config));
}

protected override void BeforeTermination()
protected override Task BeforeTerminationAsync()
{
var mute = EventFilter.Warning(pattern: new Regex("received dead letter.*Disassociate")).Mute();
Sys.EventStream.Publish(mute);
return Task.CompletedTask;
}

protected override void AfterTermination()
protected override async Task AfterAllAsync()
{
_other.Terminate().Wait(TimeSpan.FromSeconds(20));
await base.AfterAllAsync();
await ShutdownAsync(_other, verifySystemShutdown: true);
}

[Fact]
Expand Down
Loading