Skip to content

Commit

Permalink
Make updates to FishUntil (#5433)
Browse files Browse the repository at this point in the history
* Make updates to `FishUntil`

Implement nitpicks from #5430

* Nitpick

* cleaned up `FishUntilMessage`

* removed `FluentAssertions` reference

* fixed bad `TestKit.ReceiveTests`
  • Loading branch information
Aaronontheweb authored Dec 17, 2021
1 parent 60807fe commit 985e5ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
44 changes: 22 additions & 22 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using FluentAssertions;
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -179,7 +181,5 @@ public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_ma

ExpectMsg(6);
}

}
}

}
1 change: 0 additions & 1 deletion src/core/Akka.TestKit/Akka.TestKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
22 changes: 12 additions & 10 deletions src/core/Akka.TestKit/TestKitBase_Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Threading;
using System.Threading.Tasks;
using Akka.TestKit.Internal;
using FluentAssertions;

namespace Akka.TestKit
{
Expand Down Expand Up @@ -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
});
});
}
Expand Down

0 comments on commit 985e5ed

Please sign in to comment.