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.
 
 
 
 

53 lines
1.6 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.Tests.Mockups;
  8. namespace MQTTnet.Tests
  9. {
  10. [TestClass]
  11. public class RoundtripTime_Tests
  12. {
  13. public TestContext TestContext { get; set; }
  14. [TestMethod]
  15. public async Task Round_Trip_Time()
  16. {
  17. using (var testEnvironment = new TestEnvironment(TestContext))
  18. {
  19. await testEnvironment.StartServer();
  20. var receiverClient = await testEnvironment.ConnectClient();
  21. var senderClient = await testEnvironment.ConnectClient();
  22. await receiverClient.SubscribeAsync("#");
  23. TaskCompletionSource<string> response = null;
  24. receiverClient.UseApplicationMessageReceivedHandler(e =>
  25. {
  26. response?.SetResult(e.ApplicationMessage.ConvertPayloadToString());
  27. });
  28. var times = new List<TimeSpan>();
  29. var stopwatch = Stopwatch.StartNew();
  30. await Task.Delay(1000);
  31. for (var i = 0; i < 100; i++)
  32. {
  33. response = new TaskCompletionSource<string>();
  34. await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
  35. response.Task.GetAwaiter().GetResult();
  36. stopwatch.Stop();
  37. times.Add(stopwatch.Elapsed);
  38. stopwatch.Restart();
  39. }
  40. }
  41. }
  42. }
  43. }