From 0397441a1a058d49277d6be8ecdbf179f737ebec Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 26 Jul 2022 12:41:55 +0200 Subject: [PATCH] enable https://github.com/dotnet/runtime/issues/53957 and https://github.com/dotnet/runtime/issues/57519 --- .../tests/SendReceiveTest.cs | 110 +++++++----------- 1 file changed, 41 insertions(+), 69 deletions(-) diff --git a/src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.cs b/src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.cs index 1c94c0460479b7..63e59b07e8cd06 100644 --- a/src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.cs +++ b/src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.cs @@ -324,53 +324,39 @@ await SendAsync( [ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))] public async Task SendReceive_VaryingLengthBuffers_Success(Uri server) { - CancellationTokenSource ctsDefault = null; - try + using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { - using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) + var rand = new Random(); + var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds); + + // Values chosen close to boundaries in websockets message length handling as well + // as in vectors used in mask application. + foreach (int bufferSize in new int[] { 1, 3, 4, 5, 31, 32, 33, 125, 126, 127, 128, ushort.MaxValue - 1, ushort.MaxValue, ushort.MaxValue + 1, ushort.MaxValue * 2 }) { - var rand = new Random(); - ctsDefault = new CancellationTokenSource(TimeOutMilliseconds); + byte[] sendBuffer = new byte[bufferSize]; + rand.NextBytes(sendBuffer); + await SendAsync(cws, new ArraySegment(sendBuffer), WebSocketMessageType.Binary, true, ctsDefault.Token); - // Values chosen close to boundaries in websockets message length handling as well - // as in vectors used in mask application. - foreach (int bufferSize in new int[] { 1, 3, 4, 5, 31, 32, 33, 125, 126, 127, 128, ushort.MaxValue - 1, ushort.MaxValue, ushort.MaxValue + 1, ushort.MaxValue * 2 }) + byte[] receiveBuffer = new byte[bufferSize]; + int totalReceived = 0; + while (true) { - byte[] sendBuffer = new byte[bufferSize]; - rand.NextBytes(sendBuffer); - await SendAsync(cws, new ArraySegment(sendBuffer), WebSocketMessageType.Binary, true, ctsDefault.Token); - - byte[] receiveBuffer = new byte[bufferSize]; - int totalReceived = 0; - while (true) - { - WebSocketReceiveResult recvResult = await ReceiveAsync( - cws, - new ArraySegment(receiveBuffer, totalReceived, receiveBuffer.Length - totalReceived), - ctsDefault.Token); - - Assert.InRange(recvResult.Count, 0, receiveBuffer.Length - totalReceived); - totalReceived += recvResult.Count; + WebSocketReceiveResult recvResult = await ReceiveAsync( + cws, + new ArraySegment(receiveBuffer, totalReceived, receiveBuffer.Length - totalReceived), + ctsDefault.Token); - if (recvResult.EndOfMessage) break; - } + Assert.InRange(recvResult.Count, 0, receiveBuffer.Length - totalReceived); + totalReceived += recvResult.Count; - Assert.Equal(receiveBuffer.Length, totalReceived); - Assert.Equal(sendBuffer, receiveBuffer); + if (recvResult.EndOfMessage) break; } - await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "SendReceive_VaryingLengthBuffers_Success", ctsDefault.Token); - } - } - catch (OperationCanceledException ex) - { - if (PlatformDetection.IsBrowser && ctsDefault != null && ex.CancellationToken == ctsDefault.Token) - { - _output.WriteLine($"ActiveIssue https://github.com/dotnet/runtime/issues/53957"); - _output.WriteLine($"The test {nameof(SendReceive_VaryingLengthBuffers_Success)} took more than {TimeOutMilliseconds} to finish, it was canceled."); - return; + Assert.Equal(receiveBuffer.Length, totalReceived); + Assert.Equal(sendBuffer, receiveBuffer); } - throw; + + await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "SendReceive_VaryingLengthBuffers_Success", ctsDefault.Token); } } @@ -378,42 +364,28 @@ public async Task SendReceive_VaryingLengthBuffers_Success(Uri server) [ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))] public async Task SendReceive_Concurrent_Success(Uri server) { - CancellationTokenSource ctsDefault = null; - try + using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { - using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) - { - ctsDefault = new CancellationTokenSource(TimeOutMilliseconds); - - byte[] receiveBuffer = new byte[10]; - byte[] sendBuffer = new byte[10]; - for (int i = 0; i < sendBuffer.Length; i++) - { - sendBuffer[i] = (byte)i; - } - - for (int i = 0; i < sendBuffer.Length; i++) - { - Task receive = ReceiveAsync(cws, new ArraySegment(receiveBuffer, receiveBuffer.Length - i - 1, 1), ctsDefault.Token); - Task send = SendAsync(cws, new ArraySegment(sendBuffer, i, 1), WebSocketMessageType.Binary, true, ctsDefault.Token); - await Task.WhenAll(receive, send); - Assert.Equal(1, receive.Result.Count); - } - await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "SendReceive_Concurrent_Success", ctsDefault.Token); + CancellationTokenSource ctsDefault = new CancellationTokenSource(TimeOutMilliseconds); - Array.Reverse(receiveBuffer); - Assert.Equal(sendBuffer, receiveBuffer); + byte[] receiveBuffer = new byte[10]; + byte[] sendBuffer = new byte[10]; + for (int i = 0; i < sendBuffer.Length; i++) + { + sendBuffer[i] = (byte)i; } - } - catch (OperationCanceledException ex) - { - if (PlatformDetection.IsBrowser && ctsDefault != null && ex.CancellationToken == ctsDefault.Token) + + for (int i = 0; i < sendBuffer.Length; i++) { - _output.WriteLine($"ActiveIssue https://github.com/dotnet/runtime/issues/57519"); - _output.WriteLine($"The test {nameof(SendReceive_Concurrent_Success)} took more than {TimeOutMilliseconds} to finish, it was canceled."); - return; + Task receive = ReceiveAsync(cws, new ArraySegment(receiveBuffer, receiveBuffer.Length - i - 1, 1), ctsDefault.Token); + Task send = SendAsync(cws, new ArraySegment(sendBuffer, i, 1), WebSocketMessageType.Binary, true, ctsDefault.Token); + await Task.WhenAll(receive, send); + Assert.Equal(1, receive.Result.Count); } - throw; + await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "SendReceive_Concurrent_Success", ctsDefault.Token); + + Array.Reverse(receiveBuffer); + Assert.Equal(sendBuffer, receiveBuffer); } }