Add low level client.

This commit is contained in:
Christian Kratky
2020-04-01 20:43:00 +02:00
parent 0d39bbbc0c
commit 04ccdcd562
46 changed files with 711 additions and 418 deletions
@@ -1,6 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
@@ -10,10 +8,12 @@ using MQTTnet.Client.Publishing;
using MQTTnet.Client.Receiving;
using MQTTnet.Client.Subscribing;
using MQTTnet.Client.Unsubscribing;
using System.Threading;
using System.Threading.Tasks;
namespace MQTTnet.Tests.Mockups
{
public class TestClientWrapper : IMqttClient
public sealed class TestClientWrapper : IMqttClient
{
public TestClientWrapper(IMqttClient implementation, TestContext testContext)
{
@@ -22,6 +22,7 @@ namespace MQTTnet.Tests.Mockups
}
public IMqttClient Implementation { get; }
public TestContext TestContext { get; }
public bool IsConnected => Implementation.IsConnected;
@@ -34,28 +35,32 @@ namespace MQTTnet.Tests.Mockups
public Task<MqttClientAuthenticateResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
{
switch (options)
if (TestContext != null)
{
case MqttClientOptionsBuilder builder:
{
var existingClientId = builder.Build().ClientId;
if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
switch (options)
{
case MqttClientOptionsBuilder builder:
{
builder.WithClientId(TestContext.TestName + existingClientId);
var existingClientId = builder.Build().ClientId;
if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
{
builder.WithClientId(TestContext.TestName + existingClientId);
}
break;
}
}
break;
case MqttClientOptions op:
{
var existingClientId = op.ClientId;
if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
case MqttClientOptions op:
{
op.ClientId = TestContext.TestName + existingClientId;
var existingClientId = op.ClientId;
if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
{
op.ClientId = TestContext.TestName + existingClientId;
}
break;
}
}
break;
default:
break;
}
}
return Implementation.ConnectAsync(options, cancellationToken);
@@ -81,7 +86,7 @@ namespace MQTTnet.Tests.Mockups
return Implementation.SendExtendedAuthenticationExchangeDataAsync(data, cancellationToken);
}
public Task<Client.Subscribing.MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
public Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
{
return Implementation.SubscribeAsync(options, cancellationToken);
}
@@ -1,27 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Diagnostics;
using MQTTnet.Internal;
using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MQTTnet.Tests.Mockups
{
public class TestEnvironment : Disposable
public sealed class TestEnvironment : Disposable
{
private readonly MqttFactory _mqttFactory = new MqttFactory();
private readonly List<IMqttClient> _clients = new List<IMqttClient>();
private readonly IMqttNetLogger _serverLogger = new MqttNetLogger("server");
private readonly IMqttNetLogger _clientLogger = new MqttNetLogger("client");
readonly MqttFactory _mqttFactory = new MqttFactory();
readonly List<IMqttClient> _clients = new List<IMqttClient>();
readonly IMqttNetLogger _serverLogger = new MqttNetLogger("server");
readonly IMqttNetLogger _clientLogger = new MqttNetLogger("client");
private readonly List<string> _serverErrors = new List<string>();
private readonly List<string> _clientErrors = new List<string>();
readonly List<string> _serverErrors = new List<string>();
readonly List<string> _clientErrors = new List<string>();
private readonly List<Exception> _exceptions = new List<Exception>();
readonly List<Exception> _exceptions = new List<Exception>();
public IMqttServer Server { get; private set; }
@@ -37,36 +37,42 @@ namespace MQTTnet.Tests.Mockups
public TestContext TestContext { get; }
public TestEnvironment() : this(null)
{
}
public TestEnvironment(TestContext testContext)
{
TestContext = testContext;
_serverLogger.LogMessagePublished += (s, e) =>
{
if (e.TraceMessage.Level == MqttNetLogLevel.Error)
if (e.LogMessage.Level == MqttNetLogLevel.Error)
{
lock (_serverErrors)
{
_serverErrors.Add(e.TraceMessage.ToString());
_serverErrors.Add(e.LogMessage.ToString());
}
}
};
_clientLogger.LogMessagePublished += (s, e) =>
{
lock (_clientErrors)
if (e.LogMessage.Level == MqttNetLogLevel.Error)
{
if (e.TraceMessage.Level == MqttNetLogLevel.Error)
lock (_clientErrors)
{
_clientErrors.Add(e.TraceMessage.ToString());
_clientErrors.Add(e.LogMessage.ToString());
}
}
};
TestContext = testContext;
}
public IMqttClient CreateClient()
{
var client = _mqttFactory.CreateMqttClient(_clientLogger);
_clients.Add(client);
return new TestClientWrapper(client, TestContext);
}
@@ -90,15 +96,17 @@ namespace MQTTnet.Tests.Mockups
public Task<IMqttClient> ConnectClientAsync()
{
return ConnectClientAsync(new MqttClientOptionsBuilder() );
return ConnectClientAsync(new MqttClientOptionsBuilder());
}
public async Task<IMqttClient> ConnectClientAsync(MqttClientOptionsBuilder options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
options = options.WithTcpServer("localhost", ServerPort);
var client = CreateClient();
await client.ConnectAsync(options.WithTcpServer("localhost", ServerPort).Build());
await client.ConnectAsync(options.Build());
return client;
}
@@ -150,6 +158,7 @@ namespace MQTTnet.Tests.Mockups
throw new Exception($"{_exceptions.Count} exceptions tracked.\r\n" + string.Join(Environment.NewLine, _exceptions));
}
}
base.Dispose(disposing);
}
+10 -10
View File
@@ -1,20 +1,15 @@
using System;
using MQTTnet.Diagnostics;
using MQTTnet.Diagnostics;
using System;
namespace MQTTnet.Tests.Mockups
{
public class TestLogger : IMqttNetLogger, IMqttNetChildLogger
public class TestLogger : IMqttNetLogger
{
public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;
IMqttNetChildLogger IMqttNetLogger.CreateChildLogger(string source)
public IMqttNetLogger CreateChildLogger(string source = null)
{
return new MqttNetChildLogger(this, source);
}
IMqttNetChildLogger IMqttNetChildLogger.CreateChildLogger(string source)
{
return new MqttNetChildLogger(this, source);
return new TestLogger();
}
public void Verbose(string message, params object[] parameters)
@@ -36,5 +31,10 @@ namespace MQTTnet.Tests.Mockups
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
{
}
public void Publish(MqttNetLogLevel logLevel, string message, object[] parameters, Exception exception)
{
throw new NotImplementedException();
}
}
}
@@ -12,8 +12,8 @@ namespace MQTTnet.Tests.Mockups
{
_adapter = adapter;
}
public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options, IMqttNetChildLogger logger)
public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options, IMqttNetLogger logger)
{
return _adapter;
}
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Client.Publishing;
using MQTTnet.Client.Receiving;
using MQTTnet.Server;
using MQTTnet.Server.Status;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MQTTnet.Tests.Mockups
{
public class TestServerWrapper : IMqttServer
public sealed class TestServerWrapper : IMqttServer
{
public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment)
{
@@ -60,22 +60,29 @@ namespace MQTTnet.Tests.Mockups
public Task StartAsync(IMqttServerOptions options)
{
switch (options)
if (TestContext != null)
{
case MqttServerOptionsBuilder builder:
if (builder.Build().ConnectionValidator == null)
{
builder.WithConnectionValidator(ConnectionValidator);
}
break;
case MqttServerOptions op:
if (op.ConnectionValidator == null)
{
op.ConnectionValidator = new MqttServerConnectionValidatorDelegate(ConnectionValidator);
}
break;
default:
break;
switch (options)
{
case MqttServerOptionsBuilder builder:
{
if (builder.Build().ConnectionValidator == null)
{
builder.WithConnectionValidator(ConnectionValidator);
}
break;
}
case MqttServerOptions op:
{
if (op.ConnectionValidator == null)
{
op.ConnectionValidator = new MqttServerConnectionValidatorDelegate(ConnectionValidator);
}
break;
}
}
}
return Implementation.StartAsync(options);