Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ 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<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