Beispiel #1
0
        public async Task Throws_When_Initial_Connect_Fails()
        {
            var connectParams = new ConnectParameters();

            // Make the initial connect fail.
            _transportConnectorMock.Setup(c => c.ConnectAsync(It.IsAny <CancellationToken>())).Throws <TimeoutException>();

            var rfbConnection = new RfbConnection(_protocolMock.Object, new NullLoggerFactory(), connectParams);

            // Start should throw.
            await Assert.ThrowsAsync <TimeoutException>(() => rfbConnection.StartAsync());

            // Connection should still be uninitialized
            Assert.Equal(ConnectionState.Uninitialized, rfbConnection.ConnectionState);
        }
Beispiel #2
0
        public async Task Updates_ConnectionState_On_Connect_Reconnect_Close()
        {
            var connectParams = new ConnectParameters {
                ReconnectDelay       = TimeSpan.FromSeconds(1),
                MaxReconnectAttempts = 1
            };

            var rfbConnection = new RfbConnection(_protocolMock.Object, new NullLoggerFactory(), connectParams);

            Assert.Equal(ConnectionState.Uninitialized, rfbConnection.ConnectionState);

            // Start connection.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => rfbConnection.StartAsync());

            Assert.Equal(ConnectionState.Connected, rfbConnection.ConnectionState);

            // Receive and send loops should have been started.
            _messageReceiverMock.Verify(receiver => receiver.StartReceiveLoop());
            _messageSenderMock.Verify(sender => sender.StartSendLoop());

            // Status should update when connection is interrupted
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => {
                // Let's simulate a failure
                _messageReceiverMock.Raise(receiver => receiver.Failed += null, new BackgroundThreadFailedEventArgs(new Exception("Shit happens.")));

                // Wait a bit because the event handler might run on a different thread
                return(Task.Delay(500));
            });

            Assert.Equal(ConnectionState.Interrupted, rfbConnection.ConnectionState);

            // Reconnect should succeed after 1 second.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => Task.Delay(TimeSpan.FromSeconds(1.5)));

            Assert.Equal(ConnectionState.Connected, rfbConnection.ConnectionState);

            // Close connection.
            await Assert.PropertyChangedAsync(rfbConnection, nameof(rfbConnection.ConnectionState), () => rfbConnection.CloseAsync());

            Assert.Equal(ConnectionState.Closed, rfbConnection.ConnectionState);
        }