From 5c6fb642a0fe382eb82ba3adf7179fd69640d9ac Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Tue, 22 Aug 2017 10:48:30 -0700 Subject: [PATCH] Fix flaky WebSocket test (#745) --- .../WebSocketsTransportTests.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/WebSocketsTransportTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/WebSocketsTransportTests.cs index 62ef0b3e91..b46f1757de 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/WebSocketsTransportTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/WebSocketsTransportTests.cs @@ -83,7 +83,19 @@ namespace Microsoft.AspNetCore.SignalR.Tests var sendTcs = new TaskCompletionSource(); connectionToTransport.Out.TryWrite(new SendMessage(new byte[] { 0x42 }, sendTcs)); - await sendTcs.Task; + try + { + await sendTcs.Task; + } + catch (OperationCanceledException) + { + // Because the server and client are run in the same process there is a race where websocket.SendAsync + // can send a message but before returning be suspended allowing the server to run the EchoEndpoint and + // send a close frame which triggers a cancellation token on the client and cancels the websocket.SendAsync. + // Our solution to this is to just catch OperationCanceledException from the sent message if the race happens + // because we know the send went through, and its safe to check the response. + } + // The echo endpoint closes the connection immediately after sending response which should stop the transport await webSocketsTransport.Running.OrTimeout();