public void AcknowledgedSubscriptionRequestCreatesActiveSubscription() { Func <MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var pubMock = new Mock <IPublishingManager>(); var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(1).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); }
public void MultipleSubscriptionsForAcknowledgedSubscriptionThrowsException() { Func <MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); short subid = subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal <MqttQos>(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal <SubscriptionStatus>(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); // NOW THE IMPORTANT PART - Try and subscribe agin to the same topic. Assert.Throws <ArgumentException>(() => subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos)); }
public void AcknowledgedSubscriptionRequestCreatesActiveSubscription() { Func<MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var chMock = new Mock<IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny<MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); short subid = subs.RegisterSubscription<AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal<MqttQos>(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal<SubscriptionStatus>(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); }
public void SubscriptionRequestCreatesPendingSubscription() { var chMock = new Mock <IMqttConnectionHandler>(); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); Assert.Equal <SubscriptionStatus>(SubscriptionStatus.Pending, subs.GetSubscriptionsStatus(topic)); }
public void SubscriptionRequestCreatesPendingSubscription() { var chMock = new Mock<IMqttConnectionHandler>(); var pubMock = new Mock<IPublishingManager>(); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription<string, AsciiPayloadConverter>(topic, qos); Assert.Equal(SubscriptionStatus.Pending, subs.GetSubscriptionsStatus(topic)); }