fixed clients overlapping from one test to another by:

- adding client and server wrappers to verify the clientid starts with the testname
- adding disposable baseclass
- clear disconnect handler from client so it does not try to reconnect after it is disposed
- moved code from dispose to cleanup in case it is called in a stop method as dispose may only be called once while start / stop may be called multiple times
This commit is contained in:
JanEggers
2019-12-20 15:37:23 +01:00
parent 624fafc662
commit 14419e4d63
22 changed files with 486 additions and 170 deletions
@@ -2,14 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Diagnostics;
using MQTTnet.Internal;
using MQTTnet.Server;
namespace MQTTnet.Tests.Mockups
{
public class TestEnvironment : IDisposable
public class TestEnvironment : Disposable
{
private readonly MqttFactory _mqttFactory = new MqttFactory();
private readonly List<IMqttClient> _clients = new List<IMqttClient>();
@@ -33,7 +35,9 @@ namespace MQTTnet.Tests.Mockups
public IMqttNetLogger ClientLogger => _clientLogger;
public TestEnvironment()
public TestContext TestContext { get; }
public TestEnvironment(TestContext testContext)
{
_serverLogger.LogMessagePublished += (s, e) =>
{
@@ -56,13 +60,14 @@ namespace MQTTnet.Tests.Mockups
}
}
};
TestContext = testContext;
}
public IMqttClient CreateClient()
{
var client = _mqttFactory.CreateMqttClient(_clientLogger);
_clients.Add(client);
return client;
return new TestClientWrapper(client, TestContext);
}
public Task<IMqttServer> StartServerAsync()
@@ -77,7 +82,7 @@ namespace MQTTnet.Tests.Mockups
throw new InvalidOperationException("Server already started.");
}
Server = _mqttFactory.CreateMqttServer(_serverLogger);
Server = new TestServerWrapper(_mqttFactory.CreateMqttServer(_serverLogger), TestContext, this);
await Server.StartAsync(options.WithDefaultEndpointPort(ServerPort).Build());
return Server;
@@ -85,7 +90,7 @@ namespace MQTTnet.Tests.Mockups
public Task<IMqttClient> ConnectClientAsync()
{
return ConnectClientAsync(new MqttClientOptionsBuilder());
return ConnectClientAsync(new MqttClientOptionsBuilder() );
}
public async Task<IMqttClient> ConnectClientAsync(MqttClientOptionsBuilder options)
@@ -127,21 +132,25 @@ namespace MQTTnet.Tests.Mockups
}
}
public void Dispose()
protected override void Dispose(bool disposing)
{
foreach (var mqttClient in _clients)
if (disposing)
{
mqttClient?.Dispose();
}
Server?.StopAsync().GetAwaiter().GetResult();
ThrowIfLogErrors();
if (_exceptions.Any())
{
throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
foreach (var mqttClient in _clients)
{
mqttClient?.Dispose();
}
Server?.StopAsync().GetAwaiter().GetResult();
ThrowIfLogErrors();
if (_exceptions.Any())
{
throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
}
}
base.Dispose(disposing);
}
public void TrackException(Exception exception)