From 1a6ba7a3a49ce30493a226cd54c3a8c2500f3390 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Sat, 19 Sep 2026 15:02:37 +0300 Subject: [PATCH] Make WebSocket admission and shutdown tests observe asynchronous closure --- Tests/Unit/AspNetCoreIntegrationTests.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Tests/Unit/AspNetCoreIntegrationTests.cs b/Tests/Unit/AspNetCoreIntegrationTests.cs index ff608e3..36fdce5 100644 --- a/Tests/Unit/AspNetCoreIntegrationTests.cs +++ b/Tests/Unit/AspNetCoreIntegrationTests.cs @@ -334,8 +334,16 @@ public sealed class AspNetCoreIntegrationTests { // The HTTP upgrade can complete before the EP admission decision closes the // second transport. What matters is that it is never added to the server. - if (await rejectedSocket.Connect(host.WebSocketAddress, cancellation.Token)) - rejectedSocket.Begin(); + try + { + if (await rejectedSocket.Connect(host.WebSocketAddress, cancellation.Token)) + rejectedSocket.Begin(); + } + catch (AsyncException exception) when (exception.InnerException is WebSocketException) + { + // A fast admission rejection can abort the HTTP upgrade itself. + // It must still close this socket and preserve the first peer. + } await WaitUntilAsync( () => rejectedSocket.State == SocketState.Closed, @@ -403,6 +411,9 @@ public sealed class AspNetCoreIntegrationTests await host.Application.StopAsync(cancellation.Token); + // Host shutdown closes the server transport; the remote receive loop + // observes that close asynchronously on its own scheduler. + await socket.Completion.WaitAsync(cancellation.Token); Assert.True(socket.Completion.IsCompleted); Assert.Equal(SocketState.Closed, socket.State); Assert.Empty(host.Server.Connections);