Skip to content
Closed
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
44 changes: 31 additions & 13 deletions src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

namespace Akka.TestKit
{
/// <summary>
/// TBD
/// </summary>
public abstract partial class TestKitBase
{
/// <summary>
Expand All @@ -37,7 +34,7 @@ public abstract partial class TestKitBase
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
AwaitAssertAsync(assertion, duration, interval, cancellationToken)
.WaitAndUnwrapException();
.WaitAndUnwrapException(cancellationToken);
}

/// <inheritdoc cref="AwaitAssert(Action, TimeSpan?, TimeSpan?, CancellationToken)"/>
Expand All @@ -62,16 +59,19 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti
}
catch(Exception)
{
var stopped = Now + t;
if (stopped >= stop)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that we don't get 1 last chance to test the assertion if the first attempt failed and took too long

{
Sys.Log.Warning("AwaitAssert failed, timeout [{0}] is over after [{1}] attempts and [{2}] elapsed time", max, attempts, stopped - start);
throw;
}

// Check timeout after delay to avoid check-then-act race condition
}
attempts++;
await Task.Delay(t, cancellationToken);

// Check if we've exceeded the timeout AFTER sleeping
if (Now > stop)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, after the delay has been completed, even if we're overdue on time we still check one final time on exit.

{
Sys.Log.Warning("AwaitAssert failed, timeout [{0}] is over after [{1}] attempts and [{2}] elapsed time", max, attempts, Now - start);
// Re-run the assertion one final time to get the actual exception
assertion();
}

t = (stop - Now).Min(intervalValue);
}
}
Expand All @@ -96,9 +96,11 @@ public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null
var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100));
if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue;
intervalValue.EnsureIsPositiveFinite("interval");
var start = Now;
var max = RemainingOrDilated(duration);
var stop = Now + max;
var t = max.Min(intervalValue);
var attempts = 0;
while(true)
{
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -109,10 +111,26 @@ public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null
}
catch(Exception)
{
if(Now + t >= stop)
throw;
// Check timeout after delay to avoid check-then-act race condition
}
attempts++;
await Task.Delay(t, cancellationToken);

// Check if we've exceeded the timeout AFTER sleeping
if (Now > stop)
{
try
{
await assertion();
return;
}
catch (Exception ex)
{
Sys.Log.Warning(ex, "AwaitAssert failed, timeout [{0}] is over after [{1}] attempts and [{2}] elapsed time", max, attempts, Now - start);
throw;
}
}
Comment on lines 120 to 132
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only problem is that this code logs a fail warning regardless if the extra assertion invocation failed or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should remove that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some other errors with this code too, like if the final assertion succeeded we'd end up running again


t = (stop - Now).Min(intervalValue);
}
}
Expand Down
Loading