diff --git a/src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs b/src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs index b907dbd400d..85efaa4d4c8 100644 --- a/src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs +++ b/src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs @@ -6,6 +6,7 @@ //----------------------------------------------------------------------- using System; +using System.Threading.Tasks; using Akka.Actor; using Akka.TestKit; using FluentAssertions; @@ -49,13 +50,13 @@ public void FishForMessage_should_return_matched_message() TestActor.Tell(2); TestActor.Tell(10); TestActor.Tell(20); - FishForMessage<int>(i => i>=10).ShouldBe(10); + FishForMessage<int>(i => i >= 10).ShouldBe(10); } [Fact] public void FishForMessage_should_timeout_if_no_messages() { - Intercept(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(10))); + Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(10))); } [Fact] @@ -67,29 +68,24 @@ public void FishForMessage_should_timeout_if_to_few_messages() } [Fact] - public void InverseFishForMessage_should_succeed_with_good_input() + public async Task InverseFishForMessage_should_succeed_with_good_input() { var probe = CreateTestProbe("probe"); probe.Ref.Tell(1d, TestActor); - InverseFishForMessage<int>(probe, max: TimeSpan.FromMilliseconds(10)).Wait(); + await probe.FishUntilMessage<int>(max: TimeSpan.FromMilliseconds(10)); } [Fact] - public void InverseFishForMessage_should_fail_with_bad_input() + public async Task InverseFishForMessage_should_fail_with_bad_input() { var probe = CreateTestProbe("probe"); probe.Ref.Tell(3, TestActor); - try - { - /// based on: https://getakka.net/articles/actors/testing-actor-systems.html#the-way-in-between-expecting-exceptions - InverseFishForMessage<int>(probe, max: TimeSpan.FromMilliseconds(10)).Wait(); - Assert.True(false); // we should never get here - } - catch (AggregateException ex) - { - ex.InnerExceptions[0].Should().BeOfType<XunitException>(); - } + + + // based on: https://getakka.net/articles/actors/testing-actor-systems.html#the-way-in-between-expecting-exceptions + Func<Task> func = () => probe.FishUntilMessage<int>(max: TimeSpan.FromMilliseconds(10)); + await func.Should().ThrowAsync<Exception>(); } [Fact] @@ -135,25 +131,31 @@ public void ReceiveWhile_Predicate_should_break_when_predicate_returns_false_and TestActor.Tell("4"); ReceiveWhile<string>(s => s.Length == 1).ShouldOnlyContainInOrder("1", "2", "3"); } + [Fact] - public void ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages() + public void + ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages() { TestActor.Tell("1"); TestActor.Tell("2"); TestActor.Tell("3"); TestActor.Tell(4); TestActor.Tell("5"); - ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false).ShouldOnlyContainInOrder("1", "2", "3"); + ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false) + .ShouldOnlyContainInOrder("1", "2", "3"); } + [Fact] - public void ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages() + public void + ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages() { TestActor.Tell("1"); TestActor.Tell("2"); TestActor.Tell("3"); TestActor.Tell(4); TestActor.Tell("5"); - ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true).ShouldOnlyContainInOrder("1", "2", "3","5"); + ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true) + .ShouldOnlyContainInOrder("1", "2", "3", "5"); } [Fact] @@ -179,7 +181,5 @@ public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_ma ExpectMsg(6); } - } -} - +} \ No newline at end of file diff --git a/src/core/Akka.TestKit/Akka.TestKit.csproj b/src/core/Akka.TestKit/Akka.TestKit.csproj index d6937044f1c..4c97416d237 100644 --- a/src/core/Akka.TestKit/Akka.TestKit.csproj +++ b/src/core/Akka.TestKit/Akka.TestKit.csproj @@ -11,7 +11,6 @@ <ItemGroup> <EmbeddedResource Include="Configs\TestScheduler.conf;Internal\Reference.conf" /> - <PackageReference Include="FluentAssertions" Version="5.10.3" /> <ProjectReference Include="..\Akka\Akka.csproj" /> </ItemGroup> diff --git a/src/core/Akka.TestKit/TestKitBase_Receive.cs b/src/core/Akka.TestKit/TestKitBase_Receive.cs index 719acc5a8c5..faa6bf493d3 100644 --- a/src/core/Akka.TestKit/TestKitBase_Receive.cs +++ b/src/core/Akka.TestKit/TestKitBase_Receive.cs @@ -11,7 +11,6 @@ using System.Threading; using System.Threading.Tasks; using Akka.TestKit.Internal; -using FluentAssertions; namespace Akka.TestKit { @@ -51,27 +50,30 @@ public T FishForMessage<T>(Predicate<T> isMessage, TimeSpan? max = null, string var left = end - Now; var msg = ReceiveOne(left); _assertions.AssertTrue(msg != null, "Timeout ({0}) during fishForMessage{1}", maxValue, string.IsNullOrEmpty(hint) ? "" : ", hint: " + hint); - if (msg is T && isMessage((T)msg)) + if (msg is T msg1 && isMessage(msg1)) { - return (T)msg; + return msg1; } } } /// <summary> - /// Receives messages until <paramref name="max"/>. Ignores all messages except for a message of type <typeparamref name="T"/>. Asserts that all messages are not of the of type <typeparamref name="T"/>. Note that when comparing types, inheritance is ignored, in other words, only perfectly matching types are asserted. + /// Receives messages until <paramref name="max"/>. + /// + /// Ignores all messages except for a message of type <typeparamref name="T"/>. + /// Asserts that all messages are not of the of type <typeparamref name="T"/>. + /// Note that when comparing types, inheritance is ignored, in other words, only perfectly matching types are asserted. /// </summary> /// <typeparam name="T">The type that the message is not supposed to be.</typeparam> - /// <param name="probe"></param> - /// <param name="max"></param> - public async static Task InverseFishForMessage<T>(TestProbe probe, TimeSpan? max = null) + /// <param name="max">Optional. The maximum wait duration. Defaults to <see cref="RemainingOrDefault"/> when unset.</param> + public async Task FishUntilMessage<T>(TimeSpan? max = null) { await Task.Run(() => { - probe.ReceiveWhile<object>(max: max, shouldContinue: x => + ReceiveWhile<object>(max: max, shouldContinue: x => { - x.Should().NotBeOfType<T>(); - return false; // we are not returning anything + _assertions.AssertFalse(x is T, "did not expect a message of type {0}", typeof(T)); + return true; // we are not returning anything }); }); }