|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -using System.Collections.Generic; |
6 | | -using System.ComponentModel; |
7 | 5 | using System.Net.Sockets; |
8 | 6 | using System.Net.Test.Common; |
9 | 7 | using System.Threading; |
@@ -442,5 +440,42 @@ await LoopbackServer.CreateServerAsync(async (server, url) => |
442 | 440 | } |
443 | 441 | }, options); |
444 | 442 | } |
| 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 | + } |
445 | 480 | } |
446 | 481 | } |
0 commit comments