Skip to content

Commit

Permalink
Removed test context waitfor functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed May 7, 2020
1 parent ff48f3d commit 0969f9d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,17 @@ namespace Bunit
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>")]
public static class RenderWaitingHelperExtensions
{
/// <summary>
/// Wait for the next render to happen, or the <paramref name="timeout"/> is reached (default is one second).
/// If a <paramref name="renderTrigger"/> action is provided, it is invoked before the waiting.
/// </summary>
/// <param name="testContext">The test context to wait for renders from.</param>
/// <param name="renderTrigger">The action that somehow causes one or more components to render.</param>
/// <param name="timeout">The maximum time to wait for the next render. If not provided the default is 1 second. During debugging, the timeout is automatically set to infinite.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="testContext"/> is null.</exception>
/// <exception cref="WaitForRenderFailedException">Thrown if no render happens within the specified <paramref name="timeout"/>, or the default of 1 second, if non is specified.</exception>
[Obsolete("Use either the WaitForState or WaitForAssertion method instead. It will make your test more resilient to insignificant changes, as they will wait across multiple renders instead of just one. To make the change, run any render trigger first, then call either WaitForState or WaitForAssertion with the appropriate input. This method will be removed before the 1.0.0 release.", false)]
public static void WaitForNextRender(this ITestContext testContext, Action? renderTrigger = null, TimeSpan? timeout = null)
{
using var waiter = new WaitForRenderHelper(testContext, 1, timeout);
try
{
renderTrigger?.Invoke();
waiter.WaitTask.Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}
}

/// <summary>
/// Wait until the provided <paramref name="statePredicate"/> action returns true,
/// or the <paramref name="timeout"/> is reached (default is one second).
///
/// The <paramref name="statePredicate"/> is evaluated initially, and then each time
/// the renderer in the <paramref name="testContext"/> renders.
/// the <paramref name="renderedFragment"/> renders.
/// </summary>
/// <param name="testContext">The test context to wait for renders from.</param>
/// <param name="statePredicate">The predicate to invoke after each render, which returns true when the desired state has been reached.</param>
/// <param name="renderedFragment">The render fragment or component to attempt to verify state against.</param>
/// <param name="statePredicate">The predicate to invoke after each render, which must returns <c>true</c> when the desired state has been reached.</param>
/// <param name="timeout">The maximum time to wait for the desired state.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="testContext"/> is null.</exception>
/// <exception cref="WaitForStateFailedException">Thrown if the <paramref name="statePredicate"/> throw an exception during invocation, or if the timeout has been reached. See the inner exception for details.</exception>
public static void WaitForState(this ITestContext testContext, Func<bool> statePredicate, TimeSpan? timeout = null)
{
using var waiter = new WaitForContextStateHelper(testContext, statePredicate, timeout);
try
{
waiter.WaitTask.Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}
}

/// <summary>
/// Wait until the provided <paramref name="assertion"/> action passes (i.e. does not throw an
/// assertion exception), or the <paramref name="timeout"/> is reached (default is one second).
///
/// The <paramref name="assertion"/> is attempted initially, and then each time
/// the renderer in the <paramref name="testContext"/> renders.
/// </summary>
/// <param name="testContext">The test context to wait for renders from.</param>
/// <param name="assertion">The verification or assertion to perform.</param>
/// <param name="timeout">The maximum time to attempt the verification.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="testContext"/> is null.</exception>
/// <exception cref="WaitForAssertionFailedException">Thrown if the timeout has been reached. See the inner exception to see the captured assertion exception.</exception>
public static void WaitForAssertion(this ITestContext testContext, Action assertion, TimeSpan? timeout = null)
{
using var waiter = new WaitForContextAssertionHelper(testContext, assertion, timeout);
try
{
waiter.WaitTask.Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}
}

public static void WaitForState(this IRenderedFragmentBase renderedFragment, Func<bool> statePredicate, TimeSpan? timeout = null)
{
using var waiter = new WaitForStateHelper(renderedFragment, statePredicate, timeout);
Expand All @@ -106,16 +43,14 @@ public static void WaitForState(this IRenderedFragmentBase renderedFragment, Fun
}

/// <summary>
/// Wait until the provided <paramref name="assertion"/> action passes (i.e. does not throw an
/// assertion exception), or the <paramref name="timeout"/> is reached (default is one second).
/// Wait until the provided <paramref name="assertion"/> passes (i.e. does not throw an
/// exception), or the <paramref name="timeout"/> is reached (default is one second).
///
/// The <paramref name="assertion"/> is attempted initially, and then each time
/// the <paramref name="renderedFragment"/> renders.
/// The <paramref name="assertion"/> is attempted initially, and then each time the <paramref name="renderedFragment"/> renders.
/// </summary>
/// <param name="renderedFragment">The rendered fragment to wait for renders from.</param>
/// <param name="renderedFragment">The rendered fragment to wait for renders from and assert against.</param>
/// <param name="assertion">The verification or assertion to perform.</param>
/// <param name="timeout">The maximum time to attempt the verification.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="renderedFragment"/> is null.</exception>
/// <exception cref="WaitForAssertionFailedException">Thrown if the timeout has been reached. See the inner exception to see the captured assertion exception.</exception>
public static void WaitForAssertion(this IRenderedFragmentBase renderedFragment, Action assertion, TimeSpan? timeout = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Bunit
{

/// <summary>
/// Represents an async wait helper, that will wait for a specified time for an assertion to pass.
/// </summary>
public class WaitForAssertionHelper : IDisposable
{
private readonly IRenderedFragmentBase _renderedFragment;
Expand All @@ -18,15 +20,30 @@ public class WaitForAssertionHelper : IDisposable
private bool _disposed = false;
private Exception? _capturedException;

/// <summary>
/// Gets the task that will complete successfully if the assertion passed before the timeout was reached.
/// The task will complete with an <see cref="WaitForAssertionFailedException"/> exception if the timeout was reached without the assertion passing,
/// and will have the assertion exception as its InnerException.
/// </summary>
public Task WaitTask => _completionSouce.Task;

/// <summary>
/// Creates an instance of the <see cref="WaitForAssertionHelper"/> type,
/// which will until the provided <paramref name="assertion"/> passes (i.e. does not throw an
/// exception), or the <paramref name="timeout"/> is reached (default is one second).
///
/// The <paramref name="assertion"/> is attempted initially, and then each time the <paramref name="renderedFragment"/> renders.
/// </summary>
/// <param name="renderedFragment">The rendered fragment to wait for renders from and assert against.</param>
/// <param name="assertion">The verification or assertion to perform.</param>
/// <param name="timeout">The maximum time to attempt the verification.</param>
public WaitForAssertionHelper(IRenderedFragmentBase renderedFragment, Action assertion, TimeSpan? timeout = null)
{
_logger = GetLogger<WaitForAssertionHelper>(renderedFragment.Services);
_completionSouce = new TaskCompletionSource<object?>();
_renderedFragment = renderedFragment;
_assertion = assertion;
_timer = new Timer(HandleTimeout, this, timeout.GetRuntimeTimeout(), TimeSpan.FromMilliseconds(Timeout.Infinite));
_timer = new Timer(HandleTimeout, this, timeout.GetRuntimeTimeout(), Timeout.InfiniteTimeSpan);
_renderedFragment.OnAfterRender += TryAssertion;
TryAssertion();
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0969f9d

Please sign in to comment.