mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-26 08:02:59 +08:00
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:
@@ -0,0 +1,94 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.ExtendedAuthenticationExchange;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Client.Unsubscribing;
|
||||
|
||||
namespace MQTTnet.Tests.Mockups
|
||||
{
|
||||
public class TestClientWrapper : IMqttClient
|
||||
{
|
||||
public TestClientWrapper(IMqttClient implementation, TestContext testContext)
|
||||
{
|
||||
Implementation = implementation;
|
||||
TestContext = testContext;
|
||||
}
|
||||
|
||||
public IMqttClient Implementation { get; }
|
||||
public TestContext TestContext { get; }
|
||||
|
||||
public bool IsConnected => Implementation.IsConnected;
|
||||
|
||||
public IMqttClientOptions Options => Implementation.Options;
|
||||
|
||||
public IMqttClientConnectedHandler ConnectedHandler { get => Implementation.ConnectedHandler; set => Implementation.ConnectedHandler = value; }
|
||||
public IMqttClientDisconnectedHandler DisconnectedHandler { get => Implementation.DisconnectedHandler; set => Implementation.DisconnectedHandler = value; }
|
||||
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get => Implementation.ApplicationMessageReceivedHandler; set => Implementation.ApplicationMessageReceivedHandler = value; }
|
||||
|
||||
public Task<MqttClientAuthenticateResult> ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
switch (options)
|
||||
{
|
||||
case MqttClientOptionsBuilder builder:
|
||||
{
|
||||
var existingClientId = builder.Build().ClientId;
|
||||
if (!existingClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
builder.WithClientId(TestContext.TestName + existingClientId);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case MqttClientOptions op:
|
||||
{
|
||||
var existingClientId = op.ClientId;
|
||||
if (!existingClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
op.ClientId = TestContext.TestName + existingClientId;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Implementation.ConnectAsync(options, cancellationToken);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync(MqttClientDisconnectOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.DisconnectAsync(options, cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Implementation.Dispose();
|
||||
}
|
||||
|
||||
public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.PublishAsync(applicationMessage, cancellationToken);
|
||||
}
|
||||
|
||||
public Task SendExtendedAuthenticationExchangeDataAsync(MqttExtendedAuthenticationExchangeData data, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.SendExtendedAuthenticationExchangeDataAsync(data, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<Client.Subscribing.MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.SubscribeAsync(options, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.UnsubscribeAsync(options, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Server.Status;
|
||||
|
||||
namespace MQTTnet.Tests.Mockups
|
||||
{
|
||||
public class TestServerWrapper : IMqttServer
|
||||
{
|
||||
public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment)
|
||||
{
|
||||
Implementation = implementation;
|
||||
TestContext = testContext;
|
||||
TestEnvironment = testEnvironment;
|
||||
}
|
||||
|
||||
public IMqttServer Implementation { get; }
|
||||
public TestContext TestContext { get; }
|
||||
public TestEnvironment TestEnvironment { get; }
|
||||
public IMqttServerStartedHandler StartedHandler { get => Implementation.StartedHandler; set => Implementation.StartedHandler = value; }
|
||||
public IMqttServerStoppedHandler StoppedHandler { get => Implementation.StoppedHandler; set => Implementation.StoppedHandler = value; }
|
||||
public IMqttServerClientConnectedHandler ClientConnectedHandler { get => Implementation.ClientConnectedHandler; set => Implementation.ClientConnectedHandler = value; }
|
||||
public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler { get => Implementation.ClientDisconnectedHandler; set => Implementation.ClientDisconnectedHandler = value; }
|
||||
public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler { get => Implementation.ClientSubscribedTopicHandler; set => Implementation.ClientSubscribedTopicHandler = value; }
|
||||
public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler { get => Implementation.ClientUnsubscribedTopicHandler; set => Implementation.ClientUnsubscribedTopicHandler = value; }
|
||||
|
||||
public IMqttServerOptions Options => Implementation.Options;
|
||||
|
||||
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get => Implementation.ApplicationMessageReceivedHandler; set => Implementation.ApplicationMessageReceivedHandler = value; }
|
||||
|
||||
public Task ClearRetainedApplicationMessagesAsync()
|
||||
{
|
||||
return Implementation.ClearRetainedApplicationMessagesAsync();
|
||||
}
|
||||
|
||||
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
|
||||
{
|
||||
return Implementation.GetClientStatusAsync();
|
||||
}
|
||||
|
||||
public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
|
||||
{
|
||||
return Implementation.GetRetainedApplicationMessagesAsync();
|
||||
}
|
||||
|
||||
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
|
||||
{
|
||||
return Implementation.GetSessionStatusAsync();
|
||||
}
|
||||
|
||||
public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.PublishAsync(applicationMessage, cancellationToken);
|
||||
}
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
{
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Implementation.StartAsync(options);
|
||||
}
|
||||
|
||||
public void ConnectionValidator(MqttConnectionValidatorContext ctx)
|
||||
{
|
||||
if (!ctx.ClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
TestEnvironment.TrackException(new InvalidOperationException($"invalid client connected '{ctx.ClientId}'"));
|
||||
ctx.ReasonCode = Protocol.MqttConnectReasonCode.ClientIdentifierNotValid;
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
return Implementation.StopAsync();
|
||||
}
|
||||
|
||||
public Task SubscribeAsync(string clientId, ICollection<TopicFilter> topicFilters)
|
||||
{
|
||||
return Implementation.SubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
|
||||
public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
|
||||
{
|
||||
return Implementation.UnsubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user