-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestJournal with Write intreception and varios failure strategies
- Loading branch information
Showing
17 changed files
with
1,112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/core/Akka.Persistence.TestKit.Tests/Akka.Persistence.TestKit.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<AssemblyTitle>Akka.Persistence.TestKit.Tests</AssemblyTitle> | ||
<TargetFrameworks>$(NetFrameworkTestVersion);$(NetCoreTestVersion)</TargetFrameworks> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> | ||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> | ||
<PackageReference Include="xunit.runner.utility" Version="$(XunitVersion)" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" /> | ||
<PackageReference Include="TeamCity.ServiceMessages" Version="3.0.8" /> | ||
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Persistence.TestKit\Akka.Persistence.TestKit.csproj" /> | ||
<ProjectReference Include="..\Akka.Tests.Shared.Internals\Akka.Tests.Shared.Internals.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="test-journal.conf" /> | ||
<EmbeddedResource Include="test-journal.conf" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(TargetFramework)' == '$(NetCoreTestVersion)' "> | ||
<DefineConstants>$(DefineConstants);CORECLR</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> | ||
</PropertyGroup> | ||
</Project> |
144 changes: 144 additions & 0 deletions
144
src/core/Akka.Persistence.TestKit.Tests/TestJournalSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
namespace Akka.Persistence.TestKit.Tests | ||
{ | ||
<<<<<<< HEAD | ||
======= | ||
using System; | ||
>>>>>>> d16f7c013... foo | ||
using Actor; | ||
using Akka.Persistence.TestKit; | ||
using Akka.TestKit; | ||
using Configuration; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class TestJournalSpec : AkkaSpec | ||
{ | ||
<<<<<<< HEAD | ||
ITestJournal journal; | ||
|
||
public TestJournalSpec(ITestOutputHelper output = null) | ||
: base(GetConfig().ToString(), output) | ||
{ | ||
var ext = Persistence.Instance.Apply(Sys); | ||
var journalRef = ext.JournalFor("akka.persistence.journal.test"); | ||
journal = TestJournal.FromRef(journalRef); | ||
} | ||
|
||
[Fact] | ||
public void just_usage_samples() | ||
{ | ||
// by default journal will not fail | ||
// user is supposed to define journal behavior like that: | ||
|
||
// * to fail always | ||
journal.OnWrite.Fail(); | ||
|
||
// * to fail on specific message type | ||
journal.OnWrite.FailOnType<object>(); | ||
|
||
// * to fail on predicate | ||
journal.OnWrite.FailIf(x => x.Sender != ActorRefs.Nobody); | ||
|
||
// similar way Read from journal would be configured | ||
} | ||
|
||
static Config GetConfig() | ||
=> ConfigurationFactory.FromResource<TestJournalSpec>("Akka.Persistence.TestKit.Tests.test-journal.conf"); | ||
} | ||
======= | ||
public TestJournalSpec(ITestOutputHelper output = null) | ||
: base(GetConfig().ToString(), output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void must_return_ack_after_new_write_interceptor_is_set() | ||
{ | ||
var journalActor = GetJournalRef(Sys); | ||
|
||
journalActor.Tell(new TestJournal.UseWriteInterceptor(null), TestActor); | ||
|
||
ExpectMsg<TestJournal.Ack>(TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
[Fact] | ||
public void works_as_memory_journal_by_default() | ||
{ | ||
var journal = TestJournal.FromRef(GetJournalRef(Sys)); | ||
|
||
var actor = Sys.ActorOf<PersistActor>(); | ||
|
||
// should pass | ||
journal.OnWrite.Pass(); | ||
actor.Tell("write", TestActor); | ||
ExpectMsg("ack", TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
[Fact] | ||
public void when_fail_on_write_is_set_all_writes_to_journal_will_fail() | ||
{ | ||
var journal = TestJournal.FromRef(GetJournalRef(Sys)); | ||
var actor = Sys.ActorOf<PersistActor>(); | ||
Watch(actor); | ||
|
||
journal.OnWrite.Fail(); | ||
actor.Tell("write", TestActor); | ||
|
||
ExpectTerminated(actor, TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
[Fact] | ||
public void when_reject_on_write_is_set_all_writes_to_journal_will_be_rejected() | ||
{ | ||
var journal = TestJournal.FromRef(GetJournalRef(Sys)); | ||
var actor = Sys.ActorOf<PersistActor>(); | ||
Watch(actor); | ||
|
||
journal.OnWrite.Reject(); | ||
actor.Tell("write", TestActor); | ||
|
||
ExpectMsg("rejected", TimeSpan.FromSeconds(3)); | ||
} | ||
|
||
static IActorRef GetJournalRef(ActorSystem sys) => Persistence.Instance.Apply(sys).JournalFor(null); | ||
|
||
static Config GetConfig() | ||
=> ConfigurationFactory.FromResource<TestJournalSpec>("Akka.Persistence.TestKit.Tests.test-journal.conf"); | ||
} | ||
|
||
public class PersistActor : UntypedPersistentActor | ||
{ | ||
public override string PersistenceId => "foo"; | ||
|
||
protected override void OnCommand(object message) | ||
{ | ||
var sender = Sender; | ||
switch (message as string) | ||
{ | ||
case "write": | ||
Persist(message, _ => | ||
{ | ||
sender.Tell("ack"); | ||
}); | ||
|
||
break; | ||
|
||
default: | ||
return; | ||
} | ||
} | ||
|
||
protected override void OnRecover(object message) | ||
{ | ||
// noop | ||
} | ||
|
||
protected override void OnPersistRejected(Exception cause, object @event, long sequenceNr) | ||
{ | ||
Sender.Tell("rejected"); | ||
|
||
base.OnPersistRejected(cause, @event, sequenceNr); | ||
} | ||
} | ||
>>>>>>> d16f7c013... foo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
akka { | ||
persistence { | ||
journal { | ||
plugin = "akka.persistence.journal.test" | ||
auto-start-journals = ["akka.persistence.journal.test"] | ||
|
||
test { | ||
class = "Akka.Persistence.TestKit.TestJournal, Akka.Persistence.TestKit" | ||
plugin-dispatcher = "akka.actor.default-dispatcher" | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/core/Akka.Persistence.TestKit/Akka.Persistence.TestKit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<AssemblyTitle>Akka.Persistence.TestKit</AssemblyTitle> | ||
<Description>TestKit for writing tests for Akka.NET Persistance module.</Description> | ||
<TargetFrameworks>$(NetFrameworkLibVersion);$(NetStandardLibVersion)</TargetFrameworks> | ||
<PackageTags>$(AkkaPackageTags);testkit;persistance</PackageTags> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.6' ">1.6.1</NetStandardImplicitPackageVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Persistence\Akka.Persistence.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<!-- WORKAROUND: for some reason starting at Akka.NET 1.3.2 this package was determined as "unpackable" by default via DOTNET CLI --> | ||
<IsPackable>true</IsPackable> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Akka.Persistence.TestKit | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
internal class Delay : IJournalWriteInterceptor | ||
{ | ||
public Delay(TimeSpan delay, IJournalWriteInterceptor next) | ||
{ | ||
_delay = delay; | ||
_next = next; | ||
} | ||
|
||
private readonly TimeSpan _delay; | ||
private readonly IJournalWriteInterceptor _next; | ||
|
||
public async Task InterceptAsync(IPersistentRepresentation message) | ||
{ | ||
await Task.Delay(_delay); | ||
await _next.InterceptAsync(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="IJournalWriteBehavior.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2019 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2019 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace Akka.Persistence.TestKit | ||
{ | ||
using System.Threading.Tasks; | ||
using Akka.Persistence; | ||
|
||
internal class Failure : IJournalWriteInterceptor | ||
{ | ||
public static readonly IJournalWriteInterceptor Instance = new Failure(); | ||
|
||
public Task InterceptAsync(IPersistentRepresentation message) => throw new TestJournalWriteException(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/core/Akka.Persistence.TestKit/IJournalWriteInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Akka.Persistence.TestKit | ||
{ | ||
using System.Threading.Tasks; | ||
|
||
public interface IJournalWriteInterceptor | ||
{ | ||
Task InterceptAsync(IPersistentRepresentation message); | ||
} | ||
} |
Oops, something went wrong.