25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.7 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.Server;
  8. namespace MQTTnet.Core.Tests
  9. {
  10. [TestClass]
  11. public class RoundtripTimeTests
  12. {
  13. [TestMethod]
  14. public async Task Round_Trip_Time()
  15. {
  16. var factory = new MqttFactory();
  17. var server = factory.CreateMqttServer();
  18. var receiverClient = factory.CreateMqttClient();
  19. var senderClient = factory.CreateMqttClient();
  20. await server.StartAsync(new MqttServerOptionsBuilder().Build());
  21. await receiverClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  22. await receiverClient.SubscribeAsync("#");
  23. await senderClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("localhost").Build());
  24. TaskCompletionSource<string> response = null;
  25. receiverClient.ApplicationMessageReceived += (sender, args) =>
  26. {
  27. response?.SetResult(args.ApplicationMessage.ConvertPayloadToString());
  28. };
  29. var times = new List<TimeSpan>();
  30. var stopwatch = Stopwatch.StartNew();
  31. await Task.Delay(1000);
  32. for (var i = 0; i < 100; i++)
  33. {
  34. response = new TaskCompletionSource<string>();
  35. await senderClient.PublishAsync("test", DateTime.UtcNow.Ticks.ToString());
  36. response.Task.GetAwaiter().GetResult();
  37. stopwatch.Stop();
  38. times.Add(stopwatch.Elapsed);
  39. stopwatch.Restart();
  40. }
  41. await server.StopAsync();
  42. }
  43. }
  44. }