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

Change ReceiveWhile Test Methods to Sync over Async #5682

Merged
merged 11 commits into from
Feb 24, 2022
2 changes: 2 additions & 0 deletions src/core/Akka.TestKit/Akka.TestKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.1.2" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.1.2" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
22 changes: 17 additions & 5 deletions src/core/Akka.TestKit/TestKitBase_Expect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit.Internal;
using Akka.Util;
using Nito.AsyncEx.Synchronous;

namespace Akka.TestKit
{
Expand Down Expand Up @@ -369,19 +371,29 @@ public IReadOnlyCollection<T> ExpectMsgAllOf<T>(TimeSpan max, params T[] message
return InternalExpectMsgAllOf(dilated, messages);
}

private IReadOnlyCollection<T> InternalExpectMsgAllOf<T>(TimeSpan max, IReadOnlyCollection<T> messages, Func<T, T, bool> areEqual = null, bool shouldLog=false)
private IReadOnlyCollection<T> InternalExpectMsgAllOf<T>(TimeSpan max, IReadOnlyCollection<T> messages, Func<T, T, bool> areEqual = null, bool shouldLog=false, CancellationToken cancellationToken = default)
{
var task = InternalExpectMsgAllOfAsync(max, messages, areEqual, shouldLog, cancellationToken);
task.WaitAndUnwrapException(cancellationToken);
return task.Result;
}

private async Task<IReadOnlyCollection<T>> InternalExpectMsgAllOfAsync<T>(TimeSpan max,
IReadOnlyCollection<T> messages, Func<T, T, bool> areEqual = null, bool shouldLog = false,
CancellationToken cancellationToken = default)
{
ConditionalLog(shouldLog, "Expecting {0} messages during {1}", messages.Count, max);
areEqual = areEqual ?? ((x, y) => Equals(x, y));
var start = Now;
var receivedMessages = InternalReceiveN(messages.Count, max, shouldLog).ToList();
var missing = messages.Where(m => !receivedMessages.Any(r => r is T && areEqual((T)r, m))).ToList();
var unexpected = receivedMessages.Where(r => !messages.Any(m => r is T && areEqual((T)r, m))).ToList();

var receivedMessages = await InternalReceiveNAsync(messages.Count, max, shouldLog, cancellationToken).ToListAsync(cancellationToken);

var missing = messages.Where(m => !receivedMessages.Any(r => r is T obj && areEqual(obj, m))).ToList();
var unexpected = receivedMessages.Where(r => !messages.Any(m => r is T obj && areEqual(obj, m))).ToList();
CheckMissingAndUnexpected(missing, unexpected, "not found", "found unexpected", shouldLog, string.Format("Expected {0} messages during {1}. Failed after {2}. ", messages.Count, max, Now-start));
return receivedMessages.Cast<T>().ToList();
}


private void CheckMissingAndUnexpected<TMissing, TUnexpected>(IReadOnlyCollection<TMissing> missing, IReadOnlyCollection<TUnexpected> unexpected, string missingMessage, string unexpectedMessage, bool shouldLog, string hint)
{
var missingIsEmpty = missing.Count == 0;
Expand Down
Loading