From 67a7cf21e9d982e775d1735f2bd653fcdcc0442c Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 29 Jul 2026 16:06:26 +1000 Subject: [PATCH] Fix race in Razor dispatcher test Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2e8856e4-2e41-47cb-90c0-2afb0c5be773 --- .../DefaultRequestDispatcherTest.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/Microsoft.NET.Sdk.Razor.Tool.Tests/DefaultRequestDispatcherTest.cs b/test/Microsoft.NET.Sdk.Razor.Tool.Tests/DefaultRequestDispatcherTest.cs index 5cb1fbe5eea8..4b94d6b736cb 100644 --- a/test/Microsoft.NET.Sdk.Razor.Tool.Tests/DefaultRequestDispatcherTest.cs +++ b/test/Microsoft.NET.Sdk.Razor.Tool.Tests/DefaultRequestDispatcherTest.cs @@ -340,7 +340,8 @@ public async Task Dispatcher_ProcessSimultaneousConnections_HitsKeepAliveTimeout { // Arrange var totalCount = 2; - var readySource = new TaskCompletionSource(); + var timeout = TimeSpan.FromMinutes(1); + var readySource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var list = new List>(); var connectionHost = new Mock(); connectionHost @@ -349,7 +350,7 @@ public async Task Dispatcher_ProcessSimultaneousConnections_HitsKeepAliveTimeout { if (list.Count < totalCount) { - var source = new TaskCompletionSource(); + var source = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var connectionTask = CreateConnectionWithEmptyServerRequest(c => { // Keep the connection active until we decide to end it. @@ -373,10 +374,10 @@ public async Task Dispatcher_ProcessSimultaneousConnections_HitsKeepAliveTimeout var eventBus = new TestableEventBus(); var completedCompilations = 0; - var allCompilationsComplete = new TaskCompletionSource(); + var allCompilationsComplete = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); eventBus.CompilationComplete += (obj, args) => { - if (++completedCompilations == totalCount) + if (Interlocked.Increment(ref completedCompilations) == totalCount) { // All compilations have completed. allCompilationsComplete.SetResult(true); @@ -391,10 +392,10 @@ public async Task Dispatcher_ProcessSimultaneousConnections_HitsKeepAliveTimeout }, TestContext.CancellationToken); // Wait for all connections to be created. - await readySource.Task; + await readySource.Task.WaitAsync(timeout, TestContext.CancellationToken); // Wait for all compilations to complete. - await allCompilationsComplete.Task; + await allCompilationsComplete.Task.WaitAsync(timeout, TestContext.CancellationToken); // Now allow all the connections to be disconnected. foreach (var source in list) @@ -404,7 +405,7 @@ public async Task Dispatcher_ProcessSimultaneousConnections_HitsKeepAliveTimeout // Act // Now dispatcher should be in an idle state with no active connections. - await dispatcherTask; + await dispatcherTask.WaitAsync(timeout, TestContext.CancellationToken); // Assert Assert.IsFalse(eventBus.HasDetectedBadConnection);