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.

TestEnvironment.cs 4.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Client;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.Server;
  9. namespace MQTTnet.Tests.Mockups
  10. {
  11. public class TestEnvironment : IDisposable
  12. {
  13. private readonly MqttFactory _mqttFactory = new MqttFactory();
  14. private readonly List<IMqttClient> _clients = new List<IMqttClient>();
  15. private readonly IMqttNetLogger _serverLogger = new MqttNetLogger("server");
  16. private readonly IMqttNetLogger _clientLogger = new MqttNetLogger("client");
  17. private readonly List<string> _serverErrors = new List<string>();
  18. private readonly List<string> _clientErrors = new List<string>();
  19. private readonly List<Exception> _exceptions = new List<Exception>();
  20. public IMqttServer Server { get; private set; }
  21. public bool IgnoreClientLogErrors { get; set; }
  22. public bool IgnoreServerLogErrors { get; set; }
  23. public int ServerPort { get; set; } = 1888;
  24. public IMqttNetLogger ServerLogger => _serverLogger;
  25. public IMqttNetLogger ClientLogger => _clientLogger;
  26. public TestEnvironment()
  27. {
  28. _serverLogger.LogMessagePublished += (s, e) =>
  29. {
  30. if (e.TraceMessage.Level == MqttNetLogLevel.Error)
  31. {
  32. lock (_serverErrors)
  33. {
  34. _serverErrors.Add(e.TraceMessage.ToString());
  35. }
  36. }
  37. };
  38. _clientLogger.LogMessagePublished += (s, e) =>
  39. {
  40. lock (_clientErrors)
  41. {
  42. if (e.TraceMessage.Level == MqttNetLogLevel.Error)
  43. {
  44. _clientErrors.Add(e.TraceMessage.ToString());
  45. }
  46. }
  47. };
  48. }
  49. public IMqttClient CreateClient()
  50. {
  51. var client = _mqttFactory.CreateMqttClient(_clientLogger);
  52. _clients.Add(client);
  53. return client;
  54. }
  55. public Task<IMqttServer> StartServerAsync()
  56. {
  57. return StartServerAsync(new MqttServerOptionsBuilder());
  58. }
  59. public async Task<IMqttServer> StartServerAsync(MqttServerOptionsBuilder options)
  60. {
  61. if (Server != null)
  62. {
  63. throw new InvalidOperationException("Server already started.");
  64. }
  65. Server = _mqttFactory.CreateMqttServer(_serverLogger);
  66. await Server.StartAsync(options.WithDefaultEndpointPort(ServerPort).Build());
  67. return Server;
  68. }
  69. public Task<IMqttClient> ConnectClientAsync()
  70. {
  71. return ConnectClientAsync(new MqttClientOptionsBuilder());
  72. }
  73. public async Task<IMqttClient> ConnectClientAsync(MqttClientOptionsBuilder options)
  74. {
  75. if (options == null) throw new ArgumentNullException(nameof(options));
  76. var client = CreateClient();
  77. await client.ConnectAsync(options.WithTcpServer("localhost", ServerPort).Build());
  78. return client;
  79. }
  80. public async Task<IMqttClient> ConnectClientAsync(IMqttClientOptions options)
  81. {
  82. if (options == null) throw new ArgumentNullException(nameof(options));
  83. var client = CreateClient();
  84. await client.ConnectAsync(options);
  85. return client;
  86. }
  87. public void ThrowIfLogErrors()
  88. {
  89. lock (_serverErrors)
  90. {
  91. if (!IgnoreServerLogErrors && _serverErrors.Count > 0)
  92. {
  93. throw new Exception($"Server had {_serverErrors.Count} errors (${string.Join(Environment.NewLine, _serverErrors)}).");
  94. }
  95. }
  96. lock (_clientErrors)
  97. {
  98. if (!IgnoreClientLogErrors && _clientErrors.Count > 0)
  99. {
  100. throw new Exception($"Client(s) had {_clientErrors.Count} errors (${string.Join(Environment.NewLine, _clientErrors)}).");
  101. }
  102. }
  103. }
  104. public void Dispose()
  105. {
  106. foreach (var mqttClient in _clients)
  107. {
  108. mqttClient?.Dispose();
  109. }
  110. Server?.StopAsync().GetAwaiter().GetResult();
  111. ThrowIfLogErrors();
  112. if (_exceptions.Any())
  113. {
  114. throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
  115. }
  116. }
  117. public void TrackException(Exception exception)
  118. {
  119. lock (_exceptions)
  120. {
  121. _exceptions.Add(exception);
  122. }
  123. }
  124. }
  125. }