|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.Packets;
- using MQTTnet.Protocol;
- using MQTTnet.Server;
-
- namespace MQTTnet.Core.Tests
- {
- [TestClass]
- public class MqttSubscriptionsManagerTests
- {
- [TestMethod]
- public void MqttSubscriptionsManager_SubscribeSingleSuccess()
- {
- var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
-
- var sp = new MqttSubscribePacket();
- sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
-
- sm.SubscribeAsync(sp, "").Wait();
-
- var pp = new MqttApplicationMessage
- {
- Topic = "A/B/C",
- QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
- };
-
- var result = sm.CheckSubscriptionsAsync(pp).Result;
- Assert.IsTrue(result.IsSubscribed);
- Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
- }
-
- [TestMethod]
- public void MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
- {
- var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
-
- var sp = new MqttSubscribePacket();
- sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
-
- sm.SubscribeAsync(sp, "").Wait();
-
- var pp = new MqttApplicationMessage
- {
- Topic = "A/B/C",
- QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
- };
-
- var result = sm.CheckSubscriptionsAsync(pp).Result;
- Assert.IsTrue(result.IsSubscribed);
- Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
- }
-
- [TestMethod]
- public void MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
- {
- var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
-
- var sp = new MqttSubscribePacket();
- sp.TopicFilters.Add(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
- sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtLeastOnce));
-
- sm.SubscribeAsync(sp, "").Wait();
-
- var pp = new MqttApplicationMessage
- {
- Topic = "A/B/C",
- QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
- };
-
- var result = sm.CheckSubscriptionsAsync(pp).Result;
- Assert.IsTrue(result.IsSubscribed);
- Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
- }
-
- [TestMethod]
- public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
- {
- var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
-
- var sp = new MqttSubscribePacket();
- sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
-
- sm.SubscribeAsync(sp, "").Wait();
-
- var pp = new MqttApplicationMessage
- {
- Topic = "A/B/X",
- QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
- };
-
- Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
- }
-
- [TestMethod]
- public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
- {
- var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
-
- var sp = new MqttSubscribePacket();
- sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
-
- sm.SubscribeAsync(sp, "").Wait();
-
- var pp = new MqttApplicationMessage
- {
- Topic = "A/B/C",
- QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
- };
-
- Assert.IsTrue(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
-
- var up = new MqttUnsubscribePacket();
- up.TopicFilters.Add("A/B/C");
- sm.UnsubscribeAsync(up).Wait();
-
- Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
- }
- }
- }
|