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.
 
 
 
 

63 lines
2.0 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading.Tasks;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using MQTTnet.Client;
  10. using MQTTnet.Implementations;
  11. using MQTTnet.Tests.Mockups;
  12. namespace MQTTnet.Tests
  13. {
  14. [TestClass]
  15. public class RoundtripTime_Tests
  16. {
  17. public TestContext TestContext { get; set; }
  18. [TestMethod]
  19. public async Task Round_Trip_Time()
  20. {
  21. using (var testEnvironment = new TestEnvironment(TestContext))
  22. {
  23. await testEnvironment.StartServer();
  24. var receiverClient = await testEnvironment.ConnectClient();
  25. var senderClient = await testEnvironment.ConnectClient();
  26. TaskCompletionSource<string> response = null;
  27. receiverClient.ApplicationMessageReceivedAsync += e =>
  28. {
  29. response?.TrySetResult(e.ApplicationMessage.ConvertPayloadToString());
  30. return PlatformAbstractionLayer.CompletedTask;
  31. };
  32. await receiverClient.SubscribeAsync("#");
  33. var times = new List<TimeSpan>();
  34. var stopwatch = Stopwatch.StartNew();
  35. await Task.Delay(1000);
  36. for (var i = 0; i < 100; i++)
  37. {
  38. response = new TaskCompletionSource<string>();
  39. await senderClient.PublishStringAsync("test", DateTime.UtcNow.Ticks.ToString());
  40. if (!response.Task.Wait(TimeSpan.FromSeconds(5)))
  41. {
  42. throw new TimeoutException();
  43. }
  44. stopwatch.Stop();
  45. times.Add(stopwatch.Elapsed);
  46. stopwatch.Restart();
  47. }
  48. }
  49. }
  50. }
  51. }