Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Correct tests.
  • Loading branch information
ilonatommy committed Mar 8, 2024
commit 42614a0c8ceadb136198283e4d0794550e5f8a35
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,14 @@ await socket.CloseAsync(
else if (receivedMessage == ".receiveMessageAfterClose")
{
byte[] buffer = new byte[1024];
string message1 = $"{receivedMessage} 1 {DateTime.Now.ToString("HH:mm:ss")}";
buffer = System.Text.Encoding.UTF8.GetBytes(message1);
var stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Server => Sending message: {message1}");
string message = $"{receivedMessage} {DateTime.Now.ToString("HH:mm:ss")}";
buffer = System.Text.Encoding.UTF8.GetBytes(message);
await socket.SendAsync(
new ArraySegment<byte>(buffer, 0, message1.Length),
new ArraySegment<byte>(buffer, 0, message.Length),
WebSocketMessageType.Text,
true,
CancellationToken.None);
stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Server => Closing the socket");
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, receivedMessage, CancellationToken.None);
stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Server => Socket got closed");
}
else if (socket.State == WebSocketState.Open)
{
Expand Down
46 changes: 34 additions & 12 deletions src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,31 @@ await cws.SendAsync(
[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CanReceiveAfterClose(Uri server)
{
await ReceiveMessageAfterClose(server, false);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CannotReceiveAfterCloseAndStateSync(Uri server)
{
try
{
await ReceiveMessageAfterClose(server, true);
}
catch (WebSocketException e)
{
Assert.Equal(WebSocketError.InvalidState, e.WebSocketErrorCode);
return;
}
catch (Exception e)
{
Assert.True(false, $"Unexpected exception: {e}");
}
Assert.True(false, "Expected WebSocketException not thrown.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Assert.Fail when CI passes.

}

private async Task ReceiveMessageAfterClose(Uri server, bool syncState)
{
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
Expand All @@ -380,23 +405,20 @@ await cws.SendAsync(
true,
cts.Token);

int delay = 2000;
var stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Client -> '.receiveMessageAfterClose' was sent, waiting {delay}ms. cws.State={cws.State}");
await Task.Delay(delay); // even if we decrease to 100, it still fails with
// System.Net.WebSockets.WebSocketException : The WebSocket is in an invalid state ('Closed') for this operation. Valid states are: 'Open, CloseSent'
// in the manual demo we can wait even 6 sec and the message arrives just fine
await Task.Delay(2000);

if (syncState)
{
var state = cws.State;
Assert.Equal(WebSocketState.Closed, state);
// should not be able to receive after this sync
}

stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Client -> Attempting to receive data... cws.State={cws.State}");
var recvBuffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult recvResult = await cws.ReceiveAsync(recvBuffer, cts.Token);
var message = Encoding.UTF8.GetString(recvBuffer.ToArray(), 0, recvResult.Count);

stamp = DateTime.Now.ToString("HH:mm:ss");
Console.WriteLine($"[{stamp}] Client -> Received message={message}");
Assert.Contains(".shutdownAfterTwoMessages 1", message);
Console.WriteLine($"cws.State={cws.State}");
Assert.Contains(".receiveMessageAfterClose", message);
}
}

Expand Down