From 8c79622b60a580c6d2334c27c6d36a3c0be27092 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Wed, 16 Mar 2022 09:03:40 +0700 Subject: [PATCH] Convert Akka.Persistence.TestKit.Tests to async (#5718) * Convert Akka.Persistence.TestKit.Tests to async Co-authored-by: Aaron Stannard --- build-system/azure-pipeline.template.yaml | 6 +- build-system/pr-validation.yaml | 20 +++---- build.fsx | 12 ++-- .../Actors/CounterActor.cs | 4 +- .../Bug4762FixSpec.cs | 16 ++--- .../JournalInterceptorsSpecs.cs | 28 +++++---- .../SnapshotStoreInterceptorsSpec.cs | 28 +++++---- .../TestJournalSpec.cs | 60 +++++++++---------- .../TestSnapshotStoreSpec.cs | 34 +++++------ 9 files changed, 110 insertions(+), 98 deletions(-) diff --git a/build-system/azure-pipeline.template.yaml b/build-system/azure-pipeline.template.yaml index 4d0083a0d0a..9ea34aab820 100644 --- a/build-system/azure-pipeline.template.yaml +++ b/build-system/azure-pipeline.template.yaml @@ -19,9 +19,9 @@ jobs: vmImage: ${{ parameters.vmImage }} steps: - task: UseDotNet@2 - displayName: 'Use .NET 6 SDK 6.0.100' + displayName: 'Use .NET 6 SDK 6.0.201' inputs: - version: 6.0.100 + version: 6.0.201 - task: UseDotNet@2 displayName: 'Use .NET Core Runtime 3.1.10' inputs: @@ -52,7 +52,7 @@ jobs: displayName: 'Copy Build Output' inputs: sourceFolder: ${{ parameters.outputDirectory }} - contents: '**\*' + contents: '**' targetFolder: $(Build.ArtifactStagingDirectory) continueOnError: boolean # 'true' if future steps should run even if this step fails; defaults to 'false' - task: PublishBuildArtifacts@1 diff --git a/build-system/pr-validation.yaml b/build-system/pr-validation.yaml index 41b9a77bb34..f3570d9efa5 100644 --- a/build-system/pr-validation.yaml +++ b/build-system/pr-validation.yaml @@ -124,16 +124,16 @@ jobs: outputDirectory: "TestResults" artifactName: "net_core_tests_linux-$(Build.BuildId)" - - template: azure-pipeline.template.yaml - parameters: - name: "docfx_test" - displayName: "DocFX warning check" - vmImage: "windows-2019" - scriptFileName: build.cmd - scriptArgs: docfx - outputDirectory: "TestResults" - artifactName: "docfx_test-$(Build.BuildId)" - run_if: eq(variables['Build.Reason'], 'PullRequest') +# - template: azure-pipeline.template.yaml +# parameters: +# name: "docfx_test" +# displayName: "DocFX warning check" +# vmImage: "windows-2019" +# scriptFileName: build.cmd +# scriptArgs: docfx +# outputDirectory: "TestResults" +# artifactName: "docfx_test-$(Build.BuildId)" +# run_if: eq(variables['Build.Reason'], 'PullRequest') - template: azure-pipeline.template.yaml parameters: diff --git a/build.fsx b/build.fsx index 6339b45a386..c41acb7fe98 100644 --- a/build.fsx +++ b/build.fsx @@ -250,8 +250,8 @@ Target "RunTests" (fun _ -> let runSingleProject project = let arguments = match (hasTeamCity) with - | true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetFrameworkVersion outputTests) - | false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetFrameworkVersion outputTests) + | true -> (sprintf "test -c Release --blame --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetFrameworkVersion outputTests) + | false -> (sprintf "test -c Release --blame --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetFrameworkVersion outputTests) let result = ExecProcess(fun info -> info.FileName <- "dotnet" @@ -280,8 +280,8 @@ Target "RunTestsNetCore" (fun _ -> let runSingleProject project = let arguments = match (hasTeamCity) with - | true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetCoreVersion outputTests) - | false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetCoreVersion outputTests) + | true -> (sprintf "test -c Release --blame --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetCoreVersion outputTests) + | false -> (sprintf "test -c Release --blame --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetCoreVersion outputTests) let result = ExecProcess(fun info -> info.FileName <- "dotnet" @@ -310,8 +310,8 @@ Target "RunTestsNet" (fun _ -> let runSingleProject project = let arguments = match (hasTeamCity) with - | true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetVersion outputTests) - | false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetVersion outputTests) + | true -> (sprintf "test -c Release --blame-crash --blame-hang-timeout 25m --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none -teamcity" testNetVersion outputTests) + | false -> (sprintf "test -c Release --blame-crash --blame-hang-timeout 25m --no-build --logger:trx --logger:\"console;verbosity=normal\" --framework %s --results-directory \"%s\" -- -parallel none" testNetVersion outputTests) let result = ExecProcess(fun info -> info.FileName <- "dotnet" 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 04db2e29152..bde4e2473b3 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 Assert.ThrowsAsync(async () => + { + await JournalInterceptors.Failure.Instance.InterceptAsync(null); + }); } [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 Assert.ThrowsAsync(async () => + { + await JournalInterceptors.Rejection.Instance.InterceptAsync(null); + }); } [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(); } } }