Add low level client to test environment.

This commit is contained in:
Christian
2021-09-05 13:31:23 +02:00
parent aa9f8d8ef3
commit 2aa4d1b77a
@@ -18,6 +18,7 @@ namespace MQTTnet.Tests.Mockups
public sealed class TestEnvironment : IDisposable public sealed class TestEnvironment : IDisposable
{ {
readonly MqttFactory _mqttFactory = new MqttFactory(); readonly MqttFactory _mqttFactory = new MqttFactory();
readonly List<ILowLevelMqttClient> _lowLevelClients = new List<ILowLevelMqttClient>();
readonly List<IMqttClient> _clients = new List<IMqttClient>(); readonly List<IMqttClient> _clients = new List<IMqttClient>();
readonly List<string> _serverErrors = new List<string>(); readonly List<string> _serverErrors = new List<string>();
readonly List<string> _clientErrors = new List<string>(); readonly List<string> _clientErrors = new List<string>();
@@ -79,11 +80,6 @@ namespace MQTTnet.Tests.Mockups
}; };
} }
public async Task<IMqttRpcClient> ConnectRpcClientAsync(IMqttRpcClientOptions options)
{
return new MqttRpcClient(await ConnectClientAsync(), options);
}
public IMqttClient CreateClient() public IMqttClient CreateClient()
{ {
lock (_clients) lock (_clients)
@@ -95,54 +91,11 @@ namespace MQTTnet.Tests.Mockups
} }
} }
public Task<IMqttServer> StartServerAsync()
{
return StartServerAsync(new MqttServerOptionsBuilder());
}
public async Task<IMqttServer> StartServerAsync(MqttServerOptionsBuilder options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (Server != null)
{
throw new InvalidOperationException("Server already started.");
}
Server = new TestServerWrapper(_mqttFactory.CreateMqttServer(ServerLogger), TestContext, this);
options.WithDefaultEndpointPort(ServerPort);
options.WithMaxPendingMessagesPerClient(int.MaxValue);
await Server.StartAsync(options.Build()).ConfigureAwait(false);
return Server;
}
public Task<IMqttClient> ConnectClientAsync() public Task<IMqttClient> ConnectClientAsync()
{ {
return ConnectClientAsync(new MqttClientOptionsBuilder()); return ConnectClientAsync(new MqttClientOptionsBuilder());
} }
public Task<ILowLevelMqttClient> ConnectLowLevelClientAsync()
{
return ConnectLowLevelClientAsync(o => { });
}
public async Task<ILowLevelMqttClient> ConnectLowLevelClientAsync(Action<MqttClientOptionsBuilder> optionsBuilder)
{
if (optionsBuilder == null) throw new ArgumentNullException(nameof(optionsBuilder));
var options = new MqttClientOptionsBuilder();
options = options.WithTcpServer("127.0.0.1", ServerPort);
optionsBuilder.Invoke(options);
var client = new MqttFactory().CreateLowLevelMqttClient();
await client.ConnectAsync(options.Build(), CancellationToken.None).ConfigureAwait(false);
return client;
}
public async Task<IMqttClient> ConnectClientAsync(Action<MqttClientOptionsBuilder> optionsBuilder) public async Task<IMqttClient> ConnectClientAsync(Action<MqttClientOptionsBuilder> optionsBuilder)
{ {
if (optionsBuilder == null) throw new ArgumentNullException(nameof(optionsBuilder)); if (optionsBuilder == null) throw new ArgumentNullException(nameof(optionsBuilder));
@@ -179,6 +132,65 @@ namespace MQTTnet.Tests.Mockups
return client; return client;
} }
public ILowLevelMqttClient CreateLowLevelClient()
{
lock (_clients)
{
var client = _mqttFactory.CreateLowLevelMqttClient(ClientLogger);
_lowLevelClients.Add(client);
return client;
}
}
public Task<ILowLevelMqttClient> ConnectLowLevelClientAsync()
{
return ConnectLowLevelClientAsync(o => { });
}
public async Task<ILowLevelMqttClient> ConnectLowLevelClientAsync(Action<MqttClientOptionsBuilder> optionsBuilder)
{
if (optionsBuilder == null) throw new ArgumentNullException(nameof(optionsBuilder));
var options = new MqttClientOptionsBuilder();
options = options.WithTcpServer("127.0.0.1", ServerPort);
optionsBuilder.Invoke(options);
var client = CreateLowLevelClient();
await client.ConnectAsync(options.Build(), CancellationToken.None).ConfigureAwait(false);
return client;
}
public async Task<IMqttRpcClient> ConnectRpcClientAsync(IMqttRpcClientOptions options)
{
return new MqttRpcClient(await ConnectClientAsync(), options);
}
public Task<IMqttServer> StartServerAsync()
{
return StartServerAsync(new MqttServerOptionsBuilder());
}
public async Task<IMqttServer> StartServerAsync(MqttServerOptionsBuilder options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
if (Server != null)
{
throw new InvalidOperationException("Server already started.");
}
Server = new TestServerWrapper(_mqttFactory.CreateMqttServer(ServerLogger), TestContext, this);
options.WithDefaultEndpointPort(ServerPort);
options.WithMaxPendingMessagesPerClient(int.MaxValue);
await Server.StartAsync(options.Build()).ConfigureAwait(false);
return Server;
}
public void ThrowIfLogErrors() public void ThrowIfLogErrors()
{ {
lock (_serverErrors) lock (_serverErrors)
@@ -198,6 +210,14 @@ namespace MQTTnet.Tests.Mockups
} }
} }
public void TrackException(Exception exception)
{
lock (_exceptions)
{
_exceptions.Add(exception);
}
}
public void Dispose() public void Dispose()
{ {
foreach (var mqttClient in _clients) foreach (var mqttClient in _clients)
@@ -216,6 +236,11 @@ namespace MQTTnet.Tests.Mockups
} }
} }
foreach (var lowLevelMqttClient in _lowLevelClients)
{
lowLevelMqttClient.Dispose();
}
try try
{ {
Server?.StopAsync().GetAwaiter().GetResult(); Server?.StopAsync().GetAwaiter().GetResult();
@@ -236,13 +261,5 @@ namespace MQTTnet.Tests.Mockups
throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions)); throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
} }
} }
public void TrackException(Exception exception)
{
lock (_exceptions)
{
_exceptions.Add(exception);
}
}
} }
} }