Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Akka.TestKit.Tests.TestKitBaseTests.ReceiveTests to async #5868

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/Akka.TestKit.Tests/Akka.TestKit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<AssemblyTitle>Akka.TestKit.Tests</AssemblyTitle>
<TargetFrameworks>$(NetFrameworkTestVersion);$(NetTestVersion);$(NetCoreTestVersion)</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Akka\Akka.csproj" />
Expand Down
148 changes: 68 additions & 80 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,113 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using FluentAssertions;
using Xunit;
using Xunit.Sdk;
using static FluentAssertions.FluentActions;

namespace Akka.Testkit.Tests.TestKitBaseTests
namespace Akka.TestKit.Tests.TestKitBaseTests
{
public class ReceiveTests : AkkaSpec
{
[Fact]
public void ReceiveN_should_receive_correct_number_of_messages()
public async Task ReceiveNAsync_should_receive_correct_number_of_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell("4");
ReceiveN(3).ShouldOnlyContainInOrder("1", "2", "3");
ReceiveN(1).ShouldOnlyContainInOrder("4");
await ReceiveNAsync(3).ShouldOnlyContainInOrderAsync("1", "2", "3");
await ReceiveNAsync(1).ShouldOnlyContainInOrderAsync("4");
}

[Fact]
public void ReceiveN_should_timeout_if_no_messages()
public async Task ReceiveNAsync_should_timeout_if_no_messages()
{
Intercept(() => ReceiveN(3, TimeSpan.FromMilliseconds(10)));
await Awaiting(async () => await ReceiveNAsync(3, TimeSpan.FromMilliseconds(10)).ToListAsync())
.Should().ThrowAsync<Exception>();
}

[Fact]
public void ReceiveN_should_timeout_if_to_few_messages()
public async Task ReceiveNAsync_should_timeout_if_to_few_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
Intercept(() => ReceiveN(3, TimeSpan.FromMilliseconds(100)));
await Awaiting(async () => await ReceiveNAsync(3, TimeSpan.FromMilliseconds(100)).ToListAsync())
.Should().ThrowAsync<Exception>();
}


[Fact]
public void FishForMessage_should_return_matched_message()
public async Task FishForMessageAsync_should_return_matched_message()
{
TestActor.Tell(1);
TestActor.Tell(2);
TestActor.Tell(10);
TestActor.Tell(20);
FishForMessage<int>(i => i >= 10).ShouldBe(10);
await FishForMessageAsync<int>(i => i >= 10).ShouldBeAsync(10);
}

[Fact]
public void FishForMessage_should_timeout_if_no_messages()
public async Task FishForMessageAsync_should_timeout_if_no_messages()
{
Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(10)));
await Awaiting(async () => await FishForMessageAsync(_ => false, TimeSpan.FromMilliseconds(10)))
.Should().ThrowAsync<Exception>();
}

[Fact]
public void FishForMessage_should_timeout_if_to_few_messages()
public async Task FishForMessageAsync_should_timeout_if_too_few_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(100)));
await Awaiting(async () => await FishForMessageAsync(_ => false, TimeSpan.FromMilliseconds(100)))
.Should().ThrowAsync<Exception>();
}

[Fact]
public async Task FishForMessage_should_fill_the_all_messages_param_if_not_null()
public async Task FishForMessageAsync_should_fill_the_all_messages_param_if_not_null()
{
await Task.Run(delegate
{
var probe = base.CreateTestProbe("probe");
probe.Tell("1");
probe.Tell(2);
probe.Tell("3");
probe.Tell(4);
var allMessages = new ArrayList();
probe.FishForMessage<string>(isMessage: s => s == "3", allMessages: allMessages);
allMessages.Should().BeEquivalentTo(new ArrayList { "1", 2 });
});
var probe = CreateTestProbe("probe");
probe.Tell("1");
probe.Tell(2);
probe.Tell("3");
probe.Tell(4);
var allMessages = new ArrayList();
await probe.FishForMessageAsync<string>(isMessage: s => s == "3", allMessages: allMessages);
allMessages.Should().BeEquivalentTo(new ArrayList { "1", 2 });
}

[Fact]
public async Task FishForMessage_should_clear_the_all_messages_param_if_not_null_before_filling_it()
public async Task FishForMessageAsync_should_clear_the_all_messages_param_if_not_null_before_filling_it()
{
await Task.Run(delegate
{
var probe = base.CreateTestProbe("probe");
probe.Tell("1");
probe.Tell(2);
probe.Tell("3");
probe.Tell(4);
var allMessages = new ArrayList() { "pre filled data" };
probe.FishForMessage<string>(isMessage: x => x == "3", allMessages: allMessages);
allMessages.Should().BeEquivalentTo(new ArrayList { "1", 2 });
});
var probe = CreateTestProbe("probe");
probe.Tell("1");
probe.Tell(2);
probe.Tell("3");
probe.Tell(4);
var allMessages = new ArrayList { "pre filled data" };
await probe.FishForMessageAsync<string>(isMessage: x => x == "3", allMessages: allMessages);
allMessages.Should().BeEquivalentTo(new ArrayList { "1", 2 });
}

[Fact]
public async Task FishUntilMessageAsync_should_succeed_with_good_input()
{
var probe = CreateTestProbe("probe");
probe.Ref.Tell(1d, TestActor);
await probe.FishUntilMessageAsync<int>(max: TimeSpan.FromMilliseconds(10));
await Awaiting(() => probe.FishUntilMessageAsync<int>(max: TimeSpan.FromMilliseconds(10)))
.Should().NotThrowAsync();
}


