Esempio n. 1
0
        public void ReleasedMessageAtQos2RemovesMessageFromStorage()
        {
            Func <MqttMessage, bool> pubCallback    = null;
            Func <MqttMessage, bool> pubRelCallback = null;
            short msgId = 1;

            var mock = new Mock <IMqttConnectionHandler>();

            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType mt, Func <MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType mt, Func <MqttMessage, bool> cb) => pubRelCallback = cb);
            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            // fake a publish from a remote party and verify we sent a received back.
            pubCallback(new MqttPublishMessage()
                        .WithMessageIdentifier(msgId)
                        .ToTopic("A/Topic")
                        .WithQos(MqttQos.ExactlyOnce)
                        .PublishData(new byte[] { 0, 1, 2 }));
            var rcvdMsgs = GetReceivedMessages(pm);

            Assert.True(rcvdMsgs.ContainsKey(msgId));

            // fake the publish release message from remote party and verify that a publishcomplete is sent.
            pubRelCallback(new MqttPublishReleaseMessage().WithMessageIdentifier(msgId));
            Assert.False(rcvdMsgs.ContainsKey(msgId));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the received messages inside a publishing manager.
        /// </summary>
        /// <param name="pubMgr"></param>
        /// <returns></returns>
        private Dictionary <int, MqttPublishMessage> GetReceivedMessages(Nmqtt.PublishingManager pubMgr)
        {
            // we need to crack open the publishing manager and access some privates
            FieldInfo fi = typeof(Nmqtt.PublishingManager).GetField("receivedMessages", ReflectionBindingConstants.NonPublicField);

            return((Dictionary <int, MqttPublishMessage>)fi.GetValue(pubMgr));
        }
Esempio n. 3
0
        /// <summary>
        /// Performs a synchronous connect to the message broker with an optional username and password
        /// for the purposes of authentication.
        /// </summary>
        /// <param name="username">Optionally the username to authenticate as.</param>
        /// <param name="password">Optionally the password to authenticate with.</param>
        public ConnectionState Connect(string username = null, string password = null)
        {
            Log.Debug(m => m("Initiating connection to broker '{0}', port '{1}' using Client Identifier '{2}'",
                             server, port, clientIdentifier));

            if (username != null)
            {
                Log.Info(m => m("Authenticating with username '{0}' and password '{0}'", username, password));
                if (username.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Username length ({0}) exceeds the max recommended in the MQTT spec. ", username.Trim().Length));
                }
                if (password != null && password.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Password length ({0}) exceeds the max recommended in the MQTT spec. ", password.Trim().Length));
                }
            }

            var connectMessage = GetConnectMessage(username, password);

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            publishingManager    = new PublishingManager(connectionHandler);
            subscriptionsManager = new SubscriptionsManager(connectionHandler, publishingManager);
            messageLogger        = new MessageLogger(connectionHandler);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
Esempio n. 4
0
        public void CtorRegistersForPublisCompletehMessages()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny<Func<MqttMessage, bool>>()));

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; });

            chMock.VerifyAll();
        }
Esempio n. 5
0
        public void CtorRegistersForPublishReleaseMessages()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny <Func <MqttMessage, bool> >()));

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); });

            chMock.VerifyAll();
        }
Esempio n. 6
0
        public void PublishQos1Or2SavesMessageInStorage()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            var pm    = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); });
            int msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test");

            var msgs = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));
        }
Esempio n. 7
0
        public void PublishSendsPublishMessageThroughConnectionHandler()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()));

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); });

            pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();
        }
Esempio n. 8
0
        /// <summary>
        /// Performs a synchronous connect to the message broker.
        /// </summary>
        public ConnectionState Connect()
        {
            MqttConnectMessage connectMessage = GetConnectMessage();

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            subscriptionsManager = new SubscriptionsManager(connectionHandler);
            messageLogger        = new MessageLogger(connectionHandler);
            publishingManager    = new PublishingManager(connectionHandler, HandlePublishMessage);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
Esempio n. 9
0
        public void ConsequtivePublishOnSameTopicGetsNextMessageId()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()));

            // publish and save the first id
            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });
            int firstMsgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");
            int secondMsgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            Assert.Equal<int>(firstMsgId + 1, secondMsgId);
        }
