Skip to content

Commit

Permalink
enable #53957 and #57519 (#72843)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jul 27, 2022
1 parent ac48faa commit c867be6
Showing 1 changed file with 41 additions and 69 deletions.
110 changes: 41 additions & 69 deletions src/libraries/System.Net.WebSockets.Client/tests/SendReceiveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,96 +324,68 @@ public async Task SendAsync_SendZeroLengthPayloadAsEndOfMessage_Success(Uri serv
[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<byte>(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<byte>(sendBuffer), WebSocketMessageType.Binary, true, ctsDefault.Token);

byte[] receiveBuffer = new byte[bufferSize];
int totalReceived = 0;
while (true)
{
WebSocketReceiveResult recvResult = await ReceiveAsync(
cws,
new ArraySegment<byte>(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<byte>(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<byte>(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<byte>(sendBuffer, receiveBuffer);
}
throw;

await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "SendReceive_VaryingLengthBuffers_Success", ctsDefault.Token);
}
}

[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[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<WebSocketReceiveResult> receive = ReceiveAsync(cws, new ArraySegment<byte>(receiveBuffer, receiveBuffer.Length - i - 1, 1), ctsDefault.Token);
Task send = SendAsync(cws, new ArraySegment<byte>(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<byte>(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<WebSocketReceiveResult> receive = ReceiveAsync(cws, new ArraySegment<byte>(receiveBuffer, receiveBuffer.Length - i - 1, 1), ctsDefault.Token);
Task send = SendAsync(cws, new ArraySegment<byte>(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<byte>(sendBuffer, receiveBuffer);
}
}

Expand Down

0 comments on commit c867be6

Please sign in to comment.