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

Port Akka.Tests.Actor.Scheduler tests to async/await - 2 #5756

Merged
merged 4 commits into from
Mar 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/core/Akka.Tests/Actor/Scheduler/SchedulerShutdownSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Dispatch;
using Akka.Event;
using Akka.TestKit;
using Akka.Tests.Util;
using Akka.Util.Internal;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -82,26 +83,25 @@ public ShutdownScheduler(Config scheduler, ILoggingAdapter log) : base(scheduler
public static readonly Config Config = ConfigurationFactory.ParseString("akka.scheduler.implementation = \""+ typeof(ShutdownScheduler).AssemblyQualifiedName + "\"");

[Fact]
public void ActorSystem_must_terminate_scheduler_on_shutdown()
public async Task ActorSystem_must_terminate_scheduler_on_shutdown()
{
ActorSystem sys = null;
try
{
sys = ActorSystem.Create("SchedulerShutdownSys1", Config);
var scheduler = (ShutdownScheduler)sys.Scheduler;
var currentCounter = scheduler.Shutdown.Current;
sys.Terminate().Wait(sys.Settings.SchedulerShutdownTimeout).Should().BeTrue();
var nextCounter = scheduler.Shutdown.Current;
nextCounter.Should().Be(currentCounter + 1);
(await sys.Terminate().AwaitWithTimeout(sys.Settings.SchedulerShutdownTimeout)).Should().BeTrue();
(scheduler.Shutdown.Current).Should().Be(currentCounter + 1);
}
finally
{
sys?.Terminate().Wait(TimeSpan.FromSeconds(5));
await sys?.Terminate().AwaitWithTimeout(TimeSpan.FromSeconds(5));
}
}

[Fact]
public void ActorSystem_must_terminate_scheduler_with_queued_work_on_shutdown()
public async Task ActorSystem_must_terminate_scheduler_with_queued_work_on_shutdown()
{
ActorSystem sys = null;
try
Expand All @@ -110,28 +110,27 @@ public void ActorSystem_must_terminate_scheduler_with_queued_work_on_shutdown()
sys = ActorSystem.Create("SchedulerShutdownSys1", Config);
var scheduler = (ShutdownScheduler)sys.Scheduler;
sys.Scheduler.Advanced.ScheduleRepeatedly(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(10), () => i++);
Task.Delay(100).Wait(); // give the scheduler a chance to start and run
await Task.Delay(100); // give the scheduler a chance to start and run
var currentCounter = scheduler.Shutdown.Current;
sys.Terminate().Wait(sys.Settings.SchedulerShutdownTimeout).Should().BeTrue();
var nextCounter = scheduler.Shutdown.Current;
nextCounter.Should().Be(currentCounter + 1);
(await sys.Terminate().AwaitWithTimeout(sys.Settings.SchedulerShutdownTimeout)).Should().BeTrue();
(scheduler.Shutdown.Current).Should().Be(currentCounter + 1);
var stoppedValue = i;
stoppedValue.Should().BeGreaterThan(0, "should have incremented at least once");
Task.Delay(100).Wait();
await Task.Delay(100);
i.Should().Be(stoppedValue, "Scheduler shutdown; should not still be incrementing values.");
}
finally
{
sys?.Terminate().Wait(TimeSpan.FromSeconds(5));
sys?.Terminate().AwaitWithTimeout(TimeSpan.FromSeconds(5));
}
}

[Fact]
public void ActorSystem_default_scheduler_mustbe_able_to_terminate_on_shutdown()
public async Task ActorSystem_default_scheduler_mustbe_able_to_terminate_on_shutdown()
{
ActorSystem sys = ActorSystem.Create("SchedulerShutdownSys2");
Assert.True(sys.Scheduler is IDisposable);
sys.Terminate().Wait(TimeSpan.FromSeconds(5));
await sys.Terminate().AwaitWithTimeout(TimeSpan.FromSeconds(5));
}


Expand All @@ -154,15 +153,17 @@ public MyScheduledActor()
}

[Fact]
public void ActorSystem_default_scheduler_must_never_accept_more_work_after_shutdown()
public async Task ActorSystem_default_scheduler_must_never_accept_more_work_after_shutdown()
{
ActorSystem sys = ActorSystem.Create("SchedulerShutdownSys3");
var receiver = sys.ActorOf(Props.Create(() => new MyScheduledActor()));
sys.Scheduler.ScheduleTellOnce(0, receiver, "set", ActorRefs.NoSender);
Thread.Sleep(50); // let the scheduler run
Assert.True(receiver.Ask<bool>("get", TimeSpan.FromMilliseconds(100)).Result);
await Task.Delay(50); // let the scheduler run
var received = await receiver.Ask<bool>("get", TimeSpan.FromMilliseconds(100));
Assert.True(received);

if(!sys.Terminate().Wait(TimeSpan.FromSeconds(5)))
var terminated = await sys.Terminate().AwaitWithTimeout(TimeSpan.FromSeconds(5));
if (!terminated)
Assert.True(false, $"Expected ActorSystem to terminate within 5s. Took longer.");

Assert.Throws<SchedulerException>(() =>
Expand Down
15 changes: 15 additions & 0 deletions src/core/Akka.Tests/Util/TaskHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;

namespace Akka.Tests.Util
{
public static class TaskHelpers
Copy link
Member

Choose a reason for hiding this comment

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

Might not be a bad idea to move this into AkkaSpec at some point in the future

{
public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan timeout)
{
var delayed = Task.Delay(timeout);
await Task.WhenAny(delayed, parentTask);
return parentTask.IsCompleted;
}
}
}