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

[TEST] Change Within methods to async #5701

Merged
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
46 changes: 46 additions & 0 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/WithinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Sdk;
using static FluentAssertions.FluentActions;

namespace Akka.TestKit.Tests.TestKitBaseTests
{
Expand All @@ -17,5 +23,45 @@ public void Within_should_increase_max_timeout_by_the_provided_epsilon_value()
{
Within(TimeSpan.FromSeconds(1), () => ExpectNoMsg(), TimeSpan.FromMilliseconds(50));
}

[Fact]
public void Within_should_respect_minimum_time()
{
Within(0.3.Seconds(), 1.Seconds(), () => ExpectNoMsg(0.4.Seconds()), "", 0.1.Seconds());
}

[Fact]
public async Task WithinAsync_should_respect_minimum_time()
{
await WithinAsync(
0.3.Seconds(),
1.Seconds(),
async () => await ExpectNoMsgAsync(0.4.Seconds()),
"",
0.1.Seconds());
}

[Fact]
public void Within_should_throw_if_execution_is_shorter_than_minimum_time()
{
Invoking(() =>
{
Within(0.5.Seconds(), 1.Seconds(), () => ExpectNoMsg(0.1.Seconds()), null, 0.1.Seconds());
}).Should().Throw<XunitException>();
}

[Fact]
public async Task WithinAsync_should_throw_if_execution_is_shorter_than_minimum_time()
{
await Awaiting(async () =>
{
await WithinAsync(
0.5.Seconds(),
1.Seconds(),
async () => await ExpectNoMsgAsync(0.1.Seconds()),
null,
0.1.Seconds());
}).Should().ThrowAsync<XunitException>();
}
}
}
4 changes: 2 additions & 2 deletions src/core/Akka.TestKit/TestKitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public void SetAutoPilot(AutoPilot pilot)
/// <summary>
/// <para>
/// Retrieves the time remaining for execution of the innermost enclosing
/// <see cref="Within(TimeSpan, Action, TimeSpan?)">Within</see> block.
/// <see cref="Within(TimeSpan, Action, TimeSpan?, CancellationToken)">Within</see> block.
/// If missing that, then it returns the properly dilated default for this
/// case from settings (key: "akka.test.single-expect-default").
/// </para>
Expand All @@ -378,7 +378,7 @@ public TimeSpan RemainingOrDefault
/// <summary>
/// <para>
/// Retrieves the time remaining for execution of the innermost enclosing
/// <see cref="Within(TimeSpan, Action, TimeSpan?)">Within</see> block.
/// <see cref="Within(TimeSpan, Action, TimeSpan?, CancellationToken)">Within</see> block.
/// </para>
/// <remarks>The returned value is always finite.</remarks>
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/core/Akka.TestKit/TestKitBase_AwaitAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public abstract partial class TestKitBase
/// <param name="cancellationToken"></param>
public void AwaitAssert(Action assertion, TimeSpan? duration=null, TimeSpan? interval=null, CancellationToken cancellationToken = default)
{
var task = AwaitAssertAsync(assertion, duration, interval, cancellationToken);
task.WaitAndUnwrapException();
AwaitAssertAsync(assertion, duration, interval, cancellationToken)
.WaitAndUnwrapException();
Copy link
Member

Choose a reason for hiding this comment

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

LGTM

}

/// <inheritdoc cref="AwaitAssert(Action, TimeSpan?, TimeSpan?, CancellationToken)"/>
Expand All @@ -53,6 +53,7 @@ public async Task AwaitAssertAsync(Action assertion, TimeSpan? duration=null, Ti
cancellationToken.ThrowIfCancellationRequested();
try
{
// TODO: assertion can run forever, need a way to stop this if this happens.
Copy link
Member

Choose a reason for hiding this comment

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

Probably isn't a good way to do this without cancellation support or some crazy detached task execution structure. Better to let the test runner just hang and make it the users' problem to debug.

assertion();
return;
}
Expand Down
Loading