Skip to content

Commit

Permalink
AwaitAssert() over AwaitAssertAsync() (#5683)
Browse files Browse the repository at this point in the history
* Converted `AwaitAssert()` to sync over async and called `AwaitAssertAsync()`

* Added CancellationToken support

* Prefer `ThrowIfCancellationRequested()` over `IsCancellationRequested`
  • Loading branch information
eaba authored Feb 24, 2022
1 parent b9b8849 commit 540da87
Showing 1 changed file with 14 additions and 41 deletions.
55 changes: 14 additions & 41 deletions src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Akka.TestKit.Internal;
using Nito.AsyncEx.Synchronous;

namespace Akka.TestKit
{
Expand All @@ -31,46 +32,15 @@ public abstract partial class TestKitBase
/// <param name="assertion">The action.</param>
/// <param name="duration">The timeout.</param>
/// <param name="interval">The interval to wait between executing the assertion.</param>
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null)
/// <param name="cancellationToken"></param>
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100));
if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue;
intervalValue.EnsureIsPositiveFinite("interval");
var max = RemainingOrDilated(duration);
var stop = Now + max;
var t = max.Min(intervalValue);
while(true)
{
try
{
assertion();
return;
}
catch(Exception)
{
if(Now + t >= stop)
throw;
}
Thread.Sleep(t);
t = (stop - Now).Min(intervalValue);
}
var task = AwaitAssertAsync(assertion, duration, interval, cancellationToken);
task.WaitAndUnwrapException();
}

/// <summary>
/// <para>Await until the given assertion does not throw an exception or the timeout
/// expires, whichever comes first. If the timeout expires the last exception
/// is thrown.</para>
/// <para>The action is called, and if it throws an exception the thread sleeps
/// the specified interval before retrying.</para>
/// <para>If no timeout is given, take it from the innermost enclosing `within`
/// block.</para>
/// <para>Note that the timeout is scaled using <see cref="Dilated" />,
/// which uses the configuration entry "akka.test.timefactor".</para>
/// </summary>
/// <param name="assertion">The action.</param>
/// <param name="duration">The timeout.</param>
/// <param name="interval">The interval to wait between executing the assertion.</param>
public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null)
/// <inheritdoc cref="AwaitAssert(Action, TimeSpan?, TimeSpan?, CancellationToken)"/>
public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100));
if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue;
Expand All @@ -80,6 +50,7 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti
var t = max.Min(intervalValue);
while(true)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
assertion();
Expand All @@ -90,11 +61,11 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti
if(Now + t >= stop)
throw;
}
await Task.Delay(t);
await Task.Delay(t, cancellationToken);
t = (stop - Now).Min(intervalValue);
}
}

/// <summary>
/// <para>Await until the given assertion does not throw an exception or the timeout
/// expires, whichever comes first. If the timeout expires the last exception
Expand All @@ -109,7 +80,8 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti
/// <param name="assertion">The action.</param>
/// <param name="duration">The timeout.</param>
/// <param name="interval">The interval to wait between executing the assertion.</param>
public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null, TimeSpan? interval=null)
/// <param name="cancellationToken"></param>
public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100));
if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue;
Expand All @@ -119,6 +91,7 @@ public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null
var t = max.Min(intervalValue);
while(true)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await assertion();
Expand All @@ -129,7 +102,7 @@ public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null
if(Now + t >= stop)
throw;
}
await Task.Delay(t);
await Task.Delay(t, cancellationToken);
t = (stop - Now).Min(intervalValue);
}
}
Expand Down

0 comments on commit 540da87

Please sign in to comment.