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.
 
 
 
 

143 lines
4.3 KiB

  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. var client = CreateClient();
  76. await client.ConnectAsync(options.WithTcpServer("localhost", ServerPort).Build());
  77. return client;
  78. }
  79. public void ThrowIfLogErrors()
  80. {
  81. lock (_serverErrors)
  82. {
  83. if (!IgnoreServerLogErrors && _serverErrors.Count > 0)
  84. {
  85. throw new Exception($"Server had {_serverErrors.Count} errors (${string.Join(Environment.NewLine, _serverErrors)}).");
  86. }
  87. }
  88. lock (_clientErrors)
  89. {
  90. if (!IgnoreClientLogErrors && _clientErrors.Count > 0)
  91. {
  92. throw new Exception($"Client(s) had {_clientErrors.Count} errors (${string.Join(Environment.NewLine, _clientErrors)}).");
  93. }
  94. }
  95. }
  96. public void Dispose()
  97. {
  98. foreach (var mqttClient in _clients)
  99. {
  100. mqttClient?.Dispose();
  101. }
  102. Server?.StopAsync().GetAwaiter().GetResult();
  103. ThrowIfLogErrors();
  104. if (_exceptions.Any())
  105. {
  106. throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
  107. }
  108. }
  109. public void TrackException(Exception exception)
  110. {
  111. lock (_exceptions)
  112. {
  113. _exceptions.Add(exception);
  114. }
  115. }
  116. }
  117. }