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.

MqttSubscriptionsManagerTests.cs 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Protocol;
  4. using MQTTnet.Server;
  5. namespace MQTTnet.Core.Tests
  6. {
  7. [TestClass]
  8. public class MqttSubscriptionsManagerTests
  9. {
  10. [TestMethod]
  11. public void MqttSubscriptionsManager_SubscribeSingleSuccess()
  12. {
  13. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions(), "");
  14. var sp = new MqttSubscribePacket();
  15. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  16. sm.SubscribeAsync(sp).Wait();
  17. var pp = new MqttApplicationMessage
  18. {
  19. Topic = "A/B/C",
  20. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  21. };
  22. var result = sm.CheckSubscriptionsAsync(pp).Result;
  23. Assert.IsTrue(result.IsSubscribed);
  24. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  25. }
  26. [TestMethod]
  27. public void MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
  28. {
  29. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions(), "");
  30. var sp = new MqttSubscribePacket();
  31. sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
  32. sm.SubscribeAsync(sp).Wait();
  33. var pp = new MqttApplicationMessage
  34. {
  35. Topic = "A/B/C",
  36. QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
  37. };
  38. var result = sm.CheckSubscriptionsAsync(pp).Result;
  39. Assert.IsTrue(result.IsSubscribed);
  40. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  41. }
  42. [TestMethod]
  43. public void MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
  44. {
  45. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions(), "");
  46. var sp = new MqttSubscribePacket();
  47. sp.TopicFilters.Add(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  48. sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtLeastOnce));
  49. sm.SubscribeAsync(sp).Wait();
  50. var pp = new MqttApplicationMessage
  51. {
  52. Topic = "A/B/C",
  53. QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
  54. };
  55. var result = sm.CheckSubscriptionsAsync(pp).Result;
  56. Assert.IsTrue(result.IsSubscribed);
  57. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
  58. }
  59. [TestMethod]
  60. public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
  61. {
  62. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions(), "");
  63. var sp = new MqttSubscribePacket();
  64. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  65. sm.SubscribeAsync(sp).Wait();
  66. var pp = new MqttApplicationMessage
  67. {
  68. Topic = "A/B/X",
  69. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  70. };
  71. Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
  72. }
  73. [TestMethod]
  74. public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
  75. {
  76. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions(), "");
  77. var sp = new MqttSubscribePacket();
  78. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  79. sm.SubscribeAsync(sp).Wait();
  80. var pp = new MqttApplicationMessage
  81. {
  82. Topic = "A/B/C",
  83. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  84. };
  85. Assert.IsTrue(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
  86. var up = new MqttUnsubscribePacket();
  87. up.TopicFilters.Add("A/B/C");
  88. sm.UnsubscribeAsync(up).Wait();
  89. Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
  90. }
  91. }
  92. }