Skip to content
Open
Show file tree
Hide file tree
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/DilatedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task ReceiveNAsync_should_dilate_timeout()
{
var stopwatch = Stopwatch.StartNew();
await Awaiting(async () => await ReceiveNAsync(42, TimeSpan.FromMilliseconds(Timeout)).ToListAsync())
.Should().ThrowAsync<TrueException>();
.Should().ThrowAsync<FailException>();
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}
Expand All @@ -60,7 +60,7 @@ public async Task ExpectMsgAllOfAsync_should_dilate_timeout()
{
var stopwatch = Stopwatch.StartNew();
await Awaiting(async () => await ExpectMsgAllOfAsync(TimeSpan.FromMilliseconds(Timeout), new []{ "1", "2" }).ToListAsync())
.Should().ThrowAsync<TrueException>();
.Should().ThrowAsync<FailException>();
stopwatch.Stop();
AssertDilated(stopwatch.ElapsedMilliseconds, $"Expected the timeout to be {ExpectedTimeout} but in fact it was {stopwatch.ElapsedMilliseconds}.");
}
Expand Down
1,343 changes: 697 additions & 646 deletions src/core/Akka.TestKit/TestKitBase.cs

Large diffs are not rendered by default.

638 changes: 319 additions & 319 deletions src/core/Akka.TestKit/TestKitBase_ActorOf.cs

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,109 +12,109 @@
using Akka.TestKit.Internal;
using Nito.AsyncEx.Synchronous;

namespace Akka.TestKit
#nullable enable
namespace Akka.TestKit;

/// <summary>
/// TBD
/// </summary>
public abstract partial class TestKitBase
{
/// <summary>
/// TBD
/// <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>
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>
/// <param name="cancellationToken"></param>
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
/// <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>
/// <param name="cancellationToken"></param>
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
AwaitAssertAsync(assertion, duration, interval, cancellationToken)
.WaitAndUnwrapException();
}
AwaitAssertAsync(assertion, duration, interval, cancellationToken)
.WaitAndUnwrapException(cancellationToken);
}

/// <inheritdoc cref="AwaitAssert(Action, TimeSpan?, TimeSpan?, CancellationToken)"/>
public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
/// <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;
intervalValue.EnsureIsPositiveFinite(nameof(interval));
var start = Now;
var max = RemainingOrDilated(duration);
var stop = Now + max;
var t = max.Min(intervalValue);
var attempts = 0;
while(true)
{
var intervalValue = interval.GetValueOrDefault(TimeSpan.FromMilliseconds(100));
if(intervalValue == Timeout.InfiniteTimeSpan) intervalValue = TimeSpan.MaxValue;
intervalValue.EnsureIsPositiveFinite(nameof(interval));
var start = Now;
var max = RemainingOrDilated(duration);
var stop = Now + max;
var t = max.Min(intervalValue);
var attempts = 0;
while(true)
cancellationToken.ThrowIfCancellationRequested();
try
{
cancellationToken.ThrowIfCancellationRequested();
try
// TODO: assertion can run forever, need a way to stop this if this happens.
assertion();
return;
}
catch(Exception)
{
var stopped = Now + t;
if (stopped >= stop)
{
// TODO: assertion can run forever, need a way to stop this if this happens.
assertion();
return;
Sys.Log.Warning("AwaitAssert failed, timeout [{0}] is over after [{1}] attempts and [{2}] elapsed time", max, attempts, stopped - start);
throw;
}
catch(Exception)
{
var stopped = Now + t;
if (stopped >= stop)
{
Sys.Log.Warning("AwaitAssert failed, timeout [{0}] is over after [{1}] attempts and [{2}] elapsed time", max, attempts, stopped - start);
throw;
}

}
attempts++;
await Task.Delay(t, cancellationToken);
t = (stop - Now).Min(intervalValue);
}
attempts++;
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
/// 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>
/// <param name="cancellationToken"></param>
public async Task AwaitAssertAsync(Func<Task> assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
/// <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>
/// <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;
intervalValue.EnsureIsPositiveFinite("interval");
var max = RemainingOrDilated(duration);
var stop = Now + max;
var t = max.Min(intervalValue);
while(true)
{
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)
cancellationToken.ThrowIfCancellationRequested();
try
{
cancellationToken.ThrowIfCancellationRequested();
try
{
await assertion();
return;
}
catch(Exception)
{
if(Now + t >= stop)
throw;
}
await Task.Delay(t, cancellationToken);
t = (stop - Now).Min(intervalValue);
await assertion();
return;
}
catch(Exception)
{
if(Now + t >= stop)
throw;
}
await Task.Delay(t, cancellationToken);
t = (stop - Now).Min(intervalValue);
}
}
}
}
Loading
Loading