Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

ManagedMqttClient_Tests.cs 4.5 KiB

5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Connecting;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.Extensions.ManagedClient;
  9. using MQTTnet.Server;
  10. using MQTTnet.Tests.Mockups;
  11. namespace MQTTnet.Tests
  12. {
  13. [TestClass]
  14. public class ManagedMqttClient_Tests
  15. {
  16. [TestMethod]
  17. public async Task Drop_New_Messages_On_Full_Queue()
  18. {
  19. var factory = new MqttFactory();
  20. var managedClient = factory.CreateManagedMqttClient();
  21. try
  22. {
  23. var clientOptions = new ManagedMqttClientOptionsBuilder()
  24. .WithMaxPendingMessages(5)
  25. .WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropNewMessage);
  26. clientOptions.WithClientOptions(o => o.WithTcpServer("localhost"));
  27. await managedClient.StartAsync(clientOptions.Build());
  28. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "1" });
  29. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "2" });
  30. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "3" });
  31. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "4" });
  32. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "5" });
  33. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "6" });
  34. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "7" });
  35. await managedClient.PublishAsync(new MqttApplicationMessage { Topic = "8" });
  36. Assert.AreEqual(5, managedClient.PendingApplicationMessagesCount);
  37. }
  38. finally
  39. {
  40. await managedClient.StopAsync();
  41. }
  42. }
  43. [TestMethod]
  44. public async Task ManagedClients_Will_Message_Send()
  45. {
  46. using (var testEnvironment = new TestEnvironment())
  47. {
  48. var receivedMessagesCount = 0;
  49. var factory = new MqttFactory();
  50. await testEnvironment.StartServerAsync();
  51. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  52. var clientOptions = new MqttClientOptionsBuilder()
  53. .WithTcpServer("localhost", testEnvironment.ServerPort)
  54. .WithWillMessage(willMessage);
  55. var dyingClient = testEnvironment.CreateClient();
  56. var dyingManagedClient = new ManagedMqttClient(dyingClient, testEnvironment.ClientLogger.CreateChildLogger());
  57. await dyingManagedClient.StartAsync(new ManagedMqttClientOptionsBuilder()
  58. .WithClientOptions(clientOptions)
  59. .Build());
  60. var recievingClient = await testEnvironment.ConnectClientAsync();
  61. await recievingClient.SubscribeAsync("My/last/will");
  62. recievingClient.UseApplicationMessageReceivedHandler(context => Interlocked.Increment(ref receivedMessagesCount));
  63. dyingManagedClient.Dispose();
  64. await Task.Delay(1000);
  65. Assert.AreEqual(1, receivedMessagesCount);
  66. }
  67. }
  68. [TestMethod]
  69. public async Task Start_Stop()
  70. {
  71. using (var testEnvironment = new TestEnvironment())
  72. {
  73. var factory = new MqttFactory();
  74. var server = await testEnvironment.StartServerAsync();
  75. var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger().CreateChildLogger());
  76. var clientOptions = new MqttClientOptionsBuilder()
  77. .WithTcpServer("localhost", testEnvironment.ServerPort);
  78. TaskCompletionSource<bool> connected = new TaskCompletionSource<bool>();
  79. managedClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(e => { connected.SetResult(true);});
  80. await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder()
  81. .WithClientOptions(clientOptions)
  82. .Build());
  83. await connected.Task;
  84. await managedClient.StopAsync();
  85. Assert.AreEqual(0, (await server.GetClientStatusAsync()).Count);
  86. }
  87. }
  88. }
  89. }