Esempio n. 10
0
        public void ConsequtivePublishOnSameTopicGetsNextMessageId()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()));

            // publish and save the first id
            var pm          = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });
            int firstMsgId  = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");
            int secondMsgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            Assert.Equal <int>(firstMsgId + 1, secondMsgId);
        }
Esempio n. 11
0
        public void PublishReturnIdMatchesPublishedMessageId()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()))
            .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm    = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });
            int retId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message message ids match
            Assert.Equal <int>(pubMsg.VariableHeader.MessageIdentifier, retId);
        }
Esempio n. 12
0
        public void PublishSendsPublishMessageWithCorrectTopic()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()))
            .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message topic was correct
            Assert.Equal <string>("A/Topic", pubMsg.VariableHeader.TopicName);
        }
Esempio n. 13
0
        public void AcknowledgedQos1MessageRemovedFromStorage()
        {
            Func<MqttMessage, bool> ackCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishAck, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { ackCallback = cb; });

            // send the message, verify we've stored it ok.
            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; });
            var msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test");
            var msgs = GetPublishedMessages(pm);
            Assert.True(msgs.ContainsKey(msgId));

            // now fake an acknowledgement of the message, and ensure it's been removed from storage.
            ackCallback(new MqttPublishAckMessage().WithMessageIdentifier(msgId));
            Assert.False(msgs.ContainsKey(msgId));
        }
Esempio n. 14
0
        public void PublishSendsPublishMessageCorrectPayload()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()))
            .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message payload was correct
            Assert.Equal <string>("test", Encoding.ASCII.GetString(pubMsg.Payload.Message.ToArray <byte>()));
        }
Esempio n. 15
0
        public void AcknowledgedQos1MessageRemovedFromStorage()
        {
            Func <MqttMessage, bool> ackCallback = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishAck, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { ackCallback = cb; });

            // send the message, verify we've stored it ok.
            var pm    = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); });
            var msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test");
            var msgs  = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));

            // now fake an acknowledgement of the message, and ensure it's been removed from storage.
            ackCallback(new MqttPublishAckMessage().WithMessageIdentifier(msgId));
            Assert.False(msgs.ContainsKey(msgId));
        }
Esempio n. 16
0
        public void ReceivedMessageAtQos2SendsPublishReceived()
        {
            Func <MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock <IMqttConnectionHandler>();

            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType mt, Func <MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.SendMessage(It.IsAny <MqttPublishReceivedMessage>()));

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            pubCallback(new MqttPublishMessage()
                        .WithMessageIdentifier(msgId)
                        .ToTopic("A/Topic")
                        .WithQos(MqttQos.ExactlyOnce)
                        .PublishData(new byte[] { 0, 1, 2 }));

            mock.VerifyAll();
        }
Esempio n. 17
0
        public void ReceivedMessageAtQos0DoesNotRespondToRemoteParty()
        {
            Func <MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock <IMqttConnectionHandler>();

            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType mt, Func <MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.SendMessage(It.IsAny <MqttMessage>()));

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            pubCallback(new MqttPublishMessage()
                        .WithMessageIdentifier(msgId)
                        .ToTopic("A/Topic")
                        .WithQos(MqttQos.AtMostOnce)
                        .PublishData(new byte[] { 0, 1, 2 }));

            // verify nothing was sent by the pub mgr
            mock.Verify(x => x.SendMessage(It.IsAny <MqttMessage>()), Times.Never());
        }
Esempio n. 18
0
        public void ReceivedMessageAtQos1DoesNotStoreMessage()
        {
            Func <MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock <IMqttConnectionHandler>();

            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType mt, Func <MqttMessage, bool> cb) => pubCallback = cb);

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });

            pubCallback(new MqttPublishMessage()
                        .WithMessageIdentifier(msgId)
                        .ToTopic("A/Topic")
                        .WithQos(MqttQos.AtLeastOnce)
                        .PublishData(new byte[] { 0, 1, 2 }));

            var rcvdMsgs = GetReceivedMessages(pm);

            Assert.False(rcvdMsgs.ContainsKey(msgId));
        }
