diff --git a/src/Components/Components/test/RendererTest.cs b/src/Components/Components/test/RendererTest.cs index 0a170b5ca2..5eedeb6630 100644 --- a/src/Components/Components/test/RendererTest.cs +++ b/src/Components/Components/test/RendererTest.cs @@ -2566,8 +2566,15 @@ namespace Microsoft.AspNetCore.Components.Test // Act/Assert 3: After we complete the first UI update in which a given // event handler ID is disposed, we can no longer reuse that event handler ID + + // From here we can't see when the async disposal is completed. Just give it plenty of time (Task.Yield isn't enough). + // There is a small chance in which the continuations from TaskCompletionSource run asynchronously. + // In that case we might not be able to see the results from RemoveEventHandlerIds as they might run asynchronously. + // For that case, we are going to queue a continuation on render1TCS.Task, include a 1s delay and await the resulting + // task to offer the best chance that we get to see the error in all cases. + var awaitableTask = render1TCS.Task.ContinueWith(_ => Task.Delay(1000)).Unwrap(); render1TCS.SetResult(null); - await Task.Delay(500); // From here we can't see when the async disposal is completed. Just give it plenty of time (Task.Yield isn't enough). + await awaitableTask; var ex = await Assert.ThrowsAsync(() => { return renderer.DispatchEventAsync(eventHandlerId, new UIEventArgs());