From 91bf88c1dbfd7efbd4a389d3348bf3a5f0c034b5 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Sat, 12 Mar 2022 00:48:20 +0700 Subject: [PATCH] Convert Akka.Persistence.TestKit.Tests to async --- .../Actors/CounterActor.cs | 4 +- .../Bug4762FixSpec.cs | 16 ++--- .../JournalInterceptorsSpecs.cs | 28 +++++---- .../SnapshotStoreInterceptorsSpec.cs | 28 +++++---- .../TestJournalSpec.cs | 60 +++++++++---------- .../TestSnapshotStoreSpec.cs | 34 +++++------ 6 files changed, 91 insertions(+), 79 deletions(-) diff --git a/src/core/Akka.Persistence.TestKit.Tests/Actors/CounterActor.cs b/src/core/Akka.Persistence.TestKit.Tests/Actors/CounterActor.cs index 8d7e3724b3f..494f1b47106 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/Actors/CounterActor.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/Actors/CounterActor.cs @@ -77,13 +77,13 @@ await WithJournalWrite(write => write.Fail(), async () => Watch(actor); actor.Tell("inc", TestActor); - ExpectMsg(TimeSpan.FromSeconds(3)); + await ExpectMsgAsync(TimeSpan.FromSeconds(3)); // need to restart actor actor = ActorOf(counterProps, "counter1"); actor.Tell("read", TestActor); - var value = ExpectMsg(TimeSpan.FromSeconds(3)); + var value = await ExpectMsgAsync(TimeSpan.FromSeconds(3)); value.ShouldBe(0); }); } diff --git a/src/core/Akka.Persistence.TestKit.Tests/Bug4762FixSpec.cs b/src/core/Akka.Persistence.TestKit.Tests/Bug4762FixSpec.cs index 02d9f1fbbda..dab23cd7719 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/Bug4762FixSpec.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/Bug4762FixSpec.cs @@ -21,13 +21,13 @@ namespace Akka.Persistence.TestKit.Tests /// public class Bug4762FixSpec : PersistenceTestKit { - class WriteMessage + private class WriteMessage { } - class TestEvent + private class TestEvent { } - class TestActor2 : UntypedPersistentActor + private class TestActor2 : UntypedPersistentActor { private readonly IActorRef _probe; private readonly ILoggingAdapter _log; @@ -68,7 +68,7 @@ protected override void OnRecover(object message) public async Task TestJournal_PersistAll_should_only_count_each_event_exceptions_once() { var probe = CreateTestProbe(); - await WithJournalWrite(write => write.Pass(), () => + await WithJournalWrite(write => write.Pass(), async () => { var actor = ActorOf(() => new TestActor2(probe)); Watch(actor); @@ -76,10 +76,10 @@ await WithJournalWrite(write => write.Pass(), () => var command = new WriteMessage(); actor.Tell(command, actor); - probe.ExpectMsg(); - probe.ExpectMsg(); - probe.ExpectMsg(); - probe.ExpectNoMsg(3000); + await probe.ExpectMsgAsync(); + await probe.ExpectMsgAsync(); + await probe.ExpectMsgAsync(); + await probe.ExpectNoMsgAsync(3000); }); } } diff --git a/src/core/Akka.Persistence.TestKit.Tests/JournalInterceptorsSpecs.cs b/src/core/Akka.Persistence.TestKit.Tests/JournalInterceptorsSpecs.cs index 90131770ea9..a1e6c263b52 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/JournalInterceptorsSpecs.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/JournalInterceptorsSpecs.cs @@ -12,31 +12,35 @@ namespace Akka.Persistence.TestKit.Tests using Akka.Persistence.TestKit; using FluentAssertions; using Xunit; + using static FluentAssertions.FluentActions; public class JournalInterceptorsSpecs { [Fact] - public void noop_immediately_returns_without_exception() + public async Task noop_immediately_returns_without_exception() { - JournalInterceptors.Noop.Instance - .Awaiting(x => x.InterceptAsync(null)) - .Should().NotThrow(); + await Awaiting(async () => + { + await JournalInterceptors.Noop.Instance.InterceptAsync(null); + }).Should().NotThrowAsync(); } [Fact] - public void failure_must_throw_specific_exception() + public async Task failure_must_throw_specific_exception() { - JournalInterceptors.Failure.Instance - .Awaiting(x => x.InterceptAsync(null)) - .Should().ThrowExactly(); + await Awaiting(async () => + { + await JournalInterceptors.Failure.Instance.InterceptAsync(null); + }).Should().ThrowExactlyAsync(); } [Fact] - public void rejection_must_throw_specific_exception() + public async Task rejection_must_throw_specific_exception() { - JournalInterceptors.Rejection.Instance - .Awaiting(x => x.InterceptAsync(null)) - .Should().ThrowExactly(); + await Awaiting(async () => + { + await JournalInterceptors.Rejection.Instance.InterceptAsync(null); + }).Should().ThrowExactlyAsync(); } [Fact] diff --git a/src/core/Akka.Persistence.TestKit.Tests/SnapshotStoreInterceptorsSpec.cs b/src/core/Akka.Persistence.TestKit.Tests/SnapshotStoreInterceptorsSpec.cs index abce8d55286..f3b8df781c4 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/SnapshotStoreInterceptorsSpec.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/SnapshotStoreInterceptorsSpec.cs @@ -11,25 +11,33 @@ namespace Akka.Persistence.TestKit.Tests using System.Threading.Tasks; using FluentAssertions; using Xunit; + using static FluentAssertions.FluentActions; public class SnapshotStoreInterceptorsSpec { [Fact] - public void noop_must_do_nothing() - => SnapshotStoreInterceptors.Noop.Instance - .Awaiting(x => x.InterceptAsync(null, null)) - .Should().NotThrow(); + public async Task noop_must_do_nothing() + { + await Awaiting(async () => + { + await SnapshotStoreInterceptors.Noop.Instance.InterceptAsync(null, null); + }).Should().NotThrowAsync(); + } [Fact] - public void failure_must_always_throw_exception() - => SnapshotStoreInterceptors.Failure.Instance - .Awaiting(x => x.InterceptAsync(null, null)) - .Should().ThrowExactly(); + public async Task failure_must_always_throw_exception() + { + await Awaiting(async () => + { + await SnapshotStoreInterceptors.Failure.Instance.InterceptAsync(null, null); + }).Should().ThrowExactlyAsync(); + } [Fact] public async Task delay_must_call_next_interceptor_after_specified_delay() { - var duration = TimeSpan.FromMilliseconds(100); + var duration = TimeSpan.FromMilliseconds(200); + var epsilon = TimeSpan.FromMilliseconds(50); var probe = new InterceptorProbe(); var delay = new SnapshotStoreInterceptors.Delay(duration, probe); @@ -37,7 +45,7 @@ public async Task delay_must_call_next_interceptor_after_specified_delay() await delay.InterceptAsync(null, null); probe.WasCalled.Should().BeTrue(); - probe.CalledAt.Should().BeOnOrAfter(startedAt + duration); + probe.CalledAt.Should().BeOnOrAfter(startedAt + duration - epsilon); } [Fact] diff --git a/src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs b/src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs index b32b1fa0286..f66189c7c1d 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs @@ -30,23 +30,23 @@ public TestJournalSpec() : base(DefaultTimeoutConfig) private readonly TestProbe _probe; [Fact] - public void must_return_ack_after_new_write_interceptor_is_set() + public async Task must_return_ack_after_new_write_interceptor_is_set() { JournalActorRef.Tell(new TestJournal.UseWriteInterceptor(null), TestActor); - ExpectMsg(TimeSpan.FromSeconds(3)); + await ExpectMsgAsync(TimeSpan.FromSeconds(3)); } [Fact] public async Task works_as_memory_journal_by_default() { var actor = ActorOf(() => new PersistActor(_probe)); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await Journal.OnWrite.Pass(); actor.Tell(new PersistActor.WriteMessage("write"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); } [Fact] @@ -54,21 +54,21 @@ public async Task must_recover_restarted_actor() { var actor = ActorOf(() => new PersistActor(_probe)); Watch(actor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await Journal.OnRecovery.Pass(); actor.Tell(new PersistActor.WriteMessage("1"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); actor.Tell(new PersistActor.WriteMessage("2"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); await actor.GracefulStop(TimeSpan.FromSeconds(1)); - ExpectTerminated(actor); + await ExpectTerminatedAsync(actor); ActorOf(() => new PersistActor(_probe)); - _probe.ExpectMsg("1"); - _probe.ExpectMsg("2"); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync("1"); + await _probe.ExpectMsgAsync("2"); + await _probe.ExpectMsgAsync(); } [Fact] @@ -76,13 +76,13 @@ public async Task when_fail_on_write_is_set_all_writes_to_journal_will_fail() { var actor = ActorOf(() => new PersistActor(_probe)); Watch(actor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await Journal.OnWrite.Fail(); actor.Tell(new PersistActor.WriteMessage("write"), TestActor); - _probe.ExpectMsg("failure"); - ExpectTerminated(actor); + await _probe.ExpectMsgAsync("failure"); + await ExpectTerminatedAsync(actor); } [Fact] @@ -90,24 +90,24 @@ public async Task must_recover_failed_actor() { var actor = ActorOf(() => new PersistActor(_probe)); Watch(actor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await Journal.OnRecovery.Pass(); actor.Tell(new PersistActor.WriteMessage("1"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); actor.Tell(new PersistActor.WriteMessage("2"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); await Journal.OnWrite.Fail(); actor.Tell(new PersistActor.WriteMessage("3"), TestActor); - _probe.ExpectMsg("failure"); - ExpectTerminated(actor); + await _probe.ExpectMsgAsync("failure"); + await ExpectTerminatedAsync(actor); ActorOf(() => new PersistActor(_probe)); - _probe.ExpectMsg("1"); - _probe.ExpectMsg("2"); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync("1"); + await _probe.ExpectMsgAsync("2"); + await _probe.ExpectMsgAsync(); } [Fact] @@ -115,34 +115,34 @@ public async Task when_reject_on_write_is_set_all_writes_to_journal_will_be_reje { var actor = ActorOf(() => new PersistActor(_probe)); Watch(actor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await Journal.OnWrite.Reject(); actor.Tell(new PersistActor.WriteMessage("write"), TestActor); - _probe.ExpectMsg("rejected"); + await _probe.ExpectMsgAsync("rejected"); } [Fact] public async Task journal_must_reset_state_to_pass() { - await WithJournalWrite(write => write.Fail(), () => + await WithJournalWrite(write => write.Fail(), async () => { var actor = ActorOf(() => new PersistActor(_probe)); Watch(actor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); actor.Tell(new PersistActor.WriteMessage("write"), TestActor); - _probe.ExpectMsg("failure"); - ExpectTerminated(actor); + await _probe.ExpectMsgAsync("failure"); + await ExpectTerminatedAsync(actor); }); var actor2 = ActorOf(() => new PersistActor(_probe)); Watch(actor2); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); actor2.Tell(new PersistActor.WriteMessage("write"), TestActor); - _probe.ExpectMsg("ack"); + await _probe.ExpectMsgAsync("ack"); } } } diff --git a/src/core/Akka.Persistence.TestKit.Tests/TestSnapshotStoreSpec.cs b/src/core/Akka.Persistence.TestKit.Tests/TestSnapshotStoreSpec.cs index 37497714232..84c1777684d 100644 --- a/src/core/Akka.Persistence.TestKit.Tests/TestSnapshotStoreSpec.cs +++ b/src/core/Akka.Persistence.TestKit.Tests/TestSnapshotStoreSpec.cs @@ -23,24 +23,24 @@ public TestSnapshotStoreSpec() private readonly TestProbe _probe; [Fact] - public void send_ack_after_load_interceptor_is_set() + public async Task send_ack_after_load_interceptor_is_set() { SnapshotsActorRef.Tell(new TestSnapshotStore.UseLoadInterceptor(null), TestActor); - ExpectMsg(); + await ExpectMsgAsync(); } [Fact] - public void send_ack_after_save_interceptor_is_set() + public async Task send_ack_after_save_interceptor_is_set() { SnapshotsActorRef.Tell(new TestSnapshotStore.UseSaveInterceptor(null), TestActor); - ExpectMsg(); + await ExpectMsgAsync(); } [Fact] - public void send_ack_after_delete_interceptor_is_set() + public async Task send_ack_after_delete_interceptor_is_set() { SnapshotsActorRef.Tell(new TestSnapshotStore.UseDeleteInterceptor(null), TestActor); - ExpectMsg(); + await ExpectMsgAsync(); } [Fact] @@ -49,17 +49,17 @@ public async Task after_load_behavior_was_executed_store_is_back_to_pass_mode() // create snapshot var actor = ActorOf(() => new SnapshotActor(_probe)); actor.Tell("save"); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); await actor.GracefulStop(TimeSpan.FromSeconds(3)); - await WithSnapshotLoad(load => load.Fail(), () => + await WithSnapshotLoad(load => load.Fail(), async () => { ActorOf(() => new SnapshotActor(_probe)); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); }); ActorOf(() => new SnapshotActor(_probe)); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); } [Fact] @@ -68,14 +68,14 @@ public async Task after_save_behavior_was_executed_store_is_back_to_pass_mode() // create snapshot var actor = ActorOf(() => new SnapshotActor(_probe)); - await WithSnapshotSave(save => save.Fail(), () => + await WithSnapshotSave(save => save.Fail(), async () => { actor.Tell("save"); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); }); actor.Tell("save"); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); } [Fact] @@ -85,17 +85,17 @@ public async Task after_delete_behavior_was_executed_store_is_back_to_pass_mode( var actor = ActorOf(() => new SnapshotActor(_probe)); actor.Tell("save"); - var success = _probe.ExpectMsg(); + var success = await _probe.ExpectMsgAsync(); var nr = success.Metadata.SequenceNr; - await WithSnapshotDelete(del => del.Fail(), () => + await WithSnapshotDelete(del => del.Fail(), async () => { actor.Tell(new SnapshotActor.DeleteOne(nr), TestActor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); }); actor.Tell(new SnapshotActor.DeleteOne(nr), TestActor); - _probe.ExpectMsg(); + await _probe.ExpectMsgAsync(); } } }