Skip to content

Commit

Permalink
Port Akka.Tests.Actor tests to async/await - TimerSpec (#5787)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
  • Loading branch information
eaba and Arkatufus authored Mar 30, 2022
1 parent 121ceb9 commit 613e7d5
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions src/core/Akka.Tests/Actor/TimerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,148 +43,148 @@ public AbstractTimerSpec()
}

[Fact]
public void Must_schedule_non_repeated_ticks()
public async Task Must_schedule_non_repeated_ticks()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, TimeSpan.FromMilliseconds(10), false));
var actor = Sys.ActorOf(TargetProps(probe.Ref, TimeSpan.FromMilliseconds(10), false));

probe.ExpectMsg(new Tock(1));
probe.ExpectNoMsg(100);
await probe.ExpectMsgAsync(new Tock(1));
await probe.ExpectNoMsgAsync(100);

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_schedule_repeated_ticks()
public async Task Must_schedule_repeated_ticks()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

probe.Within(TimeSpan.FromSeconds(interval * 4) - TimeSpan.FromMilliseconds(100), () =>
await probe.WithinAsync(TimeSpan.FromSeconds(interval * 4) - TimeSpan.FromMilliseconds(100), async() =>
{
probe.ExpectMsg(new Tock(1));
probe.ExpectMsg(new Tock(1));
probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));
});

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_replace_timer()
public async Task Must_replace_timer()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));

var latch = this.CreateTestLatch(1);
var latch = CreateTestLatch(1);
// next Tock(1) enqueued in mailboxed, but should be discarded because of new timer
actor.Tell(new SlowThenBump(latch));
probe.ExpectNoMsg(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
await probe.ExpectNoMsgAsync(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
latch.CountDown();
probe.ExpectMsg(new Tock(2));
await probe.ExpectMsgAsync(new Tock(2));

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_cancel_timer()
public async Task Must_cancel_timer()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));

actor.Tell(Cancel.Instance);
probe.ExpectNoMsg(dilatedInterval + TimeSpan.FromMilliseconds(100));
await probe.ExpectNoMsgAsync(dilatedInterval + TimeSpan.FromMilliseconds(100));

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_cancel_timers_when_restarted()
public async Task Must_cancel_timers_when_restarted()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

actor.Tell(new Throw(new Exc()));
probe.ExpectMsg(new GotPreRestart(false));
await probe.ExpectMsgAsync(new GotPreRestart(false));

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_discard_timers_from_old_incarnation_after_restart_alt_1()
public async Task Must_discard_timers_from_old_incarnation_after_restart_alt_1()
{
var probe = CreateTestProbe();
var startCounter = new AtomicCounter(0);
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true, () => startCounter.IncrementAndGet()));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true, () => startCounter.IncrementAndGet()));

probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));

var latch = this.CreateTestLatch(1);
var latch = CreateTestLatch(1);
// next Tock(1) is enqueued in mailbox, but should be discarded by new incarnation
actor.Tell(new SlowThenThrow(latch, new Exc()));
probe.ExpectNoMsg(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
await probe.ExpectNoMsgAsync(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
latch.CountDown();
probe.ExpectMsg(new GotPreRestart(false));
probe.ExpectNoMsg(TimeSpan.FromSeconds(interval / 2));
probe.ExpectMsg(new Tock(2)); // this is from the startCounter increment
await probe.ExpectMsgAsync(new GotPreRestart(false));
await probe.ExpectNoMsgAsync(TimeSpan.FromSeconds(interval / 2));
await probe.ExpectMsgAsync(new Tock(2)); // this is from the startCounter increment

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_discard_timers_from_old_incarnation_after_restart_alt_2()
public async Task Must_discard_timers_from_old_incarnation_after_restart_alt_2()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new Tock(1));
// change state so that we see that the restart starts over again
actor.Tell(Bump.Instance);

probe.ExpectMsg(new Tock(2));
await probe.ExpectMsgAsync(new Tock(2));

var latch = this.CreateTestLatch(1);
var latch = CreateTestLatch(1);
// next Tock(2) is enqueued in mailbox, but should be discarded by new incarnation
actor.Tell(new SlowThenThrow(latch, new Exc()));
probe.ExpectNoMsg(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
await probe.ExpectNoMsgAsync(TimeSpan.FromSeconds(interval) + TimeSpan.FromMilliseconds(100));
latch.CountDown();
probe.ExpectMsg(new GotPreRestart(false));
probe.ExpectMsg(new Tock(1));
await probe.ExpectMsgAsync(new GotPreRestart(false));
await probe.ExpectMsgAsync(new Tock(1));

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_cancel_timers_when_stopped()
public async Task Must_cancel_timers_when_stopped()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));
var actor = Sys.ActorOf(TargetProps(probe.Ref, dilatedInterval, true));

actor.Tell(End.Instance);
probe.ExpectMsg(new GotPostStop(false));
await probe.ExpectMsgAsync(new GotPostStop(false));
}

[Fact]
public void Must_handle_AutoReceivedMessages_automatically()
public async Task Must_handle_AutoReceivedMessages_automatically()
{
var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(TargetProps(probe.Ref, TimeSpan.FromMilliseconds(10), false));
var actor = Sys.ActorOf(TargetProps(probe.Ref, TimeSpan.FromMilliseconds(10), false));

this.Watch(actor);
Watch(actor);
actor.Tell(AutoReceive.Instance);

ExpectTerminated(actor);
await ExpectTerminatedAsync(actor);
}


Expand Down Expand Up @@ -539,14 +539,14 @@ private void InitializeFSM(int initial)
public class TimersAndStashSpec : AkkaSpec
{
[Fact]
public void Timers_combined_with_stashing_should_work()
public async Task Timers_combined_with_stashing_should_work()
{

var probe = CreateTestProbe();
var actor = this.Sys.ActorOf(Props.Create(() => new ActorWithTimerAndStash(probe.Ref)));
probe.ExpectMsg("saw-scheduled");
var actor = Sys.ActorOf(Props.Create(() => new ActorWithTimerAndStash(probe.Ref)));
await probe.ExpectMsgAsync("saw-scheduled");
actor.Tell(StopStashing.Instance);
probe.ExpectMsg("scheduled");
await probe.ExpectMsgAsync("scheduled");
}

#region actors
Expand Down

0 comments on commit 613e7d5

Please sign in to comment.