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.
 
 
 
 

122 lines
4.5 KiB

  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().CreateChildLogger("")));
  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().CreateChildLogger("")));
  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().CreateChildLogger("")));
  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().CreateChildLogger("")));
  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().CreateChildLogger("")));
  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. }