[Fact]
public async Task FishUntilMessageAsync_should_fail_with_bad_input()
{
var probe = CreateTestProbe("probe");
probe.Ref.Tell(3, TestActor);
Func<Task> func = () => probe.FishUntilMessageAsync<int>(max: TimeSpan.FromMilliseconds(10));
await func.Should().ThrowAsync<Exception>();
await Awaiting(() => probe.FishUntilMessageAsync<int>(max: TimeSpan.FromMilliseconds(10)))
.Should().ThrowAsync<Exception>();
}

[Fact]
Expand Down Expand Up @@ -169,86 +166,82 @@ public async Task WaitForRadioSilenceAsync_should_fail_immediately_with_bad_inpu
{
var probe = CreateTestProbe("probe");
probe.Ref.Tell(3, TestActor);
try
{
await probe.WaitForRadioSilenceAsync(max: TimeSpan.FromMilliseconds(0), maxMessages: 0);
Assert.True(false, "we should never get here");
}
catch (XunitException) { }
await Awaiting(() => probe.WaitForRadioSilenceAsync(max: TimeSpan.FromMilliseconds(0), maxMessages: 0))
.Should().ThrowAsync<FalseException>().WithMessage("maxMessages violated*");
}

[Fact]
public void ReceiveWhile_Filter_should_on_a_timeout_return_no_messages()
public async Task ReceiveWhileAsync_Filter_should_on_a_timeout_return_no_messages()
{
ReceiveWhile<object>(_ => _, TimeSpan.FromMilliseconds(10)).Count.ShouldBe(0);
await using var e = ReceiveWhileAsync(_ => _, TimeSpan.FromMilliseconds(10)).GetAsyncEnumerator();
(await e.MoveNextAsync()).Should().BeFalse();
}

[Fact]
public void ReceiveWhile_Filter_should_break_on_function_returning_null_and_return_correct_messages()
public async Task ReceiveWhileAsync_Filter_should_break_on_function_returning_null_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell(2);
TestActor.Tell("3");
TestActor.Tell(99999.0);
TestActor.Tell(4);
ReceiveWhile<string>(_ => _ is double ? null : _.ToString()).ShouldOnlyContainInOrder("1", "2", "3");
await ReceiveWhileAsync(_ => _ is double ? null : _.ToString()).ShouldOnlyContainInOrderAsync("1", "2", "3");
}

[Fact]
public void ReceiveWhile_Filter_should_not_consume_last_message_that_didnt_match()
public async Task ReceiveWhileAsync_Filter_should_not_consume_last_message_that_didnt_match()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell(4711);
ReceiveWhile<object>(_ => _ is string ? _ : null);
ExpectMsg(4711);
await ReceiveWhileAsync(_ => _ is string ? _ : null).ToListAsync();
await ExpectMsgAsync(4711);
}

[Fact]
public void ReceiveWhile_Predicate_should_on_a_timeout_return_no_messages()
public async Task ReceiveWhileAsync_Predicate_should_on_a_timeout_return_no_messages()
{
ReceiveWhile<object>(_ => false, TimeSpan.FromMilliseconds(10)).Count.ShouldBe(0);
await using var e = ReceiveWhileAsync<object>(_ => false, TimeSpan.FromMilliseconds(10)).GetAsyncEnumerator();
(await e.MoveNextAsync()).Should().BeFalse();
}

[Fact]
public void ReceiveWhile_Predicate_should_break_when_predicate_returns_false_and_return_correct_messages()
public async Task ReceiveWhileAsync_Predicate_should_break_when_predicate_returns_false_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell("-----------");
TestActor.Tell("4");
ReceiveWhile<string>(s => s.Length == 1).ShouldOnlyContainInOrder("1", "2", "3");
await ReceiveWhileAsync<string>(s => s.Length == 1).ShouldOnlyContainInOrderAsync("1", "2", "3");
}

[Fact]
public void
ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages()
public async Task ReceiveWhileAsync_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");
await ReceiveWhileAsync<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false)
.ShouldOnlyContainInOrderAsync("1", "2", "3");
}

[Fact]
public void
ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages()
public async Task ReceiveWhileAsync_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");
await ReceiveWhileAsync<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true)
.ShouldOnlyContainInOrderAsync("1", "2", "3", "5");
}

[Fact]
public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_match()
public async Task ReceiveWhileAsync_Predicate_should_not_consume_last_message_that_didnt_match()
{
TestActor.Tell("1");
TestActor.Tell("2");
Expand All @@ -260,15 +253,10 @@ public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_ma
TestActor.Tell("7");
TestActor.Tell("8");

var received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("1", "2");

ExpectMsg(4711);

received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("3", "4", "5");

ExpectMsg(6);
await ReceiveWhileAsync<object>(_ => _ is string).ShouldOnlyContainInOrderAsync("1", "2");
await ExpectMsgAsync(4711);
await ReceiveWhileAsync<object>(_ => _ is string).ShouldOnlyContainInOrderAsync("3", "4", "5");
await ExpectMsgAsync(6);
}
}
}
2 changes: 1 addition & 1 deletion src/core/Akka.TestKit/TestKitBase_Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ public IReadOnlyCollection<object> ReceiveN(
/// <inheritdoc cref="ReceiveN(int, CancellationToken)"/>
public async IAsyncEnumerable<object> ReceiveNAsync(
int numberOfMessages,
[EnumeratorCancellation] CancellationToken cancellationToken)
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var enumerable = InternalReceiveNAsync(numberOfMessages, RemainingOrDefault, true, cancellationToken)
.ConfigureAwait(false).WithCancellation(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFrameworks>$(NetStandardLibVersion)</TargetFrameworks>
<TargetFramework>$(NetStandardLibVersion)</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading