public async Task ZeroByteReceive_CompletesWhenDataAvailable(Uri server) { using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { var rand = new Random(); var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds); // Do a 0-byte receive. It shouldn't complete yet. Task <WebSocketReceiveResult> t = ReceiveAsync(cws, new ArraySegment <byte>(Array.Empty <byte>()), ctsDefault.Token); Assert.False(t.IsCompleted); // Send a packet to the echo server. await SendAsync(cws, new ArraySegment <byte>(new byte[1] { 42 }), WebSocketMessageType.Binary, true, ctsDefault.Token); // Now the 0-byte receive should complete, but without reading any data. WebSocketReceiveResult r = await t; Assert.Equal(WebSocketMessageType.Binary, r.MessageType); Assert.Equal(0, r.Count); Assert.False(r.EndOfMessage); // Now do a receive to get the payload. var receiveBuffer = new byte[1]; t = ReceiveAsync(cws, new ArraySegment <byte>(receiveBuffer), ctsDefault.Token); Assert.Equal(TaskStatus.RanToCompletion, t.Status); r = await t; Assert.Equal(WebSocketMessageType.Binary, r.MessageType); Assert.Equal(1, r.Count); Assert.True(r.EndOfMessage); Assert.Equal(42, receiveBuffer[0]); // Clean up. await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, nameof(ZeroByteReceive_CompletesWhenDataAvailable), ctsDefault.Token); } }
public async Task SendAsync_SendCloseMessageType_ThrowsArgumentExceptionWithMessage(Uri server) { using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { var cts = new CancellationTokenSource(TimeOutMilliseconds); string expectedInnerMessage = ResourceHelper.GetExceptionMessage( "net_WebSockets_Argument_InvalidMessageType", "Close", "SendAsync", "Binary", "Text", "CloseOutputAsync"); var expectedException = new ArgumentException(expectedInnerMessage, "messageType"); string expectedMessage = expectedException.Message; Assert.Throws <ArgumentException>(() => { Task t = cws.SendAsync(new ArraySegment <byte>(), WebSocketMessageType.Close, true, cts.Token); }); Assert.Equal(WebSocketState.Open, cws.State); } }
public async Task CloseAsync_CloseOutputAsync_Throws(Uri server) { using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { var cts = new CancellationTokenSource(TimeOutMilliseconds); var closeStatus = WebSocketCloseStatus.NormalClosure; string closeDescription = null; await cws.CloseAsync(closeStatus, closeDescription, cts.Token); Assert.True( cws.State == WebSocketState.CloseSent || cws.State == WebSocketState.Closed, $"Expected CloseSent or Closed, got {cws.State}"); Assert.True(string.IsNullOrEmpty(cws.CloseStatusDescription)); await Assert.ThrowsAnyAsync <WebSocketException>(async() => { await cws.CloseOutputAsync(closeStatus, closeDescription, cts.Token); }); Assert.True( cws.State == WebSocketState.CloseSent || cws.State == WebSocketState.Closed, $"Expected CloseSent or Closed, got {cws.State}"); Assert.True(string.IsNullOrEmpty(cws.CloseStatusDescription)); } }
// This will also pass when no exception is thrown. Current implementation doesn't throw. public async Task ReceiveAsync_MultipleOutstandingReceiveOperations_Throws(Uri server) { using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output)) { var cts = new CancellationTokenSource(TimeOutMilliseconds); Task[] tasks = new Task[2]; await SendAsync( cws, WebSocketData.GetBufferFromText(".delay5sec"), WebSocketMessageType.Text, true, cts.Token); var recvBuffer = new byte[100]; var recvSegment = new ArraySegment <byte>(recvBuffer); try { for (int i = 0; i < tasks.Length; i++) { tasks[i] = ReceiveAsync(cws, recvSegment, cts.Token); } await Task.WhenAll(tasks); Assert.Equal(WebSocketState.Open, cws.State); } catch (Exception ex) { if (ex is InvalidOperationException) { Assert.Equal( ResourceHelper.GetExceptionMessage( "net_Websockets_AlreadyOneOutstandingOperation", "ReceiveAsync"), ex.Message); Assert.True(WebSocketState.Aborted == cws.State, cws.State + " state when InvalidOperationException"); } else if (ex is WebSocketException) { // Multiple cases. Assert.True(WebSocketState.Aborted == cws.State, cws.State + " state when WebSocketException"); WebSocketError errCode = (ex as WebSocketException).WebSocketErrorCode; Assert.True( (errCode == WebSocketError.InvalidState) || (errCode == WebSocketError.Success), "WebSocketErrorCode"); } else if (ex is OperationCanceledException) { Assert.True(WebSocketState.Aborted == cws.State, cws.State + " state when OperationCanceledException"); } else { Assert.True(false, "Unexpected exception: " + ex.Message); } } } }
public async Task EchoTextMessage_Success(Uri server) { await WebSocketHelper.TestEcho(server, WebSocketMessageType.Text, TimeOutMilliseconds, _output); }
protected Task TestEcho(Uri uri, WebSocketMessageType type, int timeOutMilliseconds, ITestOutputHelper output) => WebSocketHelper.TestEcho(uri, WebSocketMessageType.Text, TimeOutMilliseconds, _output, GetInvoker());
protected Task <ClientWebSocket> GetConnectedWebSocket(Uri uri, int TimeOutMilliseconds, ITestOutputHelper output) => WebSocketHelper.GetConnectedWebSocket(uri, TimeOutMilliseconds, output, invoker: GetInvoker());
public async Task EchoTextMessage_Success(Uri server) { var helper = new WebSocketHelper(server, s_TimeOutMilliseconds); await helper.TestEcho(WebSocketMessageType.Text); }