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.
 
 
 
 

114 lines
4.5 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Protocol;
  4. using MQTTnet.Server;
  5. using MQTTnet.Tests.Mockups;
  6. using System.Collections.Concurrent;
  7. using System.Threading.Tasks;
  8. namespace MQTTnet.Tests
  9. {
  10. [TestClass]
  11. public class MqttSubscriptionsManager_Tests
  12. {
  13. [TestMethod]
  14. public async Task MqttSubscriptionsManager_SubscribeSingleSuccess()
  15. {
  16. var s = CreateSession();
  17. var sm = new MqttClientSubscriptionsManager(s, new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  18. var sp = new MqttSubscribePacket();
  19. sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
  20. await sm.SubscribeAsync(sp, new MqttConnectPacket());
  21. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce);
  22. Assert.IsTrue(result.IsSubscribed);
  23. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  24. }
  25. [TestMethod]
  26. public async Task MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
  27. {
  28. var s = CreateSession();
  29. var sm = new MqttClientSubscriptionsManager(s, new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  30. var sp = new MqttSubscribePacket();
  31. sp.TopicFilters.Add(new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  32. await sm.SubscribeAsync(sp, new MqttConnectPacket());
  33. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  34. Assert.IsTrue(result.IsSubscribed);
  35. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  36. }
  37. [TestMethod]
  38. public async Task MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
  39. {
  40. var s = CreateSession();
  41. var sm = new MqttClientSubscriptionsManager(s, new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  42. var sp = new MqttSubscribePacket();
  43. sp.TopicFilters.Add(new MqttTopicFilter { Topic = "#", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  44. sp.TopicFilters.Add(new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
  45. await sm.SubscribeAsync(sp, new MqttConnectPacket());
  46. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  47. Assert.IsTrue(result.IsSubscribed);
  48. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
  49. }
  50. [TestMethod]
  51. public async Task MqttSubscriptionsManager_SubscribeSingleNoSuccess()
  52. {
  53. var s = CreateSession();
  54. var sm = new MqttClientSubscriptionsManager(s, new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  55. var sp = new MqttSubscribePacket();
  56. sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
  57. await sm.SubscribeAsync(sp, new MqttConnectPacket());
  58. Assert.IsFalse(sm.CheckSubscriptions("A/B/X", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  59. }
  60. [TestMethod]
  61. public async Task MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
  62. {
  63. var s = CreateSession();
  64. var sm = new MqttClientSubscriptionsManager(s, new MqttServerEventDispatcher(new TestLogger()), new MqttServerOptions());
  65. var sp = new MqttSubscribePacket();
  66. sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
  67. await sm.SubscribeAsync(sp, new MqttConnectPacket());
  68. Assert.IsTrue(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  69. var up = new MqttUnsubscribePacket();
  70. up.TopicFilters.Add("A/B/C");
  71. await sm.UnsubscribeAsync(up);
  72. Assert.IsFalse(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  73. }
  74. MqttClientSession CreateSession()
  75. {
  76. return new MqttClientSession(
  77. "",
  78. new ConcurrentDictionary<object, object>(),
  79. new MqttServerEventDispatcher(new TestLogger()),
  80. new MqttServerOptions(),
  81. new MqttRetainedMessagesManager(),
  82. new TestLogger());
  83. }
  84. }
  85. }