Beispiel #1
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 PublishingManager(chMock.Object);
            int firstMsgId  = pm.Publish <string, AsciiPayloadConverter>("A/Topic", MqttQos.AtMostOnce, "test");
            int secondMsgId = pm.Publish <string, AsciiPayloadConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            Assert.Equal <int>(firstMsgId + 1, secondMsgId);
        }
Beispiel #2
0
        public void PublishQos1Or2SavesMessageInStorage()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            var pm    = new PublishingManager(chMock.Object);
            int msgId = pm.Publish <string, AsciiPayloadConverter>("A/Topic", MqttQos.AtLeastOnce, "test");

            var msgs = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));
        }
Beispiel #3
0
        public void PublishSendsPublishMessageThroughConnectionHandler()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

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

            var pm = new PublishingManager(chMock.Object);

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

            chMock.VerifyAll();
        }
Beispiel #4
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 PublishingManager(chMock.Object);
            int retId = pm.Publish <string, AsciiPayloadConverter>("A/Topic", MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message message ids match
            Assert.Equal <int>(pubMsg.VariableHeader.MessageIdentifier, retId);
        }
Beispiel #5
0
        public void PublishSendsPublishMessageWithCorrectQos()
        {
            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 PublishingManager(chMock.Object);

            pm.Publish <string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtLeastOnce, "test");

            chMock.VerifyAll();

            // check the message QOS was correct
            Assert.Equal(MqttQos.AtLeastOnce, pubMsg.Header.Qos);
        }
Beispiel #6
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 PublishingManager(chMock.Object);

            pm.Publish <string, AsciiPayloadConverter>("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>()));
        }
Beispiel #7
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 PublishingManager(chMock.Object);

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

            chMock.VerifyAll();

            // check the message topic was correct
            Assert.Equal("A/Topic", pubMsg.VariableHeader.TopicName);
        }
Beispiel #8
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 PublishingManager(chMock.Object);
            var msgId = pm.Publish <string, AsciiPayloadConverter>("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));
        }
Beispiel #9
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 PublishingManager(chMock.Object);
            var msgId = pm.Publish <string, AsciiPayloadConverter>("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));
        }
Beispiel #10
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 PublishingManager(chMock.Object);
            pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message payload was correct
            Assert.Equal<string>("test", Encoding.ASCII.GetString(pubMsg.Payload.Message.ToArray<byte>()));
        }
Beispiel #11
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 PublishingManager(chMock.Object);
            int firstMsgId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtMostOnce, "test");
            int secondMsgId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            Assert.Equal<int>(firstMsgId + 1, secondMsgId);
        }
Beispiel #12
0
        public void PublishQos1Or2SavesMessageInStorage() {
            var chMock = new Mock<IMqttConnectionHandler>();

            var pm = new PublishingManager(chMock.Object);
            int msgId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtLeastOnce, "test");

            var msgs = GetPublishedMessages(pm);

            Assert.True(msgs.ContainsKey(msgId));
        }
Beispiel #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 PublishingManager(chMock.Object);
            var msgId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), 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));
        }
Beispiel #14
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 PublishingManager(chMock.Object);
            int retId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();

            // check the message message ids match
            Assert.Equal<int>(pubMsg.VariableHeader.MessageIdentifier, retId);
        }
Beispiel #15
0
        public void PublishSendsPublishMessageWithCorrectQos() {
            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 PublishingManager(chMock.Object);
            pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtLeastOnce, "test");

            chMock.VerifyAll();

            // check the message QOS was correct
            Assert.Equal(MqttQos.AtLeastOnce, pubMsg.Header.Qos);
        }
Beispiel #16
0
        public void PublishSendsPublishMessageThroughConnectionHandler() {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>()));

            var pm = new PublishingManager(chMock.Object);
            pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), MqttQos.AtMostOnce, "test");

            chMock.VerifyAll();
        }
Beispiel #17
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 PublishingManager(chMock.Object);
            var msgId = pm.Publish<string, AsciiPayloadConverter>(new PublicationTopic("A/rawTopic"), 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));
        }