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).");
            }
        }
        public void PingToFailedBrokerCallsNetworkErrorEventAfterTimeout()
        {
            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 MqttPingRequestMessageBuilder();

            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 PingRequest.");
            }
        }
        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.");
            }
        }
        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.");
            }
        }
        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.");
            }
        }