public void ConnectAsyncCallsConnectCompleteEventWithEventData() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; MqttProtocolInformation.Settings.KeepAliveTime = 5*60; client.ConnectComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect)); are.Set(); }; client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!are.WaitOne(5000)) { Assert.Fail("ConnectComplete event not fired."); } }
internal SubscriptionClient(MqttClientProtocol mqttClient, SubscriptionItem subscription, string clientUid) { _mqtt = mqttClient; _mqtt.SubscribeComplete += MqttOnSubscribeComplete; _mqtt.SendMessageComplete += MqttOnOperationComplete; _mqtt.ConnectComplete += MqttOnConnectComplete; _subscription = subscription; _clientUid = clientUid; _disposed = false; }
internal MqttClient(ISocketAdapter socket, ILogger logger, SocketEncryption encryptionLevel) { _logger = logger; _encryptionLevel = encryptionLevel; _mqtt = new MqttClientProtocol(_logger, socket); _mqtt.ConnectComplete += MqttOnConnectComplete; _mqtt.SubscribeComplete += MqttOnSubscribeComplete; _mqtt.SendMessageComplete += MqttOnSendMessageComplete; _mqtt.PublishReceived += MqttOnPublishReceived; _mqtt.NetworkError += MqttOnNetworkError; }
public void ConnectDisconnectConnectSequenceDoesNotThrow() { var areConnect = new AutoResetEvent(false); var areDisconnect = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; client.ConnectComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect)); areConnect.Set(); }; client.SendMessageComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Disconnect)); areDisconnect.Set(); }; client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!areConnect.WaitOne(5000)) { Assert.Fail("First ConnectComplete event did not fire."); } client.DisconnectAsync(eventData, null); if (!areDisconnect.WaitOne(5000)) { Assert.Fail("First DisconnectComplete event did not fire."); } client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!areConnect.WaitOne(5000)) { Assert.Fail("Second ConnectComplete event did not fire."); } }
public void OnMessageCallbackGetsCalledWithWildcardTopics1() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/+", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); subClient.OnMessage(msg => { Assert.IsNotNull(msg); Assert.IsTrue(msg.MessageType == MessageType.Publish); Assert.AreEqual(msg.Payload[0], 0x00); Assert.AreEqual(msg.Payload[1], 0x01); Assert.AreEqual(msg.Payload[2], 0x02); are.Set(); }); moqSocket.ReceiveMessage(new MqttPublishMessageBuilder { TopicName = "a/b/c", Payload = new byte[] { 0x00, 0x01, 0x02 } }); if (!are.WaitOne(5000)) { Assert.Fail("OnMessage callback not called."); } }
public void DisconnectAsyncCallsDisconnectCompleteEventWithEventData() { var are = new AutoResetEvent(false); var eventData = "Test data"; var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); client.SendMessageComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Disconnect)); are.Set(); }; client.DisconnectAsync(eventData, null); if (!are.WaitOne(5000)) { Assert.Fail("DisconnectComplete event not fired."); } }
public void OnMessageCallbackDoesNotGetCalledAfterClose() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/c", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); subClient.OnMessage(msg => { Assert.Fail("OnMessage callback was called after Close call."); are.Set(); }); subClient.Close(); moqSocket.ReceiveMessage(new MqttPublishMessageBuilder { TopicName = "a/b/c", Payload = new byte[] { 0x00, 0x01, 0x02 } }); are.WaitOne(3000); }
public void IsClosedCorrectlyReflectsStateOfObject() { var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/+", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); Assert.IsTrue(subClient.IsClosed); subClient.OnMessage(msg => { Assert.IsNotNull(msg); Assert.IsTrue(msg.MessageType == MessageType.Publish); }); Assert.IsFalse(subClient.IsClosed); subClient.Close(); Assert.IsTrue(subClient.IsClosed); }
public void ReceivePublishQosLevel2CallsMessageReceived() { var are = new AutoResetEvent(false); var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var topic = "a/b/c"; var id = 42; var bldr = new MqttPublishMessageBuilder { MessageId = id, QualityOfService = QualityOfService.ExactlyOnce, TopicName = topic }; client.PublishReceived += (sender, args) => { var pubMsg = args.Message as MqttPublishMessage; Assert.IsNotNull(pubMsg); Assert.AreEqual(topic, pubMsg.TopicName); Assert.AreEqual(2, moqSocket.SentMessages.Count); Assert.AreEqual(id, pubMsg.MessageId); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.PubRec)); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.PubComp)); are.Set(); }; moqSocket.ReceiveMessage(bldr); if (!are.WaitOne(5000)) { Assert.Fail("PublishReceived event not fired for received Publish (Qos=ExactlyOnce)."); } }
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 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 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 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."); } }