Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MqttSubscriptionsManagerTests.cs 4.4 KiB

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