Esempio n. 19
0
        public void ReleasedQos2MessageRemovedFromStorage()
        {
            Func <MqttMessage, bool> rcvdCallback = null;
            Func <MqttMessage, bool> compCallback = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishReceived, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { rcvdCallback = cb; });
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny <Func <MqttMessage, bool> >()));
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { compCallback = cb; });
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>()));
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishReleaseMessage>()));

            // send the message, verify we've stored it ok.
            var pm    = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); });
            var msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.ExactlyOnce, "test");
            var msgs  = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));

            // verify the pub msg has send a publish message.
            chMock.Verify(x => x.SendMessage(It.IsAny <MqttPublishMessage>()));

            // fake a response from the other party saying Received, this should initiate a Release to the other party
            rcvdCallback(new MqttPublishReceivedMessage().WithMessageIdentifier(msgId));
            Assert.True(msgs.ContainsKey(msgId));

            // verify the pub msg has sent a publish release message.
            chMock.Verify(x => x.SendMessage(It.IsAny <MqttPublishReleaseMessage>()));

            // fake a response from the other party saying "Complete", this should remove our copy of the message locally.
            compCallback(new MqttPublishCompleteMessage().WithMessageIdentifier(msgId));
            Assert.False(msgs.ContainsKey(msgId));
        }
Esempio n. 20
0
        public void PublishSendsPublishMessageCorrectPayload()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()))
                .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });
            pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message payload was correct
            Assert.Equal<string>("test", Encoding.ASCII.GetString(pubMsg.Payload.Message.ToArray<byte>()));
        }
Esempio n. 21
0
        public void PublishSendsPublishMessageThroughConnectionHandler()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()));

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; });
            pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();
        }
Esempio n. 22
0
        public void PublishSendsPublishMessageWithCorrectTopic()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()))
                .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });
            pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message topic was correct
            Assert.Equal<string>("A/Topic", pubMsg.VariableHeader.TopicName);
        }
Esempio n. 23
0
        public void PublishReturnIdMatchesPublishedMessageId()
        {
            MqttPublishMessage pubMsg = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()))
                .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg);

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });
            int retId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message message ids match
            Assert.Equal<int>(pubMsg.VariableHeader.MessageIdentifier, retId);
        }
Esempio n. 24
0
        public void ReceivedPublishMessageAtQos2StoresMessage()
        {
            Func<MqttMessage, bool> pubCallback = null;
            Func<MqttMessage, bool> pubRelCallback = null;
            short msgId = 1;

            var mock = new Mock<IMqttConnectionHandler>();
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubRelCallback = cb);
            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });

            // fake a publish from a remote party and verify we sent a received back.
            pubCallback(new MqttPublishMessage()
                .WithMessageIdentifier(msgId)
                .ToTopic("A/Topic")
                .WithQos(MqttQos.ExactlyOnce)
                .PublishData(new byte[] { 0, 1, 2 }));
            var rcvdMsgs = GetReceivedMessages(pm);
            Assert.True(rcvdMsgs.ContainsKey(msgId));
        }
Esempio n. 25
0
        public void PublishQos1Or2SavesMessageInStorage()
        {
            var chMock = new Mock<IMqttConnectionHandler>();

            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; });
            int msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test");

            var msgs = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));
        }
Esempio n. 26
0
        public void ReceivedMessageAtQos1DoesNotStoreMessage()
        {
            Func<MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock<IMqttConnectionHandler>();
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubCallback = cb);

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });

            pubCallback(new MqttPublishMessage()
                .WithMessageIdentifier(msgId)
                .ToTopic("A/Topic")
                .WithQos(MqttQos.AtLeastOnce)
                .PublishData(new byte[] { 0, 1, 2 }));

            var rcvdMsgs = GetReceivedMessages(pm);
            Assert.False(rcvdMsgs.ContainsKey(msgId));
        }
