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

AwaitAssert() over AwaitAssertAsync() #5683

Merged
merged 3 commits into from
Feb 24, 2022
Merged
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
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
Arkatufus marked this conversation as resolved.
Show resolved Hide resolved
{
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