Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        public void DisposeUnRegistersForPingResponse()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.UnRegisterForMessage(MqttMessageType.PingResponse, It.IsAny <Func <MqttMessage, bool> >()));

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

            ka.Dispose();

            chMock.VerifyAll();
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        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();
        }