Skip to content

Commit cc5318f

Browse files
committed
Make updates to FishUntil (akkadotnet#5433)
* Make updates to `FishUntil` Implement nitpicks from akkadotnet#5430 * Nitpick * cleaned up `FishUntilMessage` * removed `FluentAssertions` reference * fixed bad `TestKit.ReceiveTests`
1 parent ceb137e commit cc5318f

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//-----------------------------------------------------------------------
77

88
using System;
9+
using System.Threading.Tasks;
910
using Akka.Actor;
1011
using Akka.TestKit;
1112
using FluentAssertions;
@@ -49,13 +50,13 @@ public void FishForMessage_should_return_matched_message()
4950
TestActor.Tell(2);
5051
TestActor.Tell(10);
5152
TestActor.Tell(20);
52-
FishForMessage<int>(i => i>=10).ShouldBe(10);
53+
FishForMessage<int>(i => i >= 10).ShouldBe(10);
5354
}
5455

5556
[Fact]
5657
public void FishForMessage_should_timeout_if_no_messages()
5758
{
58-
Intercept(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(10)));
59+
Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(10)));
5960
}
6061

6162
[Fact]
@@ -67,29 +68,24 @@ public void FishForMessage_should_timeout_if_to_few_messages()
6768
}
6869

6970
[Fact]
70-
public void InverseFishForMessage_should_succeed_with_good_input()
71+
public async Task InverseFishForMessage_should_succeed_with_good_input()
7172
{
7273
var probe = CreateTestProbe("probe");
7374
probe.Ref.Tell(1d, TestActor);
74-
InverseFishForMessage<int>(probe, max: TimeSpan.FromMilliseconds(10)).Wait();
75+
await probe.FishUntilMessage<int>(max: TimeSpan.FromMilliseconds(10));
7576
}
7677

7778

7879
[Fact]
79-
public void InverseFishForMessage_should_fail_with_bad_input()
80+
public async Task InverseFishForMessage_should_fail_with_bad_input()
8081
{
8182
var probe = CreateTestProbe("probe");
8283
probe.Ref.Tell(3, TestActor);
83-
try
84-
{
85-
/// based on: https://getakka.net/articles/actors/testing-actor-systems.html#the-way-in-between-expecting-exceptions
86-
InverseFishForMessage<int>(probe, max: TimeSpan.FromMilliseconds(10)).Wait();
87-
Assert.True(false); // we should never get here
88-
}
89-
catch (AggregateException ex)
90-
{
91-
ex.InnerExceptions[0].Should().BeOfType<XunitException>();
92-
}
84+
85+
86+
// based on: https://getakka.net/articles/actors/testing-actor-systems.html#the-way-in-between-expecting-exceptions
87+
Func<Task> func = () => probe.FishUntilMessage<int>(max: TimeSpan.FromMilliseconds(10));
88+
await func.Should().ThrowAsync<Exception>();
9389
}
9490

9591
[Fact]
@@ -135,25 +131,31 @@ public void ReceiveWhile_Predicate_should_break_when_predicate_returns_false_and
135131
TestActor.Tell("4");
136132
ReceiveWhile<string>(s => s.Length == 1).ShouldOnlyContainInOrder("1", "2", "3");
137133
}
134+
138135
[Fact]
139-
public void ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages()
136+
public void
137+
ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages()
140138
{
141139
TestActor.Tell("1");
142140
TestActor.Tell("2");
143141
TestActor.Tell("3");
144142
TestActor.Tell(4);
145143
TestActor.Tell("5");
146-
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false).ShouldOnlyContainInOrder("1", "2", "3");
144+
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false)
145+
.ShouldOnlyContainInOrder("1", "2", "3");
147146
}
147+
148148
[Fact]
149-
public void ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages()
149+
public void
150+
ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages()
150151
{
151152
TestActor.Tell("1");
152153
TestActor.Tell("2");
153154
TestActor.Tell("3");
154155
TestActor.Tell(4);
155156
TestActor.Tell("5");
156-
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true).ShouldOnlyContainInOrder("1", "2", "3","5");
157+
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true)
158+
.ShouldOnlyContainInOrder("1", "2", "3", "5");
157159
}
158160

159161
[Fact]
@@ -179,7 +181,5 @@ public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_ma
179181

180182
ExpectMsg(6);
181183
}
182-
183184
}
184-
}
185-
185+
}

src/core/Akka.TestKit/Akka.TestKit.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<ItemGroup>
1313
<EmbeddedResource Include="Configs\TestScheduler.conf;Internal\Reference.conf" />
14-
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1514
<ProjectReference Include="..\Akka\Akka.csproj" />
1615
</ItemGroup>
1716

src/core/Akka.TestKit/TestKitBase_Receive.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Akka.TestKit.Internal;
14-
using FluentAssertions;
1514

1615
namespace Akka.TestKit
1716
{
@@ -51,27 +50,30 @@ public T FishForMessage<T>(Predicate<T> isMessage, TimeSpan? max = null, string
5150
var left = end - Now;
5251
var msg = ReceiveOne(left);
5352
_assertions.AssertTrue(msg != null, "Timeout ({0}) during fishForMessage{1}", maxValue, string.IsNullOrEmpty(hint) ? "" : ", hint: " + hint);
54-
if (msg is T && isMessage((T)msg))
53+
if (msg is T msg1 && isMessage(msg1))
5554
{
56-
return (T)msg;
55+
return msg1;
5756
}
5857
}
5958
}
6059

6160
/// <summary>
62-
/// 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.
61+
/// Receives messages until <paramref name="max"/>.
62+
///
63+
/// Ignores all messages except for a message of type <typeparamref name="T"/>.
64+
/// Asserts that all messages are not of the of type <typeparamref name="T"/>.
65+
/// Note that when comparing types, inheritance is ignored, in other words, only perfectly matching types are asserted.
6366
/// </summary>
6467
/// <typeparam name="T">The type that the message is not supposed to be.</typeparam>
65-
/// <param name="probe"></param>
66-
/// <param name="max"></param>
67-
public async static Task InverseFishForMessage<T>(TestProbe probe, TimeSpan? max = null)
68+
/// <param name="max">Optional. The maximum wait duration. Defaults to <see cref="RemainingOrDefault"/> when unset.</param>
69+
public async Task FishUntilMessage<T>(TimeSpan? max = null)
6870
{
6971
await Task.Run(() =>
7072
{
71-
probe.ReceiveWhile<object>(max: max, shouldContinue: x =>
73+
ReceiveWhile<object>(max: max, shouldContinue: x =>
7274
{
73-
x.Should().NotBeOfType<T>();
74-
return false; // we are not returning anything
75+
_assertions.AssertFalse(x is T, "did not expect a message of type {0}", typeof(T));
76+
return true; // we are not returning anything
7577
});
7678
});
7779
}

0 commit comments

Comments
 (0)