public void CanReadProtocolHeader()
        {
            var msgBuilder = new MqttSubscribeMessageBuilder()
            {
                Duplicate = true,
                QualityOfService = QualityOfService.ExactlyOnce,
                Retain = true,
                MessageId = 42
            };

            var msg = msgBuilder.GetMessage() as MqttSubscribeMessage;

            Assert.AreEqual(msgBuilder.Duplicate, msg.Duplicate);
            Assert.AreEqual(msgBuilder.QualityOfService, msg.QualityOfService);
            Assert.AreEqual(msgBuilder.Retain, msg.Retain);
            Assert.AreEqual(42, msg.MessageId);
        }
        public void CanReadSubscriptions()
        {
            var msgBuilder = new MqttSubscribeMessageBuilder()
            {
                Duplicate = true,
                QualityOfService = QualityOfService.ExactlyOnce,
                Retain = true,
                MessageId = 42
            };

            msgBuilder.Subscriptions.Add(new SubscriptionItem()
            {
                QualityOfService = QualityOfService.AtLeastOnce,
                TopicName = "a/b0"
            });
            msgBuilder.Subscriptions.Add(new SubscriptionItem()
            {
                QualityOfService = QualityOfService.AtLeastOnce,
                TopicName = "a/b1"
            });
            msgBuilder.Subscriptions.Add(new SubscriptionItem()
            {
                QualityOfService = QualityOfService.AtLeastOnce,
                TopicName = "a/b2"
            });

            var msg = msgBuilder.GetMessage() as MqttSubscribeMessage;

            Assert.AreEqual(3, msg.Subscriptions.Count);

            var item = msg.Subscriptions.GetAt(0);
            Assert.AreEqual(QualityOfService.AtLeastOnce, item.QualityOfService);
            Assert.AreEqual("a/b0", item.TopicName);

            item = msg.Subscriptions.GetAt(1);
            Assert.AreEqual(QualityOfService.AtLeastOnce, item.QualityOfService);
            Assert.AreEqual("a/b1", item.TopicName);

            item = msg.Subscriptions.GetAt(2);
            Assert.AreEqual(QualityOfService.AtLeastOnce, item.QualityOfService);
            Assert.AreEqual("a/b2", item.TopicName);
        }
        public void CanCreateFromMessageBuilder()
        {
            var msgBuilder = new MqttSubscribeMessageBuilder()
            {
                MessageId = 42
            };

            Assert.AreEqual(MessageType.Subscribe, msgBuilder.MessageType);
            Assert.AreEqual(QualityOfService.AtLeastOnce, msgBuilder.QualityOfService);
            Assert.AreEqual(0, msgBuilder.Subscriptions.Count);

            var msg = msgBuilder.GetMessage() as MqttSubscribeMessage;

            Assert.IsNotNull(msg);
            Assert.AreEqual(MessageType.Subscribe, msg.MessageType);
            Assert.AreEqual(false, msg.Duplicate);
            Assert.AreEqual(QualityOfService.AtLeastOnce, msg.QualityOfService);
            Assert.AreEqual(false, msg.Retain);
            Assert.AreEqual(42, msg.MessageId);
            Assert.AreEqual(0, msg.Subscriptions.Count);
        }
示例#4
0
        public void SubscribeAsync(SubscriptionItem[] items, int messageId)
        {
            var msgBuilder = new MqttSubscribeMessageBuilder
            {
                MessageId = messageId,
                QualityOfService = QualityOfService.AtLeastOnce
            };

            foreach (var item in items)
            {
                msgBuilder.Subscriptions.Add(item);
            }

            SendMessageAsync(msgBuilder);
        }
        private Task<MqttSubscribeAckMessage> SubscribeAsync(SubscriptionItem[] items, int messageId)
        {
            var msgBuilder = new MqttSubscribeMessageBuilder
            {
                MessageId = messageId
            };

            foreach (var item in items)
            {
                msgBuilder.Subscriptions.Add(item);
            }

            var tcs = new TaskCompletionSource<MqttSubscribeAckMessage>();
            _mqtt.SendMessageAsync(msgBuilder, tcs, _clientUid);
            return tcs.Task;
        }
        public void SubscribeCallsNetworkErrorEventWhenNoResponseReceived()
        {
            var are = new AutoResetEvent(false);
            var eventData = "Test data";
            var moqSocket = new MoqSocket
            {
                DoNotRespond = true
            };

            MqttProtocolInformation.Settings.NetworkTimeout = 5; // 5 seconds
            var client = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr = new MqttSubscribeMessageBuilder
            {
                MessageId = 42,
            };

            client.SendMessageComplete += (sender, args) => Assert.Fail();
            client.NetworkError += (sender, args) =>
            {
                Assert.AreEqual(bldr.MessageType, args.Message.MessageType);
                are.Set();
            };

            client.SendMessageAsync(bldr, eventData, null);

            if (!are.WaitOne(MqttProtocolInformation.Settings.NetworkTimeout * 1000 + 5000))
            {
                Assert.Fail("NetworkError event not fired for Subscribe.");
            }
        }
        public void SubscribeCallsSendMessageCompleteEventWithEventData()
        {
            var are = new AutoResetEvent(false);
            var eventData = "Test data";
            var moqSocket = new MoqSocket();
            var client = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr = new MqttSubscribeMessageBuilder
            {
                MessageId = 42,
            };

            client.SubscribeComplete += (sender, args) =>
            {
                var msg = args.Message as MqttSubscribeAckMessage;
                Assert.IsNotNull(msg);
                Assert.AreEqual(bldr.MessageId, msg.MessageId);
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Subscribe));
                are.Set();
            };

            client.SendMessageAsync(bldr, eventData, null);

            if (!are.WaitOne(5000))
            {
                Assert.Fail("SendMessageComplete event not fired for Subscribe.");
            }
        }
        public void MessageIdValidationCatchesOutOfRangeMessageId()
        {
            var msgBuilder = new MqttSubscribeMessageBuilder();

            try
            {
                msgBuilder.MessageId = 0x1FFFF;
            }
            catch (ArgumentException)
            {
                return;
            }
            catch (Exception)
            {
                Assert.Fail("Incorrect exception type thrown.");
            }

            Assert.Fail("No exception thrown for out of range MessageID");
        }