Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

58 linhas
1.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Client;
  7. using MQTTnet.Client.Options;
  8. using MQTTnet.Client.Receiving;
  9. using MQTTnet.Server;
  10. namespace MQTTnet.Tests
  11. {
  12. [TestClass]
  13. public class RoundtripTimeTests
  14. {
  15. [TestMethod]
  16. public async Task Round_Trip_Time()
  17. {
  18. var factory = new MqttFactory();
  19. var server = factory.CreateMqttServer();
  20. var receiverClient = factory.CreateMqttClient();
  21. var senderClient = factory.CreateMqttClient();
  22. await server.StartAsync(new MqttServerOptionsBuilder().Build());
  23. await receiverClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  24. await receiverClient.SubscribeAsync("#");
  25. await senderClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  26. TaskCompletionSource<string> response = null;
  27. receiverClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(args =>
  28. {
  29. response?.SetResult(args.ApplicationMessage.ConvertPayloadToString());
  30. });
  31. var times = new List<TimeSpan>();
  32. var stopwatch = Stopwatch.StartNew();
  33. await Task.Delay(1000);
  34. for (var i = 0; i < 100; i++)
  35. {
  36. response = new TaskCompletionSource<string>();
  37. await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
  38. response.Task.GetAwaiter().GetResult();
  39. stopwatch.Stop();
  40. times.Add(stopwatch.Elapsed);
  41. stopwatch.Restart();
  42. }
  43. await server.StopAsync();
  44. }
  45. }
  46. }