Esempio n. 27
0
        public void ReleasedMessageAtQos2SendsPublishComplete()
        {
            Func<MqttMessage, bool> pubCallback = null;
            Func<MqttMessage, bool> pubRelCallback = null;
            short msgId = 1;

            var mock = new Mock<IMqttConnectionHandler>();
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishReceived, It.IsAny<Func<MqttMessage, bool>>()));
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubRelCallback = cb);
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny<Func<MqttMessage, bool>>()));

            mock.Setup(x => x.SendMessage(It.IsAny<MqttPublishReceivedMessage>()));
            mock.Setup(x => x.SendMessage(It.IsAny<MqttPublishCompleteMessage>()));

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });

            // fake a publish from a remote party and verify we sent a received back.
            pubCallback(new MqttPublishMessage()
                .WithMessageIdentifier(msgId)
                .ToTopic("A/Topic")
                .WithQos(MqttQos.ExactlyOnce)
                .PublishData(new byte[] { 0, 1, 2 }));
            mock.Verify(x => x.SendMessage(It.IsAny<MqttPublishReceivedMessage>()));

            // fake the publish release message from remote party and verify that a publishcomplete is sent.
            pubRelCallback(new MqttPublishReleaseMessage().WithMessageIdentifier(msgId));
            mock.Verify(x => x.SendMessage(It.IsAny<MqttPublishCompleteMessage>()));
        }
Esempio n. 28
0
        public void ReleasedQos2MessageRemovedFromStorage()
        {
            Func<MqttMessage, bool> rcvdCallback = null;
            Func<MqttMessage, bool> compCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishReceived, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { rcvdCallback = cb; });
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny<Func<MqttMessage, bool>>()));
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { compCallback = cb; });
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()));
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishReleaseMessage>()));

            // send the message, verify we've stored it ok.
            var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });
            var msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.ExactlyOnce, "test");
            var msgs = GetPublishedMessages(pm);
            Assert.True(msgs.ContainsKey(msgId));

            // verify the pub msg has send a publish message.
            chMock.Verify(x => x.SendMessage(It.IsAny<MqttPublishMessage>()));

            // fake a response from the other party saying Received, this should initiate a Release to the other party
            rcvdCallback(new MqttPublishReceivedMessage().WithMessageIdentifier(msgId));
            Assert.True(msgs.ContainsKey(msgId));

            // verify the pub msg has sent a publish release message.
            chMock.Verify(x => x.SendMessage(It.IsAny<MqttPublishReleaseMessage>()));

            // fake a response from the other party saying "Complete", this should remove our copy of the message locally.
            compCallback(new MqttPublishCompleteMessage().WithMessageIdentifier(msgId));
            Assert.False(msgs.ContainsKey(msgId));
        }
Esempio n. 29
0
        public void ReceivedMessageAtQos0DoesNotRespondToRemoteParty()
        {
            Func<MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock<IMqttConnectionHandler>();
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.SendMessage(It.IsAny<MqttMessage>()));

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });

            pubCallback(new MqttPublishMessage()
                .WithMessageIdentifier(msgId)
                .ToTopic("A/Topic")
                .WithQos(MqttQos.AtMostOnce)
                .PublishData(new byte[] { 0, 1, 2 }));

            // verify nothing was sent by the pub mgr
            mock.Verify(x => x.SendMessage(It.IsAny<MqttMessage>()), Times.Never());
        }
Esempio n. 30
0
        public void ReceivedMessageAtQos2SendsPublishReceived()
        {
            Func<MqttMessage, bool> pubCallback = null;
            short msgId = 1;

            var mock = new Mock<IMqttConnectionHandler>();
            mock.Setup(x => x.RegisterForMessage(MqttMessageType.Publish, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pubCallback = cb);
            mock.Setup(x => x.SendMessage(It.IsAny<MqttPublishReceivedMessage>()));

            var pm = new Nmqtt.PublishingManager(mock.Object, (MqttPublishMessage dummyPubMsg) => { return true; });

            pubCallback(new MqttPublishMessage()
                .WithMessageIdentifier(msgId)
                .ToTopic("A/Topic")
                .WithQos(MqttQos.ExactlyOnce)
                .PublishData(new byte[] { 0, 1, 2 }));

            mock.VerifyAll();
        }