Skip to content

Commit

Permalink
Convert Akka.Persistence.TestKit.Tests to async (#5718)
Browse files Browse the repository at this point in the history
* Convert Akka.Persistence.TestKit.Tests to async

Co-authored-by: Aaron Stannard <aaron@petabridge.com>
  • Loading branch information
Arkatufus and Aaronontheweb authored Mar 16, 2022
1 parent fccb23f commit 8c79622
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 98 deletions.
6 changes: 3 additions & 3 deletions build-system/azure-pipeline.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions build-system/pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ await WithJournalWrite(write => write.Fail(), async () =>
Watch(actor);
actor.Tell("inc", TestActor);
ExpectMsg<Terminated>(TimeSpan.FromSeconds(3));
await ExpectMsgAsync<Terminated>(TimeSpan.FromSeconds(3));
// need to restart actor
actor = ActorOf(counterProps, "counter1");
actor.Tell("read", TestActor);
var value = ExpectMsg<int>(TimeSpan.FromSeconds(3));
var value = await ExpectMsgAsync<int>(TimeSpan.FromSeconds(3));
value.ShouldBe(0);
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/core/Akka.Persistence.TestKit.Tests/Bug4762FixSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ namespace Akka.Persistence.TestKit.Tests
/// </summary>
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;
Expand Down Expand Up @@ -68,18 +68,18 @@ 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);
var command = new WriteMessage();
actor.Tell(command, actor);
probe.ExpectMsg<RecoveryCompleted>();
probe.ExpectMsg<Done>();
probe.ExpectMsg<Done>();
probe.ExpectNoMsg(3000);
await probe.ExpectMsgAsync<RecoveryCompleted>();
await probe.ExpectMsgAsync<Done>();
await probe.ExpectMsgAsync<Done>();
await probe.ExpectNoMsgAsync(3000);
});
}
}
Expand Down
28 changes: 16 additions & 12 deletions src/core/Akka.Persistence.TestKit.Tests/JournalInterceptorsSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestJournalFailureException>();
await Assert.ThrowsAsync<TestJournalFailureException>(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<TestJournalRejectionException>();
await Assert.ThrowsAsync<TestJournalRejectionException>(async () =>
{
await JournalInterceptors.Rejection.Instance.InterceptAsync(null);
});
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,41 @@ 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<TestSnapshotStoreFailureException>();
public async Task failure_must_always_throw_exception()
{
await Awaiting(async () =>
{
await SnapshotStoreInterceptors.Failure.Instance.InterceptAsync(null, null);
}).Should().ThrowExactlyAsync<TestSnapshotStoreFailureException>();
}

[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);

var startedAt = DateTime.Now;
await delay.InterceptAsync(null, null);

probe.WasCalled.Should().BeTrue();
probe.CalledAt.Should().BeOnOrAfter(startedAt + duration);
probe.CalledAt.Should().BeOnOrAfter(startedAt + duration - epsilon);
}

[Fact]
Expand Down
60 changes: 30 additions & 30 deletions src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,119 +30,119 @@ 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<TestJournal.Ack>(TimeSpan.FromSeconds(3));
await ExpectMsgAsync<TestJournal.Ack>(TimeSpan.FromSeconds(3));
}

[Fact]
public async Task works_as_memory_journal_by_default()
{
var actor = ActorOf(() => new PersistActor(_probe));
_probe.ExpectMsg<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();

await Journal.OnWrite.Pass();
actor.Tell(new PersistActor.WriteMessage("write"), TestActor);

_probe.ExpectMsg("ack");
await _probe.ExpectMsgAsync("ack");
}

[Fact]
public async Task must_recover_restarted_actor()
{
var actor = ActorOf(() => new PersistActor(_probe));
Watch(actor);
_probe.ExpectMsg<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();

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<RecoveryCompleted>();
await _probe.ExpectMsgAsync("1");
await _probe.ExpectMsgAsync("2");
await _probe.ExpectMsgAsync<RecoveryCompleted>();
}

[Fact]
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<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();

await Journal.OnWrite.Fail();
actor.Tell(new PersistActor.WriteMessage("write"), TestActor);

_probe.ExpectMsg("failure");
ExpectTerminated(actor);
await _probe.ExpectMsgAsync("failure");
await ExpectTerminatedAsync(actor);
}

[Fact]
public async Task must_recover_failed_actor()
{
var actor = ActorOf(() => new PersistActor(_probe));
Watch(actor);
_probe.ExpectMsg<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();

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<RecoveryCompleted>();
await _probe.ExpectMsgAsync("1");
await _probe.ExpectMsgAsync("2");
await _probe.ExpectMsgAsync<RecoveryCompleted>();
}

[Fact]
public async Task when_reject_on_write_is_set_all_writes_to_journal_will_be_rejected()
{
var actor = ActorOf(() => new PersistActor(_probe));
Watch(actor);
_probe.ExpectMsg<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();

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<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();
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<RecoveryCompleted>();
await _probe.ExpectMsgAsync<RecoveryCompleted>();
actor2.Tell(new PersistActor.WriteMessage("write"), TestActor);
_probe.ExpectMsg("ack");
await _probe.ExpectMsgAsync("ack");
}
}
}
Loading

0 comments on commit 8c79622

Please sign in to comment.