public void CtorRegistersForAllSentMessages()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.RegisterForAllSentMessages(It.IsAny<Func<MqttMessage, bool>>()));

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 60);

            chMock.VerifyAll();
            ka.Dispose();
        }
        public void DisposeUnRegistersForPingRequest()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.UnRegisterForMessage(MqttMessageType.PingRequest, It.IsAny<Func<MqttMessage, bool>>()));

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 60);
            ka.Dispose();

            chMock.VerifyAll();
        }
Example #3
0
        /// <summary>
        ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (keepAlive != null)
            {
                keepAlive.Dispose();
            }

            if (subscriptionsManager != null)
            {
                subscriptionsManager.Dispose();
            }

            if (messageLogger != null)
            {
                messageLogger.Dispose();
            }

            if (connectionHandler != null)
            {
                connectionHandler.Dispose();
            }
        }
Example #4
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (keepAlive != null)
            {
                keepAlive.Dispose();
            }

            if (subscriptionsManager != null)
            {
                subscriptionsManager.Dispose();
            }

            if (messageLogger != null)
            {
                messageLogger.Dispose();
            }

            if (connectionHandler != null)
            {
                connectionHandler.Dispose();
            }

            GC.SuppressFinalize(this);
        }
        public void PingRequestFromBrokerCausesPingResponse()
        {
            Func<MqttMessage, bool> pingReqCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingResponseMessage>()));
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.PingRequest, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pingReqCallback = cb);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            pingReqCallback(new MqttPingRequestMessage());
            
            ka.Dispose();

            // verify that we did actually send a ping request.
            chMock.VerifyAll();
        }
        public void PingReceiveDoesNotStopPingRequest()
        {
            Func<MqttMessage, bool> pingRespCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send

            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.PingResponse, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pingRespCallback = cb);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            System.Threading.Thread.Sleep(800);
            pingRespCallback(new MqttPingResponseMessage()); // fake that we received a ping response from the broker.
            System.Threading.Thread.Sleep(300);

            ka.Dispose();

            // verify that we did actually send a ping request.
            chMock.VerifyAll();
        }
        public void SendPingRequestDoesNotOccurWhenOtherMessageSent()
        {
            Func<MqttMessage, bool> sendCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send

            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));
            chMock.Setup((x) => x.RegisterForAllSentMessages(It.IsAny < Func<MqttMessage, bool>>()))
                .Callback((Func<MqttMessage, bool> regSendCallback) => sendCallback = regSendCallback);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            System.Threading.Thread.Sleep(800);
            sendCallback(new MqttSubscribeAckMessage()); // fake that something was sent
            System.Threading.Thread.Sleep(300);

            // 1.3s should have passed, and we should not have had a send operation occur because we faked
            // a send operation to the callback.
            // Ie. SendMessage (first expectation we set on the mock) should not have been called.

            ka.Dispose();

            // verify it wasn't called.
            chMock.Verify((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()), Times.Never());
        }
        public void SendInactivityCausesPingRequest()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send
            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));

            // initiate the keepalive connection, then sleep for a few ms, to allow the timer to execute
            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 0);
            System.Threading.Thread.Sleep(1000);

            chMock.VerifyAll();
            ka.Dispose();
        }