-
Notifications
You must be signed in to change notification settings - Fork 23
Add Akka.Hosting.TestKit module #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 15 commits into
akkadotnet:dev
from
Arkatufus:Add_Akka.Hosting.TestKit
Oct 4, 2022
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d2c96c8
Add Akka.Hosting.TestKit
Arkatufus 4f8786e
Add xunit.runner.json file
Arkatufus 1d9c746
Fix ImplicitSender
Arkatufus 9a5d73b
Merge branch 'dev' into Add_Akka.Hosting.TestKit
Arkatufus 9d3fab6
Fix spec .csproj file
Arkatufus cf6c4f8
Merge branch 'Add_Akka.Hosting.TestKit' of github.com:Arkatufus/Akka.…
Arkatufus 6259f0a
Base the TestKit off TestKitBase instead
Arkatufus 0c7bf1d
Merge branch 'dev' into Add_Akka.Hosting.TestKit
Arkatufus 5d54f42
Fix TestKit, test should start only after TestKit is initialized
Arkatufus 69dd73c
Fix TestEventListenerTest
Arkatufus dde3e0d
Merge branch 'dev' into Add_Akka.Hosting.TestKit
Aaronontheweb 2c3ce3d
Merge branch 'dev' into Add_Akka.Hosting.TestKit
Aaronontheweb f62069a
Change `ConfigureAkka` from virtual to abstract
Arkatufus 95bab69
Merge branch 'Add_Akka.Hosting.TestKit' of github.com:Arkatufus/Akka.…
Arkatufus f15c1cd
Refactor Akka.Persistence.Hosting.Tests to use Akka.Hosting.TestKit
Arkatufus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
src/Akka.Hosting.TestKit.Tests/Akka.Hosting.TestKit.Tests.csproj
This file contains hidden or 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,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(TestsNetCoreFramework)</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Akka.Hosting.TestKit\Akka.Hosting.TestKit.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> | ||
| <PackageReference Include="xunit" Version="$(XunitVersion)" /> | ||
| <PackageReference Include="FluentAssertions" Version="6.7.0" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunneVisualstudio)"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="xunit.runner.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,64 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="HostingSpecSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Akka.Actor; | ||
| using Akka.Event; | ||
| using Akka.TestKit.TestActors; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| using LogLevel = Microsoft.Extensions.Logging.LogLevel; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests | ||
| { | ||
| public class HostingSpecSpec: TestKit | ||
| { | ||
| private enum Echo | ||
| { } | ||
|
|
||
| public HostingSpecSpec(ITestOutputHelper output) | ||
| : base(nameof(HostingSpecSpec), output, logLevel: LogLevel.Debug) | ||
| { | ||
| } | ||
|
|
||
| protected override Task ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) | ||
| { | ||
| builder.WithActors((system, registry) => | ||
| { | ||
| var echo = system.ActorOf(Props.Create(() => new SimpleEchoActor())); | ||
| registry.Register<Echo>(echo); | ||
| }); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ActorTest() | ||
| { | ||
| var echo = ActorRegistry.Get<Echo>(); | ||
| var probe = CreateTestProbe(); | ||
|
|
||
| echo.Tell("TestMessage", probe); | ||
| var msg = probe.ExpectMsg("TestMessage"); | ||
| Log.Info(msg); | ||
| } | ||
|
|
||
| private class SimpleEchoActor : ReceiveActor | ||
| { | ||
| public SimpleEchoActor() | ||
| { | ||
| var log = Context.GetLogger(); | ||
|
|
||
| ReceiveAny(msg => | ||
| { | ||
| log.Info($"Received {msg}"); | ||
| Sender.Tell(msg); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,63 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="NoImplicitSenderSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using Akka.Actor; | ||
| using Akka.Actor.Dsl; | ||
| using Akka.TestKit; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests; | ||
|
|
||
| public class NoImplicitSenderSpec : TestKit, INoImplicitSender | ||
| { | ||
| [Fact] | ||
| public void When_Not_ImplicitSender_then_testActor_is_not_sender() | ||
| { | ||
| var echoActor = Sys.ActorOf(c => c.ReceiveAny((m, ctx) => TestActor.Tell(ctx.Sender))); | ||
| echoActor.Tell("message"); | ||
| var actorRef = ExpectMsg<IActorRef>(); | ||
| actorRef.Should().Be(Sys.DeadLetters); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public class ImplicitSenderSpec : TestKit | ||
| { | ||
| [Fact] | ||
| public void ImplicitSender_should_have_testActor_as_sender() | ||
| { | ||
| var echoActor = Sys.ActorOf(c => c.ReceiveAny((m, ctx) => TestActor.Tell(ctx.Sender))); | ||
| echoActor.Tell("message"); | ||
| ExpectMsg<IActorRef>(actorRef => Equals(actorRef, TestActor)); | ||
|
|
||
| //Test that it works after we know that context has been changed | ||
| echoActor.Tell("message"); | ||
| ExpectMsg<IActorRef>(actorRef => Equals(actorRef, TestActor)); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| [Fact] | ||
| public void ImplicitSender_should_not_change_when_creating_Testprobes() | ||
| { | ||
| //Verifies that bug #459 has been fixed | ||
| var testProbe = CreateTestProbe(); | ||
| TestActor.Tell("message"); | ||
| ReceiveOne(); | ||
| LastSender.Should().Be(TestActor); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ImplicitSender_should_not_change_when_creating_TestActors() | ||
| { | ||
| var testActor2 = CreateTestActor("test2"); | ||
| TestActor.Tell("message"); | ||
| ReceiveOne(); | ||
| LastSender.Should().Be(TestActor); | ||
| } | ||
| } |
This file contains hidden or 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,35 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="AssemblyInfo.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| // General Information about an assembly is controlled through the following | ||
| // set of attributes. Change these attribute values to modify the information | ||
| // associated with an assembly. | ||
| using Xunit; | ||
|
|
||
| // Setting ComVisible to false makes the types in this assembly not visible | ||
| // to COM components. If you need to access a type in this assembly from | ||
| // COM, set the ComVisible attribute to true on that type. | ||
| [assembly: ComVisible(false)] | ||
|
|
||
| // The following GUID is for the ID of the typelib if this project is exposed to COM | ||
| [assembly: Guid("b21496c0-a536-4953-9253-d2d0d526e42d")] | ||
|
|
||
| // Version information for an assembly consists of the following four values: | ||
| // | ||
| // Major Version | ||
| // Minor Version | ||
| // Build Number | ||
| // Revision | ||
| // | ||
| // You can specify all the values or you can default the Build and Revision Numbers | ||
| // by using the '*' as shown below: | ||
| // [assembly: AssemblyVersion("1.0.*")] | ||
|
|
||
| [assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly, DisableTestParallelization = true)] |
55 changes: 55 additions & 0 deletions
55
src/Akka.Hosting.TestKit.Tests/TestActorRefTests/BossActor.cs
This file contains hidden or 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,55 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="BossActor.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using Akka.Actor; | ||
| using Akka.TestKit; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests.TestActorRefTests; | ||
|
|
||
| public class BossActor : TActorBase | ||
| { | ||
| private TestActorRef<InternalActor> _child; | ||
|
|
||
| public BossActor() | ||
| { | ||
| _child = new TestActorRef<InternalActor>(Context.System, Props.Create<InternalActor>(), Self, "child"); | ||
| } | ||
|
|
||
| protected override SupervisorStrategy SupervisorStrategy() | ||
| { | ||
| return new OneForOneStrategy(maxNrOfRetries: 5, withinTimeRange: TimeSpan.FromSeconds(1), localOnlyDecider: ex => ex is ActorKilledException ? Directive.Restart : Directive.Escalate); | ||
| } | ||
|
|
||
| protected override bool ReceiveMessage(object message) | ||
| { | ||
| if(message is string && ((string)message) == "sendKill") | ||
| { | ||
| _child.Tell(Kill.Instance); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private class InternalActor : TActorBase | ||
| { | ||
| protected override void PreRestart(Exception reason, object message) | ||
| { | ||
| TestActorRefSpec.Counter--; | ||
| } | ||
|
|
||
| protected override void PostRestart(Exception reason) | ||
| { | ||
| TestActorRefSpec.Counter--; | ||
| } | ||
|
|
||
| protected override bool ReceiveMessage(object message) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
src/Akka.Hosting.TestKit.Tests/TestActorRefTests/FsmActor.cs
This file contains hidden or 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,56 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="FsmActor.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using Akka.Actor; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests.TestActorRefTests; | ||
|
|
||
| public enum TestFsmState | ||
| { | ||
| First, | ||
| Last | ||
| } | ||
|
|
||
| public class FsmActor : FSM<TestFsmState, string> | ||
| { | ||
| private readonly IActorRef _replyActor; | ||
|
|
||
| public FsmActor(IActorRef replyActor) | ||
| { | ||
| _replyActor = replyActor; | ||
|
|
||
| When(TestFsmState.First, e => | ||
| { | ||
| if (e.FsmEvent.Equals("check")) | ||
| { | ||
| _replyActor.Tell("first"); | ||
| } | ||
| else if (e.FsmEvent.Equals("next")) | ||
| { | ||
| return GoTo(TestFsmState.Last); | ||
| } | ||
|
|
||
| return Stay(); | ||
| }); | ||
|
|
||
| When(TestFsmState.Last, e => | ||
| { | ||
| if (e.FsmEvent.Equals("check")) | ||
| { | ||
| _replyActor.Tell("last"); | ||
| } | ||
| else if (e.FsmEvent.Equals("next")) | ||
| { | ||
| return GoTo(TestFsmState.First); | ||
| } | ||
|
|
||
| return Stay(); | ||
| }); | ||
|
|
||
| StartWith(TestFsmState.First, "foo"); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Akka.Hosting.TestKit.Tests/TestActorRefTests/Logger.cs
This file contains hidden or 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,28 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="Logger.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using Akka.Actor; | ||
| using Akka.Event; | ||
|
|
||
| namespace Akka.Hosting.TestKit.Tests.TestActorRefTests; | ||
|
|
||
| public class Logger : ActorBase | ||
| { | ||
| private int _count; | ||
| private string _msg; | ||
| protected override bool Receive(object message) | ||
| { | ||
| var warning = message as Warning; | ||
| if(warning != null && warning.Message is string) | ||
| { | ||
| _count++; | ||
| _msg = (string)warning.Message; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.