Skip to content

Commit b9b8849

Browse files
Change Receive Test Methods to Sync over Async (#5678)
* Changed to sync over async * * Change Peek methods to sync over async * Create Peek `async` mthods * Change FishForMessage() to sync over async that calls FishForMessageAsync() * Inherit doc from `FishForMessage` * Fix .Wait() returns AggregatedException instead of expected exxception Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent 22ec797 commit b9b8849

File tree

2 files changed

+89
-14
lines changed

2 files changed

+89
-14
lines changed

src/core/Akka.TestKit/Internal/AsyncQueue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13+
using Nito.AsyncEx.Synchronous;
1314

1415
namespace Akka.TestKit.Internal
1516
{
@@ -19,7 +20,7 @@ public class AsyncQueue<T>: ITestQueue<T> where T: class
1920

2021
public int Count => _collection.Count;
2122

22-
public void Enqueue(T item) => EnqueueAsync(item).AsTask().Wait();
23+
public void Enqueue(T item) => EnqueueAsync(item).AsTask().WaitAndUnwrapException();
2324

2425
public ValueTask EnqueueAsync(T item) => new ValueTask(_collection.AddAsync(item));
2526

src/core/Akka.TestKit/TestKitBase_Receive.cs

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading;
1313
using System.Threading.Tasks;
1414
using Akka.TestKit.Internal;
15+
using Nito.AsyncEx.Synchronous;
1516

1617
namespace Akka.TestKit
1718
{
@@ -58,14 +59,22 @@ public T FishForMessage<T>(Predicate<T> isMessage, TimeSpan? max = null, string
5859
/// <param name="allMessages">If null then will be ignored. If not null then will be initially cleared, then filled with all the messages until <paramref name="isMessage"/> returns <c>true</c></param>
5960
/// <returns>Returns the message that <paramref name="isMessage"/> matched</returns>
6061
public T FishForMessage<T>(Predicate<T> isMessage, ArrayList allMessages, TimeSpan? max = null, string hint = "")
62+
{
63+
var task = FishForMessageAsync<T>(isMessage, allMessages, max, hint).AsTask();
64+
task.WaitAndUnwrapException();
65+
return task.Result;
66+
}
67+
68+
/// <inheritdoc cref="FishForMessage{T}(Predicate{T}, ArrayList, TimeSpan?, string)"/>
69+
public async ValueTask<T> FishForMessageAsync<T>(Predicate<T> isMessage, ArrayList allMessages, TimeSpan? max = null, string hint = "")
6170
{
6271
var maxValue = RemainingOrDilated(max);
6372
var end = Now + maxValue;
6473
allMessages?.Clear();
6574
while (true)
6675
{
6776
var left = end - Now;
68-
var msg = ReceiveOne(left);
77+
var msg = await ReceiveOneAsync(left).ConfigureAwait(false);
6978
_assertions.AssertTrue(msg != null, "Timeout ({0}) during fishForMessage{1}", maxValue, string.IsNullOrEmpty(hint) ? "" : ", hint: " + hint);
7079
if (msg is T msg1 && isMessage(msg1))
7180
{
@@ -138,9 +147,20 @@ public async Task<ArrayList> WaitForRadioSilenceAsync(TimeSpan? max = null, uint
138147
/// <returns>The message if one was received; <c>null</c> otherwise</returns>
139148
public object ReceiveOne(TimeSpan? max = null)
140149
{
141-
MessageEnvelope envelope;
142-
if (TryReceiveOne(out envelope, max, CancellationToken.None))
143-
return envelope.Message;
150+
var task = ReceiveOneAsync(max).AsTask();
151+
task.WaitAndUnwrapException();
152+
var received = task.Result;
153+
return received;
154+
}
155+
156+
/// <inheritdoc cref="ReceiveOne(TimeSpan?)"/>
157+
public async ValueTask<object> ReceiveOneAsync(TimeSpan? max = null)
158+
{
159+
var received = await TryReceiveOneAsync(max, CancellationToken.None);
160+
161+
if (received.success)
162+
return received.envelope.Message;
163+
144164
return null;
145165
}
146166

@@ -152,9 +172,19 @@ public object ReceiveOne(TimeSpan? max = null)
152172
/// <returns>The message if one was received; <c>null</c> otherwise</returns>
153173
public object ReceiveOne(CancellationToken cancellationToken)
154174
{
155-
MessageEnvelope envelope;
156-
if (TryReceiveOne(out envelope, Timeout.InfiniteTimeSpan, cancellationToken))
157-
return envelope.Message;
175+
var task = ReceiveOneAsync(cancellationToken).AsTask();
176+
task.WaitAndUnwrapException();
177+
var received = task.Result;
178+
return received;
179+
}
180+
/// <inheritdoc cref="ReceiveOne(CancellationToken)"/>
181+
public async ValueTask<object> ReceiveOneAsync(CancellationToken cancellationToken)
182+
{
183+
var received = await TryReceiveOneAsync(Timeout.InfiniteTimeSpan, cancellationToken);
184+
185+
if (received.success)
186+
return received.envelope.Message;
187+
158188
return null;
159189
}
160190

@@ -177,6 +207,12 @@ public bool TryReceiveOne(out MessageEnvelope envelope, TimeSpan? max = null)
177207
return TryReceiveOne(out envelope, max, CancellationToken.None);
178208
}
179209

210+
/// <inheritdoc cref="TryReceiveOne(out MessageEnvelope, TimeSpan?)"/>
211+
public async ValueTask<(bool success, MessageEnvelope envelope)> TryReceiveOneAsync(TimeSpan? max = null)
212+
{
213+
return await TryReceiveOneAsync(max, CancellationToken.None).ConfigureAwait(false);
214+
}
215+
180216
/// <summary>
181217
/// Receive one message from the internal queue of the TestActor within
182218
/// the specified duration.
@@ -200,10 +236,16 @@ public bool TryReceiveOne(out MessageEnvelope envelope, TimeSpan? max, Cancellat
200236
return InternalTryReceiveOne(out envelope, max, cancellationToken, true);
201237
}
202238

239+
/// <inheritdoc cref="TryReceiveOne(out MessageEnvelope, TimeSpan?, CancellationToken)"/>
240+
public async ValueTask<(bool success, MessageEnvelope envelope)> TryReceiveOneAsync(TimeSpan? max, CancellationToken cancellationToken)
241+
{
242+
return await InternalTryReceiveOneAsync(max, cancellationToken, true).ConfigureAwait(false);
243+
}
244+
203245
private bool InternalTryReceiveOne(out MessageEnvelope envelope, TimeSpan? max, CancellationToken cancellationToken, bool shouldLog)
204246
{
205247
var task = InternalTryReceiveOneAsync(max, cancellationToken, shouldLog).AsTask();
206-
task.Wait();
248+
task.WaitAndUnwrapException();
207249
var received = task.Result;
208250
envelope = received.envelope;
209251
return received.success;
@@ -268,8 +310,18 @@ private bool InternalTryReceiveOne(out MessageEnvelope envelope, TimeSpan? max,
268310
/// <returns>The message if one was received; <c>null</c> otherwise</returns>
269311
public object PeekOne(TimeSpan? max = null)
270312
{
271-
if (InternalTryPeekOne(out var envelope, max, CancellationToken.None, true))
272-
return envelope.Message;
313+
var task = PeekOneAsync(max).AsTask();
314+
task.WaitAndUnwrapException();
315+
var peeked = task.Result;
316+
return peeked;
317+
}
318+
319+
/// <inheritdoc cref="PeekOne(TimeSpan?)"/>
320+
public async ValueTask<object> PeekOneAsync(TimeSpan? max = null)
321+
{
322+
var peeked = await TryPeekOneAsync(max, CancellationToken.None);
323+
if (peeked.success)
324+
return peeked.envelope.Message;
273325
return null;
274326
}
275327

@@ -281,8 +333,18 @@ public object PeekOne(TimeSpan? max = null)
281333
/// <returns>The message if one was received; <c>null</c> otherwise</returns>
282334
public object PeekOne(CancellationToken cancellationToken)
283335
{
284-
if (InternalTryPeekOne(out var envelope, Timeout.InfiniteTimeSpan, cancellationToken, true))
285-
return envelope.Message;
336+
var task = PeekOneAsync(cancellationToken).AsTask();
337+
task.WaitAndUnwrapException();
338+
var peeked = task.Result;
339+
return peeked;
340+
}
341+
342+
/// <inheritdoc cref="PeekOne(CancellationToken)"/>
343+
public async ValueTask<object> PeekOneAsync(CancellationToken cancellationToken)
344+
{
345+
var peeked = await TryPeekOneAsync(Timeout.InfiniteTimeSpan, cancellationToken);
346+
if (peeked.success)
347+
return peeked.envelope.Message;
286348
return null;
287349
}
288350

@@ -304,6 +366,12 @@ public bool TryPeekOne(out MessageEnvelope envelope, TimeSpan? max = null)
304366
return InternalTryPeekOne(out envelope, max, CancellationToken.None, true);
305367
}
306368

369+
/// <inheritdoc cref="TryPeekOne(out MessageEnvelope, TimeSpan?)"/>
370+
public async ValueTask<(bool success, MessageEnvelope envelope)> TryPeekOneAsync(TimeSpan? max = null)
371+
{
372+
return await InternalTryPeekOneAsync(max, CancellationToken.None, true);
373+
}
374+
307375
/// <summary>
308376
/// Peek one message from the head of the internal queue of the TestActor within
309377
/// the specified duration.
@@ -327,10 +395,16 @@ public bool TryPeekOne(out MessageEnvelope envelope, TimeSpan? max, Cancellation
327395
return InternalTryPeekOne(out envelope, max, cancellationToken, true);
328396
}
329397

398+
/// <inheritdoc cref="TryPeekOne(out MessageEnvelope, TimeSpan?, CancellationToken)"/>
399+
public async ValueTask<(bool success, MessageEnvelope envelope)> TryPeekOneAsync(TimeSpan? max, CancellationToken cancellationToken)
400+
{
401+
return await InternalTryPeekOneAsync(max, cancellationToken, true);
402+
}
403+
330404
private bool InternalTryPeekOne(out MessageEnvelope envelope, TimeSpan? max, CancellationToken cancellationToken, bool shouldLog)
331405
{
332406
var task = InternalTryPeekOneAsync(max, cancellationToken, shouldLog).AsTask();
333-
task.Wait();
407+
task.WaitAndUnwrapException();
334408
var received = task.Result;
335409
envelope = received.envelope;
336410
return received.success;

0 commit comments

Comments
 (0)