Skip to content

Commit

Permalink
Change ReceiveWhile Test Methods to Sync over Async (#5682)
Browse files Browse the repository at this point in the history
* Fix the remaining `FishForMessage` `Sync` over `Async` methods

* * Changed `ReceiveWhile` to `Sync` over `Async`
* Created `ReceiveWhileAsync()`

* Add missing TBD

* Create `ReceiveNAsync()`

* Potential fix for DocFx `StackOverflow` exception

* * Changed `FishForMessage` to directly call its `async` version
* Fix possible cause of `Stackoverflow` exception - methods inheriting docs from itself.

* Fix build error

* Added `CancellationToken` support

* Changed Receive methods to sync-over-async

* Add CancellationToken support to InternalReceiveNAsync, remove non-async private InternalReceiveN

Co-authored-by: Gregorius Soedharmo <[email protected]>
  • Loading branch information
eaba and Greg-Petabridge authored Feb 24, 2022
1 parent ff58598 commit 00f4f9e
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 95 deletions.
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

0 comments on commit 00f4f9e

Please sign in to comment.