Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 21947fb

Browse files
authored
Add WebSocket test for zero-byte receives (#28579)
1 parent 9685e1d commit 21947fb

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/System.Net.WebSockets.Client/tests/SendReceiveTest.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Collections.Generic;
6-
using System.ComponentModel;
75
using System.Net.Sockets;
86
using System.Net.Test.Common;
97
using System.Threading;
@@ -442,5 +440,42 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
442440
}
443441
}, options);
444442
}
443+
444+
[OuterLoop] // TODO: Issue #11345
445+
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
446+
public async Task ZeroByteReceive_CompletesWhenDataAvailable(Uri server)
447+
{
448+
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
449+
{
450+
var rand = new Random();
451+
var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds);
452+
453+
// Do a 0-byte receive. It shouldn't complete yet.
454+
Task<WebSocketReceiveResult> t = ReceiveAsync(cws, new ArraySegment<byte>(Array.Empty<byte>()), ctsDefault.Token);
455+
Assert.False(t.IsCompleted);
456+
457+
// Send a packet to the echo server.
458+
await SendAsync(cws, new ArraySegment<byte>(new byte[1] { 42 }), WebSocketMessageType.Binary, true, ctsDefault.Token);
459+
460+
// Now the 0-byte receive should complete, but without reading any data.
461+
WebSocketReceiveResult r = await t;
462+
Assert.Equal(WebSocketMessageType.Binary, r.MessageType);
463+
Assert.Equal(0, r.Count);
464+
Assert.False(r.EndOfMessage);
465+
466+
// Now do a receive to get the payload.
467+
var receiveBuffer = new byte[1];
468+
t = ReceiveAsync(cws, new ArraySegment<byte>(receiveBuffer), ctsDefault.Token);
469+
Assert.Equal(TaskStatus.RanToCompletion, t.Status);
470+
r = await t;
471+
Assert.Equal(WebSocketMessageType.Binary, r.MessageType);
472+
Assert.Equal(1, r.Count);
473+
Assert.True(r.EndOfMessage);
474+
Assert.Equal(42, receiveBuffer[0]);
475+
476+
// Clean up.
477+
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, nameof(ZeroByteReceive_CompletesWhenDataAvailable), ctsDefault.Token);
478+
}
479+
}
445480
}
446481
}

0 commit comments

Comments
 (0)