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.7 KiB

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