Skip to content

Commit

Permalink
refactor: use async/await for dispatcher and async continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 21, 2022
1 parent 9aa2a5d commit 88de4e7
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/bunit.core/Extensions/WaitForHelpers/WaitForHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected WaitForHelper(
this.completeChecker = completeChecker ?? throw new ArgumentNullException(nameof(completeChecker));

logger = renderedFragment.Services.CreateLogger<WaitForHelper<T>>();
checkPassedCompletionSource = new TaskCompletionSource<T>();
checkPassedCompletionSource = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
timer = new Timer(_ =>
{
logger.LogWaiterTimedOut(renderedFragment.ComponentId);
Expand Down Expand Up @@ -121,17 +121,12 @@ private Task<T> CreateWaitTask(IRenderedFragmentBase renderedFragment)

// Two to failure conditions, that the renderer captures an unhandled
// exception from a component or itself, or that the timeout is reached,
// are executed on the renderes scheduler, to ensure that OnAfterRender
// are executed on the renderers scheduler, to ensure that OnAfterRender
// and the continuations does not happen at the same time.
var failureTask = renderer.Dispatcher.InvokeAsync(() =>
var failureTask = renderer.Dispatcher.InvokeAsync(async () =>
{
return renderer
.UnhandledException
.ContinueWith(
x => Task.FromException<T>(x.Result),
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.FromCurrentSynchronizationContext());
var exception = await renderer.UnhandledException;
return Task.FromException<T>(exception);
}).Unwrap();

return Task
Expand Down

0 comments on commit 88de4e7

Please sign in to comment.