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.

RoundtripTimeTests.cs 1.8 KiB

преди 6 години
преди 5 години
преди 6 години
преди 5 години
преди 6 години
преди 5 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }