public void ReconnectedClearsTimedOutAndHasBeenWarnedFlags()
        {
            var mockTransport = new Mock <IClientTransport>();

            mockTransport.Setup(t => t.SupportsKeepAlive).Returns(true);

            var mockConnection = new Mock <IConnection>();

            mockConnection.Setup(c => c.Transport).Returns(mockTransport.Object);
            mockConnection.Setup(c => c.KeepAliveData).Returns(new KeepAliveData(new TimeSpan(0, 0, 9)));
            mockConnection.Setup(c => c.State).Returns(ConnectionState.Connected);

            using (var monitor = new HeartbeatMonitor(mockConnection.Object, new object(), new TimeSpan(1, 0, 0)))
            {
                monitor.Start();
                // sets TimedOut flag
                monitor.Beat(new TimeSpan(0, 10, 0));
                // sets HasBeenWarned flag
                monitor.Beat(new TimeSpan(0, 0, 7));
                Assert.True(monitor.TimedOut);
                Assert.True(monitor.HasBeenWarned);

                monitor.Reconnected();
                Assert.False(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
            }
        }
        public void ReconnectedClearsTimedOutAndHasBeenWarnedFlags()
        {
            var mockTransport = new Mock<IClientTransport>();
            mockTransport.Setup(t => t.SupportsKeepAlive).Returns(true);

            var mockConnection = new Mock<IConnection>();
            mockConnection.Setup(c => c.Transport).Returns(mockTransport.Object);
            mockConnection.Setup(c => c.KeepAliveData).Returns(new KeepAliveData(new TimeSpan(0, 0, 9)));
            mockConnection.Setup(c => c.State).Returns(ConnectionState.Connected);

            using (var monitor = new HeartbeatMonitor(mockConnection.Object, new object(), new TimeSpan(1, 0, 0)))
            {
                monitor.Start();
                // sets TimedOut flag
                monitor.Beat(new TimeSpan(0, 10, 0));
                // sets HasBeenWarned flag
                monitor.Beat(new TimeSpan(0, 0, 7));
                Assert.True(monitor.TimedOut);
                Assert.True(monitor.HasBeenWarned);

                monitor.Reconnected();
                Assert.False(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
            }
        }
Esempio n. 3
0
        public void NormalConnectionTest()
        {
            // Arrange
            var connection = new Mock <Client.IConnection>();
            var transport  = new Mock <IClientTransport>();

            // Setting the values such that a timeout or timeout warning isn't issued
            var keepAliveData = new KeepAliveData(
                timeoutWarning: TimeSpan.FromSeconds(5),
                timeout: TimeSpan.FromSeconds(10),
                checkInterval: TimeSpan.FromSeconds(2)
                );

            using (var monitor = new HeartbeatMonitor(connection.Object, new object(), keepAliveData.CheckInterval))
            {
                connection.Setup(m => m.LastMessageAt).Returns(DateTime.UtcNow);
                connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
                connection.Setup(m => m.State).Returns(ConnectionState.Connected);
                connection.Setup(m => m.Transport).Returns(transport.Object);

                monitor.Start();

                // Act - Setting timespan to be less than timeout and timeout warning
                monitor.Beat(TimeSpan.FromSeconds(2));

                // Assert
                Assert.False(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
            }
        }
Esempio n. 4
0
        public void ConnectionTimeoutTest()
        {
            // Arrange
            var connection = new Mock <Client.IConnection>();
            var transport  = new Mock <IClientTransport>();

            transport.Setup(m => m.SupportsKeepAlive).Returns(true);

            // Setting the values such that a timeout happens almost instantly
            var keepAliveData = new KeepAliveData(
                timeoutWarning: TimeSpan.FromSeconds(10),
                timeout: TimeSpan.FromSeconds(1),
                checkInterval: TimeSpan.FromSeconds(2)
                );

            using (var monitor = new HeartbeatMonitor(connection.Object, new object(), keepAliveData.CheckInterval))
            {
                connection.Setup(m => m.LastMessageAt).Returns(DateTime.UtcNow);
                connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
                connection.Setup(m => m.State).Returns(ConnectionState.Connected);
                connection.Setup(m => m.Transport).Returns(transport.Object);

                monitor.Start();

                // Act - Setting timespan to be greater then timeout
                monitor.Beat(TimeSpan.FromSeconds(5));

                // Assert
                Assert.True(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
                transport.Verify(m => m.LostConnection(connection.Object), Times.Once());
            }
        }
        public void ConnectionTimeoutTest()
        {
            // Arrange
            var connection = new Mock<Client.IConnection>();
            var transport = new Mock<IClientTransport>();

            transport.Setup(m => m.SupportsKeepAlive).Returns(true);

            // Setting the values such that a timeout happens almost instantly
            var keepAliveData = new KeepAliveData(
                timeoutWarning: TimeSpan.FromSeconds(10),
                timeout: TimeSpan.FromSeconds(1),
                checkInterval: TimeSpan.FromSeconds(2)
            );

            using (var monitor = new HeartbeatMonitor(connection.Object, new object(), keepAliveData.CheckInterval))
            {
                connection.Setup(m => m.LastMessageAt).Returns(DateTime.UtcNow);
                connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
                connection.Setup(m => m.State).Returns(ConnectionState.Connected);
                connection.Setup(m => m.Transport).Returns(transport.Object);

                monitor.Start();

                // Act - Setting timespan to be greater then timeout
                monitor.Beat(TimeSpan.FromSeconds(5));

                // Assert
                Assert.True(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
                transport.Verify(m => m.LostConnection(connection.Object), Times.Once());
            }
        }
Esempio n. 6
0
        public void TimeoutWarningTest()
        {
            // Arrange
            var connection = new Mock <Client.IConnection>();
            var monitor    = new HeartbeatMonitor(connection.Object, new object());

            // Setting the values such that a warning is thrown almost instantly and a timeout doesn't occur
            var keepAliveData = new KeepAliveData(
                lastKeepAlive: DateTime.UtcNow,
                timeoutWarning: TimeSpan.FromSeconds(1),
                timeout: TimeSpan.FromSeconds(20),
                checkInterval: TimeSpan.FromSeconds(2)
                );

            connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
            connection.Setup(m => m.State).Returns(ConnectionState.Connected);

            // Act - Setting timespan to be greater than timeout warining but less than timeout
            monitor.Beat(TimeSpan.FromSeconds(5));

            // Assert
            Assert.True(monitor.HasBeenWarned);
            Assert.False(monitor.TimedOut);
            connection.Verify(m => m.OnConnectionSlow(), Times.Once());
        }
Esempio n. 7
0
        public void TimeoutWarningTest()
        {
            // Arrange
            var connection = new Mock<Client.IConnection>();
            var monitor = new HeartbeatMonitor(connection.Object, new object());

            // Setting the values such that a warning is thrown almost instantly and a timeout doesn't occur
            var keepAliveData = new KeepAliveData(
                lastKeepAlive: DateTime.UtcNow,
                timeoutWarning: TimeSpan.FromSeconds(1),
                timeout: TimeSpan.FromSeconds(20),
                checkInterval: TimeSpan.FromSeconds(2)
            );

            connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
            connection.Setup(m => m.State).Returns(ConnectionState.Connected);

            // Act - Setting timespan to be greater than timeout warining but less than timeout
            monitor.Beat(TimeSpan.FromSeconds(5));

            // Assert
            Assert.True(monitor.HasBeenWarned);
            Assert.False(monitor.TimedOut);
            connection.Verify(m => m.OnConnectionSlow(), Times.Once());
        }
        public void NormalConnectionTest()
        {
            // Arrange
            var connection = new Mock<Client.IConnection>();
            var transport = new Mock<IClientTransport>();

            // Setting the values such that a timeout or timeout warning isn't issued
            var keepAliveData = new KeepAliveData(
                timeoutWarning: TimeSpan.FromSeconds(5),
                timeout: TimeSpan.FromSeconds(10),
                checkInterval: TimeSpan.FromSeconds(2)
            );

            using (var monitor = new HeartbeatMonitor(connection.Object, new object(), keepAliveData.CheckInterval))
            {
                connection.Setup(m => m.LastMessageAt).Returns(DateTime.UtcNow);
                connection.Setup(m => m.KeepAliveData).Returns(keepAliveData);
                connection.Setup(m => m.State).Returns(ConnectionState.Connected);
                connection.Setup(m => m.Transport).Returns(transport.Object);

                monitor.Start();

                // Act - Setting timespan to be less than timeout and timeout warning
                monitor.Beat(TimeSpan.FromSeconds(2));

                // Assert
                Assert.False(monitor.TimedOut);
                Assert.False(monitor.HasBeenWarned);
            }
        }