Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MqttSubscriptionsManagerTests.cs 3.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce);
  20. Assert.IsTrue(result.IsSubscribed);
  21. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  22. }
  23. [TestMethod]
  24. public void MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
  25. {
  26. var sm = new MqttClientSubscriptionsManager("", new MqttServerOptions(), new MqttServer(new IMqttServerAdapter[0], new MqttNetLogger().CreateChildLogger("")));
  27. var sp = new MqttSubscribePacket();
  28. sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
  29. sm.Subscribe(sp);
  30. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  31. Assert.IsTrue(result.IsSubscribed);
  32. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
  33. }
  34. [TestMethod]
  35. public void MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
  36. {
  37. var sm = new MqttClientSubscriptionsManager("", new MqttServerOptions(), new MqttServer(new IMqttServerAdapter[0], new MqttNetLogger().CreateChildLogger("")));
  38. var sp = new MqttSubscribePacket();
  39. sp.TopicFilters.Add(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  40. sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtLeastOnce));
  41. sm.Subscribe(sp);
  42. var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
  43. Assert.IsTrue(result.IsSubscribed);
  44. Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
  45. }
  46. [TestMethod]
  47. public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
  48. {
  49. var sm = new MqttClientSubscriptionsManager("", new MqttServerOptions(), new MqttServer(new IMqttServerAdapter[0], new MqttNetLogger().CreateChildLogger("")));
  50. var sp = new MqttSubscribePacket();
  51. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  52. sm.Subscribe(sp);
  53. Assert.IsFalse(sm.CheckSubscriptions("A/B/X", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  54. }
  55. [TestMethod]
  56. public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
  57. {
  58. var sm = new MqttClientSubscriptionsManager("", new MqttServerOptions(), new MqttServer(new IMqttServerAdapter[0], new MqttNetLogger().CreateChildLogger("")));
  59. var sp = new MqttSubscribePacket();
  60. sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
  61. sm.Subscribe(sp);
  62. Assert.IsTrue(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  63. var up = new MqttUnsubscribePacket();
  64. up.TopicFilters.Add("A/B/C");
  65. sm.Unsubscribe(up);
  66. Assert.IsFalse(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
  67. }
  68. }
  69. }