Пример #1
0
    public async Task TestDisposeAsyncShouldNotThrowExceptionsOnError()
    {
        // ARRANGE
        WsMock.Setup(n =>
                     n.CloseAsync(It.IsAny <WebSocketCloseStatus>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
        .Throws(new ApplicationException("what ever"));

        //ACT AND ASSERT
        try
        {
            await DefaultPipeline.DisposeAsync().ConfigureAwait(false);
        }
        catch (Exception)
        {
            Assert.True(false, "DisposeAsync should not throw exception");
        }
    }
Пример #2
0
    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>()));
    }