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
@@ -18,10 +18,12 @@ namespace MQTTnet.Tests.MQTTv5
[TestClass]
public class Client_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Connect_With_New_Mqtt_Features()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -61,7 +63,7 @@ namespace MQTTnet.Tests.MQTTv5
[TestMethod]
public async Task Connect_With_AssignedClientId()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
string serverConnectedClientId = null;
string serverDisconnectedClientId = null;
@@ -357,7 +359,7 @@ namespace MQTTnet.Tests.MQTTv5
[TestMethod]
public async Task Publish_And_Receive_New_Properties()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -13,10 +13,12 @@ namespace MQTTnet.Tests.MQTTv5
[TestClass]
public class Feature_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Use_User_Properties()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -11,10 +11,12 @@ namespace MQTTnet.Tests.MQTTv5
[TestClass]
public class Server_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Will_Message_Send()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -18,6 +18,8 @@ namespace MQTTnet.Tests
[TestClass]
public class ManagedMqttClient_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Drop_New_Messages_On_Full_Queue()
{
@@ -54,7 +56,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task ManagedClients_Will_Message_Send()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -88,7 +90,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Start_Stop()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var factory = new MqttFactory();
@@ -115,7 +117,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Storage_Queue_Drains()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
testEnvironment.IgnoreServerLogErrors = true;
@@ -167,7 +169,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscriptions_And_Unsubscriptions_Are_Made_And_Reestablished_At_Reconnect()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var unmanagedClient = testEnvironment.CreateClient();
var managedClient = await CreateManagedClientAsync(testEnvironment, unmanagedClient);
@@ -232,7 +234,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscriptions_Subscribe_Only_New_Subscriptions()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var managedClient = await CreateManagedClientAsync(testEnvironment);
@@ -265,7 +267,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscriptions_Are_Published_Immediately()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
// Use a long connection check interval to verify that the subscriptions
// do not depend on the connection check interval anymore
@@ -289,7 +291,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscriptions_Are_Cleared_At_Logout()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var managedClient = await CreateManagedClientAsync(testEnvironment);
@@ -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);
}
}
}
+18 -16
View File
@@ -20,10 +20,12 @@ namespace MQTTnet.Tests
[TestClass]
public class Client_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Send_Reply_In_Message_Handler_For_Same_Client()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();
@@ -57,7 +59,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Send_Reply_In_Message_Handler()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var client1 = await testEnvironment.ConnectClientAsync();
@@ -89,7 +91,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Reconnect()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();
@@ -112,7 +114,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Reconnect_While_Server_Offline()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
@@ -149,7 +151,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Reconnect_From_Disconnected_Event()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
@@ -189,7 +191,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task PacketIdentifier_In_Publish_Result()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();
@@ -238,7 +240,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Fire_Disconnected_Event_On_Server_Shutdown()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();
@@ -290,7 +292,7 @@ namespace MQTTnet.Tests
// is an issue).
const int MessagesCount = 50;
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -330,7 +332,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Send_Reply_For_Any_Received_Message()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -374,7 +376,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Publish_With_Correct_Retain_Flag()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -405,7 +407,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscribe_In_Callback_Events()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -444,7 +446,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Message_Send_Retry()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
testEnvironment.IgnoreServerLogErrors = true;
@@ -488,7 +490,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task NoConnectedHandler_Connect_DoesNotThrowException()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -501,7 +503,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task NoDisconnectedHandler_Disconnect_DoesNotThrowException()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var client = await testEnvironment.ConnectClientAsync();
@@ -516,7 +518,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Frequent_Connects()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -560,7 +562,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task No_Payload()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -18,7 +18,7 @@ namespace MQTTnet.Tests
//This test compares
//1. correct logID
string logId = "logId";
bool invalidLogIdOccured = false;
string invalidLogId = null;
//2. if the total log calls are the same for global and local
int globalLogCount = 0;
@@ -31,7 +31,7 @@ namespace MQTTnet.Tests
{
if (logId != e.TraceMessage.LogId)
{
invalidLogIdOccured = true;
invalidLogId = e.TraceMessage.LogId;
}
Interlocked.Increment(ref globalLogCount);
});
@@ -42,7 +42,7 @@ namespace MQTTnet.Tests
{
if (logId != e.TraceMessage.LogId)
{
invalidLogIdOccured = true;
invalidLogId = e.TraceMessage.LogId;
}
Interlocked.Increment(ref localLogCount);
};
@@ -69,7 +69,7 @@ namespace MQTTnet.Tests
MqttNetGlobalLogger.LogMessagePublished -= globalLog;
}
Assert.IsFalse(invalidLogIdOccured);
Assert.IsNull(invalidLogId);
Assert.AreNotEqual(0, globalLogCount);
Assert.AreEqual(globalLogCount, localLogCount);
}
+5 -3
View File
@@ -18,6 +18,8 @@ namespace MQTTnet.Tests
[TestClass]
public class RPC_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public Task Execute_Success_With_QoS_0()
{
@@ -58,7 +60,7 @@ namespace MQTTnet.Tests
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
public async Task Execute_Timeout()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -73,7 +75,7 @@ namespace MQTTnet.Tests
[ExpectedException(typeof(MqttCommunicationTimedOutException))]
public async Task Execute_With_Custom_Topic_Names()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -86,7 +88,7 @@ namespace MQTTnet.Tests
private async Task Execute_Success(MqttQualityOfServiceLevel qosLevel, MqttProtocolVersion protocolVersion)
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var responseSender = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithProtocolVersion(protocolVersion));
@@ -11,10 +11,12 @@ namespace MQTTnet.Tests
[TestClass]
public class RoundtripTime_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Round_Trip_Time()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
var receiverClient = await testEnvironment.ConnectClientAsync();
@@ -13,10 +13,12 @@ namespace MQTTnet.Tests
[TestClass]
public class Server_Status_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Show_Client_And_Session_Statistics()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
@@ -31,8 +33,8 @@ namespace MQTTnet.Tests
Assert.AreEqual(2, clientStatus.Count);
Assert.AreEqual(2, sessionStatus.Count);
Assert.IsTrue(clientStatus.Any(s => s.ClientId == "client1"));
Assert.IsTrue(clientStatus.Any(s => s.ClientId == "client2"));
Assert.IsTrue(clientStatus.Any(s => s.ClientId == c1.Options.ClientId));
Assert.IsTrue(clientStatus.Any(s => s.ClientId == c2.Options.ClientId));
await c1.DisconnectAsync();
await c2.DisconnectAsync();
@@ -50,7 +52,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Disconnect_Client()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
@@ -62,7 +64,7 @@ namespace MQTTnet.Tests
Assert.AreEqual(1, clientStatus.Count);
Assert.IsTrue(clientStatus.Any(s => s.ClientId == "client1"));
Assert.IsTrue(clientStatus.Any(s => s.ClientId == c1.Options.ClientId));
await clientStatus.First().DisconnectAsync();
@@ -79,7 +81,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Keep_Persistent_Session()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithPersistentSessions());
@@ -111,7 +113,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Track_Sent_Application_Messages()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithPersistentSessions());
@@ -132,7 +134,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Track_Sent_Packets()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithPersistentSessions());
+53 -60
View File
@@ -22,10 +22,12 @@ namespace MQTTnet.Tests
[TestClass]
public class Server_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Use_Empty_Client_ID()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -51,7 +53,8 @@ namespace MQTTnet.Tests
MqttQualityOfServiceLevel.AtMostOnce,
"A/B/C",
MqttQualityOfServiceLevel.AtMostOnce,
1);
1,
TestContext);
}
[TestMethod]
@@ -62,7 +65,8 @@ namespace MQTTnet.Tests
MqttQualityOfServiceLevel.AtLeastOnce,
"A/B/C",
MqttQualityOfServiceLevel.AtLeastOnce,
1);
1,
TestContext);
}
[TestMethod]
@@ -73,13 +77,14 @@ namespace MQTTnet.Tests
MqttQualityOfServiceLevel.ExactlyOnce,
"A/B/C",
MqttQualityOfServiceLevel.ExactlyOnce,
1);
1,
TestContext);
}
[TestMethod]
public async Task Use_Clean_Session()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -93,7 +98,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Will_Message_Do_Not_Send()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -119,7 +124,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Will_Message_Send()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -145,7 +150,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Intercept_Subscription()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(
c =>
@@ -184,7 +189,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscribe_Unsubscribe()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -204,7 +209,7 @@ namespace MQTTnet.Tests
var subscribeEventCalled = false;
server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedHandlerDelegate(e =>
{
subscribeEventCalled = e.TopicFilter.Topic == "a" && e.ClientId == "c1";
subscribeEventCalled = e.TopicFilter.Topic == "a" && e.ClientId == c1.Options.ClientId;
});
await c1.SubscribeAsync(new TopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
@@ -218,7 +223,7 @@ namespace MQTTnet.Tests
var unsubscribeEventCalled = false;
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e =>
{
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == "c1";
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == c1.Options.ClientId;
});
await c1.UnsubscribeAsync("a");
@@ -238,7 +243,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscribe_Multiple_In_Single_Request()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -271,7 +276,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Subscribe_Multiple_In_Multiple_Request()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -310,7 +315,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Publish_From_Server()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
@@ -336,7 +341,7 @@ namespace MQTTnet.Tests
var receivedMessagesCount = 0;
var locked = new object();
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -378,7 +383,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Session_Takeover()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -400,7 +405,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task No_Messages_If_No_Subscription()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -433,7 +438,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Set_Subscription_At_Server()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e => server.SubscribeAsync(e.ClientId, "topic1"));
@@ -464,7 +469,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Shutdown_Disconnects_Clients_Gracefully()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder());
@@ -486,7 +491,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Handle_Clean_Disconnect()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder());
@@ -515,7 +520,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Client_Disconnect_Without_Errors()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
bool clientWasConnected;
@@ -546,7 +551,7 @@ namespace MQTTnet.Tests
{
const int ClientCount = 50;
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
@@ -598,7 +603,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Retained_Messages_Flow()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var retainedMessage = new MqttApplicationMessageBuilder().WithTopic("r").WithPayload("r").WithRetainFlag().Build();
@@ -635,7 +640,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Receive_No_Retained_Message_After_Subscribe()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -658,7 +663,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Receive_Retained_Message_After_Subscribe()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync();
@@ -689,7 +694,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Clear_Retained_Message_With_Empty_Payload()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -717,7 +722,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Clear_Retained_Message_With_Null_Payload()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var receivedMessagesCount = 0;
@@ -745,7 +750,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Intercept_Application_Message()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(
@@ -768,7 +773,7 @@ namespace MQTTnet.Tests
{
var serverStorage = new TestServerStorage();
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithStorage(serverStorage));
@@ -785,7 +790,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Publish_After_Client_Connects()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync();
server.UseClientConnectedHandler(async e =>
@@ -818,7 +823,7 @@ namespace MQTTnet.Tests
context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
}
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(Interceptor));
@@ -844,7 +849,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Send_Long_Body()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
const int PayloadSizeInMB = 30;
const int CharCount = PayloadSizeInMB * 1024 * 1024;
@@ -892,28 +897,15 @@ namespace MQTTnet.Tests
context.ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized;
});
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
await testEnvironment.StartServerAsync(serverOptions);
try
{
await testEnvironment.ConnectClientAsync();
Assert.Fail("An exception should be raised.");
}
catch (Exception exception)
{
if (exception is MqttConnectingFailedException connectingFailedException)
{
Assert.AreEqual(MqttClientConnectResultCode.NotAuthorized, connectingFailedException.ResultCode);
}
else
{
Assert.Fail("Wrong exception.");
}
}
var connectingFailedException = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(() => testEnvironment.ConnectClientAsync());
Assert.AreEqual(MqttClientConnectResultCode.NotAuthorized, connectingFailedException.ResultCode);
}
}
@@ -934,7 +926,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Same_Client_Id_Refuse_Connection()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
@@ -1023,7 +1015,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Same_Client_Id_Connect_Disconnect_Event_Order()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder());
@@ -1107,7 +1099,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Remove_Session()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder());
@@ -1126,7 +1118,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Stop_And_Restart()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
testEnvironment.IgnoreClientLogErrors = true;
@@ -1152,7 +1144,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Close_Idle_Connection()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));
@@ -1181,7 +1173,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Send_Garbage()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));
@@ -1214,7 +1206,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Do_Not_Send_Retained_Messages_For_Denied_Subscription()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(c =>
{
@@ -1258,7 +1250,7 @@ namespace MQTTnet.Tests
[TestMethod]
public async Task Collect_Messages_In_Disconnected_Session()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var server = await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithPersistentSessions());
@@ -1285,7 +1277,7 @@ namespace MQTTnet.Tests
Assert.AreEqual(0, clientStatus.Count);
Assert.AreEqual(2, sessionStatus.Count);
Assert.AreEqual(3, sessionStatus.First(s => s.ClientId == "a").PendingApplicationMessagesCount);
Assert.AreEqual(3, sessionStatus.First(s => s.ClientId == client1.Options.ClientId).PendingApplicationMessagesCount);
}
}
@@ -1294,9 +1286,10 @@ namespace MQTTnet.Tests
MqttQualityOfServiceLevel qualityOfServiceLevel,
string topicFilter,
MqttQualityOfServiceLevel filterQualityOfServiceLevel,
int expectedReceivedMessagesCount)
int expectedReceivedMessagesCount,
TestContext testContext)
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(testContext))
{
var receivedMessagesCount = 0;
+3 -1
View File
@@ -11,10 +11,12 @@ namespace MQTTnet.Tests
[TestClass]
public class Session_Tests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task Set_Session_Item()
{
using (var testEnvironment = new TestEnvironment())
using (var testEnvironment = new TestEnvironment(TestContext))
{
var serverOptions = new MqttServerOptionsBuilder()
.WithConnectionValidator(delegate (MqttConnectionValidatorContext context)