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.

MqttSubscriptionsManagerTests.cs 2.2 KiB

7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Core.Packets;
  3. using MQTTnet.Core.Protocol;
  4. using MQTTnet.Core.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 MqttPublishPacket
  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 MqttPublishPacket
  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 MqttPublishPacket
  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. }