Ejemplo n.º 1
0
        private IAsyncAction SendMessageAsync(IMqttMessageBuilder msgBuilder)
        {
            var tcs = new TaskCompletionSource <object>();

            _mqtt.SendMessageAsync(msgBuilder, tcs, _clientUid);
            return(tcs.Task.AsAsyncAction());
        }
Ejemplo n.º 2
0
        public void PublishQosLevel2CallsSendMessageCompleteEventWithEventData()
        {
            var are       = new AutoResetEvent(false);
            var eventData = "Test data";
            var moqSocket = new MoqSocket();
            var client    = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr      = new MqttPublishMessageBuilder
            {
                MessageId        = 42,
                QualityOfService = QualityOfService.ExactlyOnce,
                TopicName        = "a/b/c"
            };

            client.SendMessageComplete += (sender, args) =>
            {
                var msg = args.Message as IMqttIdMessage;
                Assert.IsNotNull(msg);
                Assert.AreEqual(bldr.MessageId, msg.MessageId);
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Publish));
                Assert.IsFalse(moqSocket.SentMessages.Contains(MessageType.PubRec));
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.PubRel));
                Assert.IsFalse(moqSocket.SentMessages.Contains(MessageType.PubComp));
                are.Set();
            };

            client.SendMessageAsync(bldr, eventData, null);

            if (!are.WaitOne(5000))
            {
                Assert.Fail("SendMessageComplete event not fired for Publish (Qos=ExactlyOnce).");
            }
        }
Ejemplo n.º 3
0
        public void ConnectToFailedBrokerCallsNetworkErrorEventAfterTimeout()
        {
            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 MqttConnectMessageBuilder
            {
                ClientId = "Unit-test"
            };

            client.SendMessageComplete += (sender, args) => Assert.Fail();
            client.NetworkError        += (sender, args) => are.Set();

            client.SendMessageAsync(bldr, eventData, null);

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

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

            client.SendMessageAsync(bldr, eventData, null);

            if (!are.WaitOne(5000))
            {
                Assert.Fail("SendMessageComplete event not fired for Unsubscribe.");
            }
        }
Ejemplo n.º 5
0
        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.");
            }
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        public void PingCallsSendMessageCompleteEventWithEventData()
        {
            var are       = new AutoResetEvent(false);
            var eventData = "Test data";
            var moqSocket = new MoqSocket();
            var client    = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr      = new MqttPingRequestMessageBuilder();

            client.SendMessageComplete += (sender, args) =>
            {
                Assert.AreSame(eventData, args.EventData);
                Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.PingReq));
                are.Set();
            };

            client.SendMessageAsync(bldr, eventData, null);

            if (!are.WaitOne(5000))
            {
                Assert.Fail("SendMessageComplete event not fired for PingRequest.");
            }
        }
Ejemplo n.º 8
0
 private void SendMessageAsync(IMqttMessageBuilder msgBuilder)
 {
     _mqtt.SendMessageAsync(msgBuilder, msgBuilder.MessageType, _clientUid);
 }