From 985e5edcf8d33843242d2fe2aadb1ebcced90152 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 17 Dec 2021 15:39:21 -0600 Subject: [PATCH] Make updates to `FishUntil` (#5433) * Make updates to `FishUntil` Implement nitpicks from https://github.com/akkadotnet/akka.net/pull/5430 * Nitpick * cleaned up `FishUntilMessage` * removed `FluentAssertions` reference * fixed bad `TestKit.ReceiveTests` --- .../TestKitBaseTests/ReceiveTests.cs | 44 +++++++++---------- src/core/Akka.TestKit/Akka.TestKit.csproj | 1 - src/core/Akka.TestKit/TestKitBase_Receive.cs | 22 +++++----- 3 files changed, 34 insertions(+), 33 deletions(-) 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(i => i>=10).ShouldBe(10); + FishForMessage(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(probe, max: TimeSpan.FromMilliseconds(10)).Wait(); + await probe.FishUntilMessage(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(probe, max: TimeSpan.FromMilliseconds(10)).Wait(); - Assert.True(false); // we should never get here - } - catch (AggregateException ex) - { - ex.InnerExceptions[0].Should().BeOfType(); - } + + + // based on: https://getakka.net/articles/actors/testing-actor-systems.html#the-way-in-between-expecting-exceptions + Func func = () => probe.FishUntilMessage(max: TimeSpan.FromMilliseconds(10)); + await func.Should().ThrowAsync(); } [Fact] @@ -135,25 +131,31 @@ public void ReceiveWhile_Predicate_should_break_when_predicate_returns_false_and TestActor.Tell("4"); ReceiveWhile(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(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false).ShouldOnlyContainInOrder("1", "2", "3"); + ReceiveWhile(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(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true).ShouldOnlyContainInOrder("1", "2", "3","5"); + ReceiveWhile(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 @@ - 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(Predicate 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; } } } /// - /// Receives messages until . Ignores all messages except for a message of type . Asserts that all messages are not of the of type . Note that when comparing types, inheritance is ignored, in other words, only perfectly matching types are asserted. + /// Receives messages until . + /// + /// Ignores all messages except for a message of type . + /// Asserts that all messages are not of the of type . + /// Note that when comparing types, inheritance is ignored, in other words, only perfectly matching types are asserted. /// /// The type that the message is not supposed to be. - /// - /// - public async static Task InverseFishForMessage(TestProbe probe, TimeSpan? max = null) + /// Optional. The maximum wait duration. Defaults to when unset. + public async Task FishUntilMessage(TimeSpan? max = null) { await Task.Run(() => { - probe.ReceiveWhile(max: max, shouldContinue: x => + ReceiveWhile(max: max, shouldContinue: x => { - x.Should().NotBeOfType(); - 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 }); }); }