You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttSubscriptionsManager_Tests.cs 3.8 KiB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Protocol;
  4. using MQTTnet.Server;
  5. using MQTTnet.Tests.Mockups;
  6. namespace MQTTnet.Tests
  7. {
  8. [TestClass]
  9. public class MqttSubscriptionsManager_Tests
  10. {
  11. [TestMethod]
  12. public void MqttSubscriptionsManager_SubscribeSingleSuccess()
  13. {
  14. var sm = new MqttClientSubscriptionsManager("", new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  15. var sp = new MqttSubscribePacket();
  16. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  17. sm.SubscribeAsync(sp).GetAwaiter().GetResult();
  18. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce);
  19. Assert.IsTrue(result.IsSubscribed);
  20. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  21. }
  22. [TestMethod]
  23. public void MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
  24. {
  25. var sm = new MqttClientSubscriptionsManager("", new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  26. var sp = new MqttSubscribePacket();
  27. sp.TopicFilters.Add(new TopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  28. sm.SubscribeAsync(sp).GetAwaiter().GetResult();
  29. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  30. Assert.IsTrue(result.IsSubscribed);
  31. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  32. }
  33. [TestMethod]
  34. public void MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
  35. {
  36. var sm = new MqttClientSubscriptionsManager("", new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  37. var sp = new MqttSubscribePacket();
  38. sp.TopicFilters.Add(new TopicFilter { Topic = "#", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  39. sp.TopicFilters.Add(new TopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
  40. sm.SubscribeAsync(sp).GetAwaiter().GetResult();
  41. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  42. Assert.IsTrue(result.IsSubscribed);
  43. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
  44. }
  45. [TestMethod]
  46. public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
  47. {
  48. var sm = new MqttClientSubscriptionsManager("", new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  49. var sp = new MqttSubscribePacket();
  50. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  51. sm.SubscribeAsync(sp).GetAwaiter().GetResult();
  52. Assert.IsFalse(sm.CheckSubscriptions("A/B/X", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  53. }
  54. [TestMethod]
  55. public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
  56. {
  57. var sm = new MqttClientSubscriptionsManager("", new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  58. var sp = new MqttSubscribePacket();
  59. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  60. sm.SubscribeAsync(sp).GetAwaiter().GetResult();
  61. Assert.IsTrue(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  62. var up = new MqttUnsubscribePacket();
  63. up.TopicFilters.Add("A/B/C");
  64. sm.UnsubscribeAsync(up);
  65. Assert.IsFalse(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  66. }
  67. }
  68. }