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.
 
 
 
 

75 lines
2.2 KiB

  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 TopicFilter("A/B/C"));
  16. sm.Subscribe(sp, "");
  17. var pp = new MqttApplicationMessage
  18. {
  19. Topic = "A/B/C",
  20. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  21. };
  22. Assert.IsTrue(sm.CheckSubscriptions(pp).IsSubscribed);
  23. }
  24. [TestMethod]
  25. public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
  26. {
  27. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
  28. var sp = new MqttSubscribePacket();
  29. sp.TopicFilters.Add(new TopicFilter("A/B/C"));
  30. sm.Subscribe(sp, "");
  31. var pp = new MqttApplicationMessage
  32. {
  33. Topic = "A/B/X",
  34. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  35. };
  36. Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
  37. }
  38. [TestMethod]
  39. public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
  40. {
  41. var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
  42. var sp = new MqttSubscribePacket();
  43. sp.TopicFilters.Add(new TopicFilter("A/B/C"));
  44. sm.Subscribe(sp, "");
  45. var pp = new MqttApplicationMessage
  46. {
  47. Topic = "A/B/C",
  48. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  49. };
  50. Assert.IsTrue(sm.CheckSubscriptions(pp).IsSubscribed);
  51. var up = new MqttUnsubscribePacket();
  52. up.TopicFilters.Add("A/B/C");
  53. sm.Unsubscribe(up);
  54. Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
  55. }
  56. }
  57. }