mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 09:13:02 +08:00
Improve Unit Tests.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client;
|
||||
@@ -10,20 +9,16 @@ using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Client.Unsubscribing;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Tests.Mockups;
|
||||
|
||||
namespace MQTTnet.Tests.MQTTv5
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class Client_Tests
|
||||
public sealed class Client_Tests : BaseTestClass
|
||||
{
|
||||
public TestContext TestContext { get; set; }
|
||||
|
||||
[TestMethod]
|
||||
public async Task Connect_With_New_Mqtt_Features()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -64,7 +59,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Connect()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
await testEnvironment.ConnectClient(o => o.WithProtocolVersion(MqttProtocolVersion.V500).Build());
|
||||
@@ -74,7 +69,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Connect_And_Disconnect()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -86,7 +81,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Subscribe()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -112,7 +107,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Unsubscribe()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -130,7 +125,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Publish_QoS_0()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -145,7 +140,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Publish_QoS_1()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -160,7 +155,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Publish_QoS_2()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -175,7 +170,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Publish_With_Properties()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -204,7 +199,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Subscribe_And_Publish()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -230,7 +225,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
await client1.DisconnectAsync();
|
||||
|
||||
Assert.AreEqual(1, receivedMessages.Count);
|
||||
Assert.AreEqual("client1", receivedMessages[0].ClientId);
|
||||
Assert.AreEqual("Subscribe_And_Publish_client1", receivedMessages[0].ClientId);
|
||||
Assert.AreEqual("a", receivedMessages[0].ApplicationMessage.Topic);
|
||||
Assert.AreEqual("b", receivedMessages[0].ApplicationMessage.ConvertPayloadToString());
|
||||
}
|
||||
@@ -239,7 +234,7 @@ namespace MQTTnet.Tests.MQTTv5
|
||||
[TestMethod]
|
||||
public async Task Publish_And_Receive_New_Properties()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
@@ -17,7 +18,7 @@ namespace MQTTnet.Tests.Mockups
|
||||
{
|
||||
public TestClientWrapper(IMqttClient implementation, TestContext testContext)
|
||||
{
|
||||
Implementation = implementation;
|
||||
Implementation = implementation ?? throw new ArgumentNullException(nameof(implementation));
|
||||
TestContext = testContext;
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ namespace MQTTnet.Tests.Mockups
|
||||
var existingClientId = clientOptions.ClientId;
|
||||
if (existingClientId != null && !existingClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
clientOptions.ClientId = TestContext.TestName + existingClientId;
|
||||
clientOptions.ClientId = TestContext.TestName + "_" + existingClientId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
@@ -20,10 +19,8 @@ using MQTTnet.Tests.Mockups;
|
||||
namespace MQTTnet.Tests.Server
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class General_Tests
|
||||
public sealed class General_Tests : BaseTestClass
|
||||
{
|
||||
public TestContext TestContext { get; set; }
|
||||
|
||||
[TestMethod]
|
||||
[DataRow("", null)]
|
||||
[DataRow("", "")]
|
||||
@@ -51,7 +48,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Use_Empty_Client_ID()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -77,8 +74,7 @@ namespace MQTTnet.Tests.Server
|
||||
MqttQualityOfServiceLevel.AtMostOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtMostOnce,
|
||||
1,
|
||||
TestContext);
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -89,8 +85,7 @@ namespace MQTTnet.Tests.Server
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
1,
|
||||
TestContext);
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -101,14 +96,13 @@ namespace MQTTnet.Tests.Server
|
||||
MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
"A/B/C",
|
||||
MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
1,
|
||||
TestContext);
|
||||
1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Use_Clean_Session()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -122,7 +116,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Will_Message_Do_Not_Send_On_Clean_Disconnect()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var receivedMessagesCount = 0;
|
||||
|
||||
@@ -148,7 +142,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Will_Message_Do_Not_Send_On_Takeover()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var receivedMessagesCount = 0;
|
||||
|
||||
@@ -180,7 +174,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Will_Message_Send()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var receivedMessagesCount = 0;
|
||||
|
||||
@@ -206,7 +200,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Publish_From_Server()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
@@ -231,7 +225,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var receivedMessagesCount = 0;
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -262,7 +256,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Session_Takeover()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -290,7 +284,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task No_Messages_If_No_Subscription()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -320,7 +314,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Set_Subscription_At_Server()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
@@ -356,7 +350,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Shutdown_Disconnects_Clients_Gracefully()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder());
|
||||
|
||||
@@ -378,7 +372,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Handle_Clean_Disconnect()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder());
|
||||
|
||||
@@ -409,7 +403,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Client_Disconnect_Without_Errors()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
bool clientWasConnected;
|
||||
|
||||
@@ -440,7 +434,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
const int ClientCount = 50;
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
@@ -494,7 +488,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Intercept_Application_Message()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(
|
||||
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(
|
||||
@@ -517,7 +511,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var serverStorage = new TestServerStorage();
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithStorage(serverStorage));
|
||||
|
||||
@@ -534,7 +528,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Publish_After_Client_Connects()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
server.UseClientConnectedHandler(async e => { await server.PublishAsync("/test/1", "true", MqttQualityOfServiceLevel.ExactlyOnce, false); });
|
||||
@@ -564,7 +558,7 @@ namespace MQTTnet.Tests.Server
|
||||
context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
|
||||
}
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(Interceptor));
|
||||
|
||||
@@ -587,7 +581,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Send_Long_Body()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
const int PayloadSizeInMB = 30;
|
||||
const int CharCount = PayloadSizeInMB * 1024 * 1024;
|
||||
@@ -630,7 +624,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder().WithConnectionValidator(context => { context.ReasonCode = MqttConnectReasonCode.NotAuthorized; });
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
@@ -659,7 +653,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Same_Client_Id_Refuse_Connection()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
@@ -747,7 +741,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Same_Client_Id_Connect_Disconnect_Event_Order()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder());
|
||||
|
||||
@@ -832,7 +826,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Remove_Session()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder());
|
||||
|
||||
@@ -851,7 +845,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Stop_And_Restart()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
@@ -877,7 +871,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Do_Not_Send_Retained_Messages_For_Denied_Subscription()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(c =>
|
||||
{
|
||||
@@ -921,7 +915,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Collect_Messages_In_Disconnected_Session()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithPersistentSessions());
|
||||
|
||||
@@ -952,15 +946,14 @@ namespace MQTTnet.Tests.Server
|
||||
}
|
||||
}
|
||||
|
||||
static async Task TestPublishAsync(
|
||||
async Task TestPublishAsync(
|
||||
string topic,
|
||||
MqttQualityOfServiceLevel qualityOfServiceLevel,
|
||||
string topicFilter,
|
||||
MqttQualityOfServiceLevel filterQualityOfServiceLevel,
|
||||
int expectedReceivedMessagesCount,
|
||||
TestContext testContext)
|
||||
int expectedReceivedMessagesCount)
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(testContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var receivedMessagesCount = 0;
|
||||
|
||||
@@ -985,7 +978,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Intercept_Undelivered()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var undeliverd = string.Empty;
|
||||
|
||||
@@ -1009,7 +1002,7 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Intercept_ClientMessageQueue()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
.WithClientMessageQueueInterceptor(c => c.ApplicationMessage.Topic = "a"));
|
||||
@@ -1047,7 +1040,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
const string topic = "a";
|
||||
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
.WithClientMessageQueueInterceptor(c => { })); // Interceptor does nothing but has to be present.
|
||||
|
||||
@@ -1,42 +1,47 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Subscribing;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Tests.Mockups;
|
||||
|
||||
namespace MQTTnet.Tests.Server
|
||||
{
|
||||
[TestClass]
|
||||
public class Session_Tests
|
||||
{
|
||||
public TestContext TestContext { get; set; }
|
||||
|
||||
public sealed class Session_Tests : BaseTestClass
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task Set_Session_Item()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder()
|
||||
.WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
|
||||
.WithConnectionValidator(delegate(MqttConnectionValidatorContext context)
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
context.SessionItems["can_subscribe_x"] = true;
|
||||
context.SessionItems["default_payload"] = "Hello World";
|
||||
})
|
||||
.WithSubscriptionInterceptor(delegate (MqttSubscriptionInterceptorContext context)
|
||||
.WithSubscriptionInterceptor(delegate(MqttSubscriptionInterceptorContext context)
|
||||
{
|
||||
if (context.TopicFilter.Topic == "x")
|
||||
{
|
||||
context.AcceptSubscription = context.SessionItems["can_subscribe_x"] as bool? == true;
|
||||
if (context.SessionItems["can_subscribe_x"] as bool? == false)
|
||||
{
|
||||
context.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
}
|
||||
}
|
||||
})
|
||||
.WithApplicationMessageInterceptor(delegate (MqttApplicationMessageInterceptorContext context)
|
||||
.WithApplicationMessageInterceptor(delegate(MqttApplicationMessageInterceptorContext context)
|
||||
{
|
||||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(context.SessionItems["default_payload"] as string);
|
||||
context.ApplicationMessage.Payload =
|
||||
Encoding.UTF8.GetBytes(
|
||||
context.SessionItems["default_payload"] as string ?? String.Empty);
|
||||
});
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
@@ -44,7 +49,7 @@ namespace MQTTnet.Tests.Server
|
||||
string receivedPayload = null;
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
client.UseApplicationMessageReceivedHandler(delegate (MqttApplicationMessageReceivedEventArgs args)
|
||||
client.UseApplicationMessageReceivedHandler(delegate(MqttApplicationMessageReceivedEventArgs args)
|
||||
{
|
||||
receivedPayload = args.ApplicationMessage.ConvertPayloadToString();
|
||||
});
|
||||
@@ -65,10 +70,10 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Get_Session_Items_In_Status()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder()
|
||||
.WithConnectionValidator(delegate (MqttConnectionValidatorContext context)
|
||||
.WithConnectionValidator(delegate(MqttConnectionValidatorContext context)
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
context.SessionItems["can_subscribe_x"] = true;
|
||||
@@ -77,7 +82,7 @@ namespace MQTTnet.Tests.Server
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
await testEnvironment.ConnectClient();
|
||||
|
||||
var sessionStatus = await testEnvironment.Server.GetSessionStatusAsync();
|
||||
var session = sessionStatus.First();
|
||||
@@ -86,11 +91,10 @@ namespace MQTTnet.Tests.Server
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task Manage_Session_MaxParallel()
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
var serverOptions = new MqttServerOptionsBuilder();
|
||||
@@ -106,6 +110,61 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual(1, connectedClients.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Fire_Deleted_Event()
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
// Arrange client and server.
|
||||
var server = await testEnvironment.StartServer(o => o.WithPersistentSessions(false));
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
|
||||
// Arrange session status tracking.
|
||||
var status = await server.GetSessionStatusAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
var deletedEventFired = false;
|
||||
clientStatus.Deleted += (_, __) =>
|
||||
{
|
||||
deletedEventFired = true;
|
||||
};
|
||||
|
||||
// Act: Disconnect the client -> Event must be fired.
|
||||
await client.DisconnectAsync();
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
// Assert that the event was fired properly.
|
||||
Assert.IsTrue(deletedEventFired);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Inject_Application_Message()
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
// Arrange client and server.
|
||||
var server = await testEnvironment.StartServer();
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
var messageReceivedHandler = testEnvironment.CreateApplicationMessageHandler(client);
|
||||
|
||||
// Arrange session status tracking.
|
||||
var status = await server.GetSessionStatusAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
// Act: Inject the application message.
|
||||
await clientStatus.EnqueueApplicationMessageAsync(new MqttApplicationMessageBuilder()
|
||||
.WithTopic("injected_one").Build());
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(1, messageReceivedHandler.ReceivedApplicationMessages.Count);
|
||||
Assert.AreEqual("injected_one", messageReceivedHandler.ReceivedApplicationMessages[0].Topic);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<IMqttClient> TryConnect(TestEnvironment testEnvironment, MqttClientOptionsBuilder options)
|
||||
{
|
||||
@@ -113,10 +172,10 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
return await testEnvironment.ConnectClient(options);
|
||||
}
|
||||
catch (System.Exception)
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user