Skip to content

Commit

Permalink
Added a positive test and a negative test for InverseFishForMessage (#…
Browse files Browse the repository at this point in the history
…5434)

* Added a positive test and a negative test for InverseFishForMessage

* Renamed parameter shouldIgnore to shouldContinue to agree with code and docs.
  • Loading branch information
brah-mcdude committed Dec 15, 2021
1 parent 7e48e35 commit d760387
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System;
using Akka.Actor;
using Akka.TestKit;
using FluentAssertions;
using Xunit;
using Xunit.Sdk;

namespace Akka.Testkit.Tests.TestKitBaseTests
{
Expand Down Expand Up @@ -64,6 +66,32 @@ public void FishForMessage_should_timeout_if_to_few_messages()
Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(100)));
}

[Fact]
public void InverseFishForMessage_should_succeed_with_good_input()
{
var probe = CreateTestProbe("probe");
probe.Ref.Tell(1d, TestActor);
InverseFishForMessage<int>(probe, max: TimeSpan.FromMilliseconds(10)).Wait();
}


[Fact]
public void 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>();
}
}

[Fact]
public void ReceiveWhile_Filter_should_on_a_timeout_return_no_messages()
{
Expand Down
10 changes: 5 additions & 5 deletions src/core/Akka.TestKit/TestKitBase_Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async static Task InverseFishForMessage<T>(TestProbe probe, TimeSpan? max
{
await Task.Run(() =>
{
probe.ReceiveWhile<object>(max: max, shouldIgnore: x =>
probe.ReceiveWhile<object>(max: max, shouldContinue: x =>
{
x.Should().NotBeOfType<T>();
return false; // we are not returning anything
Expand Down Expand Up @@ -285,7 +285,7 @@ public IReadOnlyList<T> ReceiveWhile<T>(Func<object, T> filter, TimeSpan? max =

/// <summary>
/// Receive a series of messages.
/// It will continue to receive messages until the <paramref name="shouldIgnore"/> predicate returns <c>false</c> or the idle
/// It will continue to receive messages until the <paramref name="shouldContinue"/> predicate returns <c>false</c> or the idle
/// timeout is met (disabled by default) or the overall
/// maximum duration is elapsed or expected messages count is reached.
/// If a message that isn't of type <typeparamref name="T"/> the parameter <paramref name="shouldIgnoreOtherMessageTypes"/>
Expand All @@ -296,13 +296,13 @@ public IReadOnlyList<T> ReceiveWhile<T>(Func<object, T> filter, TimeSpan? max =
/// The max duration is scaled by <see cref="Dilated(TimeSpan)"/>
/// </summary>
/// <typeparam name="T">TBD</typeparam>
/// <param name="shouldIgnore">TBD</param>
/// <param name="shouldContinue">TBD</param>
/// <param name="max">TBD</param>
/// <param name="idle">TBD</param>
/// <param name="msgs">TBD</param>
/// <param name="shouldIgnoreOtherMessageTypes">TBD</param>
/// <returns>TBD</returns>
public IReadOnlyList<T> ReceiveWhile<T>(Predicate<T> shouldIgnore, TimeSpan? max = null, TimeSpan? idle = null, int msgs = int.MaxValue, bool shouldIgnoreOtherMessageTypes = true) where T : class
public IReadOnlyList<T> ReceiveWhile<T>(Predicate<T> shouldContinue, TimeSpan? max = null, TimeSpan? idle = null, int msgs = int.MaxValue, bool shouldIgnoreOtherMessageTypes = true) where T : class
{
var start = Now;
var maxValue = RemainingOrDilated(max);
Expand All @@ -326,7 +326,7 @@ public IReadOnlyList<T> ReceiveWhile<T>(Func<object, T> filter, TimeSpan? max =
var shouldStop = false;
if (typedMessage != null)
{
if (shouldIgnore(typedMessage))
if (shouldContinue(typedMessage))
{
acc.Add(typedMessage);
count++;
Expand Down

0 comments on commit d760387

Please sign in to comment.