public async Task TestGetNextMessageAsyncOnClosedSocketShouldCastException() { // ARRANGE WsMock.SetupGet(n => n.State).Returns(WebSocketState.Closed); // ACT AND ASSERT await Assert.ThrowsAsync <ApplicationException>(async() => await DefaultPipeline.GetNextMessageAsync <HassMessage>(CancellationToken.None).ConfigureAwait(false)); }
public async Task TestSendMessageAsyncOnClosedSocketShouldTrowException() { // ARRANGE WsMock.SetupGet(n => n.State).Returns(WebSocketState.Closed); // ACT AND ASSERT await Assert.ThrowsAsync <ApplicationException>(async() => await DefaultPipeline.SendMessageAsync <object>(new { test = "test" }, CancellationToken.None) .ConfigureAwait(false)); }
public async Task TestGetNextMessageAsyncOnRemoteClosingWebsocketShouldThrowException() { // ARRANGE WsMock.Setup(n => n.ReceiveAsync(It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>())) .ReturnsAsync((Memory <byte> _, CancellationToken _) => { // Simulate a close from remote WsMock.SetupGet(n => n.State).Returns(WebSocketState.CloseReceived); return(new ValueWebSocketReceiveResult(0, WebSocketMessageType.Close, true)); }); // ACT AND ASSERT // The operation should be cancelled when remote closes websocket await Assert.ThrowsAsync <OperationCanceledException>(async() => await DefaultPipeline.GetNextMessageAsync <HassMessage>(CancellationToken.None).ConfigureAwait(false)); // CloseOutput should always be called when // a close frame are sent from the remote websocket WsMock.Verify(n => n.CloseOutputAsync(It.IsAny <WebSocketCloseStatus>(), It.IsAny <string>(), It.IsAny <CancellationToken>())); }