-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved to wait helper classes with locking and better protection for r…
…ace conditions
- Loading branch information
Showing
16 changed files
with
712 additions
and
644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
568 changes: 67 additions & 501 deletions
568
src/bunit.core/Extensions/WaitForExtensions/RenderWaitingHelperExtensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/bunit.core/Extensions/WaitForExtensions/WaitForAssertionHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bunit | ||
{ | ||
|
||
public class WaitForAssertionHelper : IDisposable | ||
{ | ||
private readonly IRenderedFragmentBase _renderedFragment; | ||
private readonly Action _assertion; | ||
private readonly Timer _timer; | ||
private readonly ILogger _logger; | ||
private readonly TaskCompletionSource<object?> _completionSouce; | ||
private bool _disposed = false; | ||
private Exception? _capturedException; | ||
|
||
public Task WaitTask => _completionSouce.Task; | ||
|
||
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)); | ||
_renderedFragment.OnAfterRender += TryAssertion; | ||
TryAssertion(); | ||
} | ||
|
||
void TryAssertion() | ||
{ | ||
if (_disposed) | ||
return; | ||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
_logger.LogDebug(new EventId(1, nameof(TryAssertion)), $"Trying the assertion for component {_renderedFragment.ComponentId}"); | ||
|
||
try | ||
{ | ||
_assertion(); | ||
_capturedException = null; | ||
_completionSouce.TrySetResult(null); | ||
_logger.LogDebug(new EventId(2, nameof(TryAssertion)), $"The assertion for component {_renderedFragment.ComponentId} passed"); | ||
Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogDebug(new EventId(3, nameof(TryAssertion)), $"The assertion for component {_renderedFragment.ComponentId} did not pass. The error message was '{ex.Message}'"); | ||
_capturedException = ex; | ||
} | ||
} | ||
} | ||
|
||
void HandleTimeout(object state) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
_logger.LogDebug(new EventId(5, nameof(HandleTimeout)), $"The assertion wait helper for component {_renderedFragment.ComponentId} timed out"); | ||
|
||
var error = new WaitForAssertionFailedException(_capturedException); | ||
_completionSouce.TrySetException(error); | ||
|
||
Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the wait helper and sets the <see cref="WaitTask"/> to canceled, if it is not | ||
/// already in one of the other completed states. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
_disposed = true; | ||
_renderedFragment.OnAfterRender -= TryAssertion; | ||
_timer.Dispose(); | ||
_logger.LogDebug(new EventId(6, nameof(Dispose)), $"The state wait helper for component {_renderedFragment.ComponentId} disposed"); | ||
_completionSouce.TrySetCanceled(); | ||
} | ||
|
||
private static ILogger<T> GetLogger<T>(IServiceProvider services) | ||
{ | ||
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance; | ||
return loggerFactory.CreateLogger<T>(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/bunit.core/Extensions/WaitForExtensions/WaitForContextAssertionHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bunit | ||
{ | ||
public class WaitForContextAssertionHelper : IDisposable | ||
{ | ||
private readonly ITestContext _testContext; | ||
private readonly Action _assertion; | ||
private readonly Timer _timer; | ||
private readonly ILogger _logger; | ||
private readonly TaskCompletionSource<object?> _completionSouce; | ||
private bool _disposed = false; | ||
private Exception? _capturedException; | ||
|
||
public Task WaitTask => _completionSouce.Task; | ||
|
||
public WaitForContextAssertionHelper(ITestContext testContext, Action assertion, TimeSpan? timeout = null) | ||
{ | ||
_logger = GetLogger<WaitForContextAssertionHelper>(testContext.Services); | ||
_completionSouce = new TaskCompletionSource<object?>(); | ||
_testContext = testContext; | ||
_assertion = assertion; | ||
_timer = new Timer(HandleTimeout, this, timeout.GetRuntimeTimeout(), TimeSpan.FromMilliseconds(Timeout.Infinite)); | ||
_testContext.OnAfterRender += TryAssertion; | ||
TryAssertion(); | ||
} | ||
|
||
void TryAssertion() | ||
{ | ||
if (_disposed) | ||
return; | ||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
_logger.LogDebug(new EventId(1, nameof(TryAssertion)), $"Trying the assertion for the test context"); | ||
|
||
try | ||
{ | ||
_assertion(); | ||
_capturedException = null; | ||
_completionSouce.TrySetResult(null); | ||
_logger.LogDebug(new EventId(2, nameof(TryAssertion)), $"The assertion for the test context passed"); | ||
Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogDebug(new EventId(3, nameof(TryAssertion)), $"The assertion for the test context did not pass. The error message was '{ex.Message}'"); | ||
_capturedException = ex; | ||
} | ||
} | ||
} | ||
|
||
void HandleTimeout(object state) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
_logger.LogDebug(new EventId(5, nameof(HandleTimeout)), $"The assertion wait helper for the test context timed out"); | ||
|
||
var error = new WaitForAssertionFailedException(_capturedException); | ||
_completionSouce.TrySetException(error); | ||
|
||
Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the wait helper and sets the <see cref="WaitTask"/> to canceled, if it is not | ||
/// already in one of the other completed states. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
_disposed = true; | ||
_testContext.OnAfterRender -= TryAssertion; | ||
_timer.Dispose(); | ||
_logger.LogDebug(new EventId(6, nameof(Dispose)), $"The state wait helper for the test context disposed"); | ||
_completionSouce.TrySetCanceled(); | ||
} | ||
|
||
private static ILogger<T> GetLogger<T>(IServiceProvider services) | ||
{ | ||
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance; | ||
return loggerFactory.CreateLogger<T>(); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/bunit.core/Extensions/WaitForExtensions/WaitForContextStateHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bunit | ||
{ | ||
public class WaitForContextStateHelper : IDisposable | ||
{ | ||
private readonly ITestContext _testContext; | ||
private readonly Func<bool> _statePredicate; | ||
private readonly Timer _timer; | ||
private readonly ILogger _logger; | ||
private readonly TaskCompletionSource<bool> _completionSouce; | ||
private bool _disposed = false; | ||
private Exception? _capturedException; | ||
|
||
public Task WaitTask => _completionSouce.Task; | ||
|
||
public WaitForContextStateHelper(ITestContext testContext, Func<bool> statePredicate, TimeSpan? timeout = null) | ||
{ | ||
_logger = GetLogger<WaitForContextStateHelper>(testContext.Services); | ||
_completionSouce = new TaskCompletionSource<bool>(); | ||
_testContext = testContext; | ||
_statePredicate = statePredicate; | ||
_timer = new Timer(HandleTimeout, this, timeout.GetRuntimeTimeout(), TimeSpan.FromMilliseconds(Timeout.Infinite)); | ||
_testContext.OnAfterRender += TryPredicate; | ||
TryPredicate(); | ||
} | ||
|
||
void TryPredicate() | ||
{ | ||
if (_disposed) | ||
return; | ||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
_logger.LogDebug(new EventId(1, nameof(TryPredicate)), $"Trying the state predicate for the test context"); | ||
|
||
try | ||
{ | ||
var result = _statePredicate(); | ||
if (result) | ||
{ | ||
_logger.LogDebug(new EventId(2, nameof(TryPredicate)), $"The state predicate for the test context"); | ||
_completionSouce.TrySetResult(result); | ||
|
||
Dispose(); | ||
} | ||
else | ||
{ | ||
_logger.LogDebug(new EventId(3, nameof(TryPredicate)), $"The state predicate for the test context did not pass"); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogDebug(new EventId(4, nameof(TryPredicate)), $"The state predicate for the test context throw an exception '{ex.GetType().Name}' with message '{ex.Message}'"); | ||
_capturedException = ex; | ||
} | ||
} | ||
} | ||
|
||
void HandleTimeout(object state) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
lock (_completionSouce) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
_logger.LogDebug(new EventId(5, nameof(HandleTimeout)), $"The state wait helper for the test context timed out"); | ||
|
||
var error = new WaitForStateFailedException(WaitForStateFailedException.TIMEOUT_BEFORE_PASS, _capturedException); | ||
_completionSouce.TrySetException(error); | ||
|
||
Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disposes the wait helper and sets the <see cref="WaitTask"/> to canceled, if it is not | ||
/// already in one of the other completed states. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
_disposed = true; | ||
_testContext.OnAfterRender -= TryPredicate; | ||
_timer.Dispose(); | ||
_logger.LogDebug(new EventId(6, nameof(Dispose)), $"The state wait helper for the test context disposed"); | ||
_completionSouce.TrySetCanceled(); | ||
} | ||
|
||
private static ILogger<T> GetLogger<T>(IServiceProvider services) | ||
{ | ||
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance; | ||
return loggerFactory.CreateLogger<T>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.