Skip to content

Commit

Permalink
[browser][ws] Do not duplicate code executed in local_on_close (dot…
Browse files Browse the repository at this point in the history
…net#99685)

* Do not duplicate code executed in "local_on_close".

* Fix.
  • Loading branch information
ilonatommy authored and matouskozak committed Apr 30, 2024
1 parent 7e4968a commit 54c597f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ public async Task CloseOutputAsync_ClientInitiated_CanReceive_CanClose(Uri serve

[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CanReceive(Uri server)
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServersWithSwitch))]
public async Task CloseOutputAsync_ServerInitiated_CanReceive(Uri server, bool delayReceiving)
{
var expectedCloseStatus = WebSocketCloseStatus.NormalClosure;
var expectedCloseDescription = ".shutdownafter";
Expand All @@ -280,6 +280,10 @@ await cws.SendAsync(
true,
cts.Token);

// let server close the output before we request receiving
if (delayReceiving)
await Task.Delay(1000);

// Should be able to receive the message echoed by the server.
var recvBuffer = new byte[100];
var segmentRecv = new ArraySegment<byte>(recvBuffer);
Expand Down Expand Up @@ -363,15 +367,15 @@ await cws.SendAsync(
}
}

public static IEnumerable<object[]> EchoServersSyncState =>
public static IEnumerable<object[]> EchoServersWithSwitch =>
EchoServers.SelectMany(server => new List<object[]>
{
new object[] { server[0], true },
new object[] { server[0], false }
});

[ActiveIssue("https://github.com/dotnet/runtime/issues/28957", typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServersSyncState))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServersWithSwitch))]
public async Task CloseOutputAsync_ServerInitiated_CanReceiveAfterClose(Uri server, bool syncState)
{
using (ClientWebSocket cws = await GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
Expand Down
5 changes: 2 additions & 3 deletions src/mono/browser/runtime/web-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function verifyEnvironment () {
}
}

export function ws_get_state (ws: WebSocketExtension) : number {
export function ws_get_state (ws: WebSocketExtension): number {
if (ws.readyState != WebSocket.CLOSED)
return ws.readyState ?? -1;
const receive_event_queue = ws[wasm_ws_pending_receive_event_queue];
Expand Down Expand Up @@ -228,8 +228,7 @@ export function ws_wasm_receive (ws: WebSocketExtension, buffer_ptr: VoidPtr, bu
return resolvedPromise();
}

const readyState = ws.readyState;
if (readyState == WebSocket.CLOSED) {
if (ws[wasm_ws_close_received]) {
const receive_status_ptr = ws[wasm_ws_receive_status_ptr];
setI32(receive_status_ptr, 0); // count
setI32(<any>receive_status_ptr + 4, 2); // type:close
Expand Down

0 comments on commit 54c597f

Please sign in to comment.