mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Use latest language features (#2176)
* Make use of language features line AsyncDispose, init etc. * Fix build errors * Improve Unit Tests * Improve Unit Tests * Improve Unit Tests * Update samples * Remove obsolete benchmarks * Code cleanup * Improve unit tests * Improve Unit Tests * Fix Unit Tests * Improve Unit Tests * Improve Unit Tests * Restore unit tests * Exclude load tests from unit tests
This commit is contained in:
@@ -7,449 +7,449 @@ using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Server.Exceptions;
|
||||
|
||||
namespace MQTTnet.Tests.Server
|
||||
namespace MQTTnet.Tests.Server;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class Injection_Tests : BaseTestClass
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class Injection_Tests : BaseTestClass
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level()
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level()
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
var receiver1 = await testEnvironment.ConnectClient();
|
||||
var receiver2 = await testEnvironment.ConnectClient();
|
||||
var messageReceivedHandler1 = testEnvironment.CreateApplicationMessageHandler(receiver1);
|
||||
var messageReceivedHandler2 = testEnvironment.CreateApplicationMessageHandler(receiver2);
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
await receiver1.SubscribeAsync("#");
|
||||
await receiver2.SubscribeAsync("#");
|
||||
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
|
||||
.WithTopic("InjectedOne").Build();
|
||||
|
||||
var enqueued = clientStatus.TryEnqueueApplicationMessage(message, out var injectResult);
|
||||
|
||||
Assert.IsTrue(enqueued);
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler1.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual(injectResult.PacketIdentifier, messageReceivedHandler1.ReceivedEventArgs[0].PacketIdentifier);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler1.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
|
||||
// The second receiver should NOT receive the message.
|
||||
Assert.AreEqual(0, messageReceivedHandler2.ReceivedEventArgs.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level_QueueOverflow_DropNewMessageStrategy()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropNewMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
var receiver1 = await testEnvironment.ConnectClient();
|
||||
var receiver2 = await testEnvironment.ConnectClient();
|
||||
var messageReceivedHandler1 = testEnvironment.CreateApplicationMessageHandler(receiver1);
|
||||
var messageReceivedHandler2 = testEnvironment.CreateApplicationMessageHandler(receiver2);
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
await receiver1.SubscribeAsync("#");
|
||||
await receiver2.SubscribeAsync("#");
|
||||
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
|
||||
.WithTopic("InjectedOne").Build();
|
||||
|
||||
var enqueued = clientStatus.TryEnqueueApplicationMessage(message, out var injectResult);
|
||||
|
||||
Assert.IsTrue(enqueued);
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler1.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual(injectResult.PacketIdentifier, messageReceivedHandler1.ReceivedEventArgs[0].PacketIdentifier);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler1.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
|
||||
// The second receiver should NOT receive the message.
|
||||
Assert.AreEqual(0, messageReceivedHandler2.ReceivedEventArgs.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level_QueueOverflow_DropNewMessageStrategy()
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropNewMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
var firstMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build(), out _);
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var secondMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build(), out _);
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var thirdMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build(), out _);
|
||||
var firstMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build(), out _);
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
// Due to the DropNewMessage strategy the third message will not be enqueued.
|
||||
// As a result, no existing messages in the queue will be dropped (evicted).
|
||||
Assert.IsTrue(firstMessageEnqueued);
|
||||
Assert.IsTrue(secondMessageEnqueued);
|
||||
Assert.IsFalse(thirdMessageEnqueued);
|
||||
var secondMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build(), out _);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsFalse(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
var thirdMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build(), out _);
|
||||
|
||||
// Due to the DropNewMessage strategy the third message will not be enqueued.
|
||||
// As a result, no existing messages in the queue will be dropped (evicted).
|
||||
Assert.IsTrue(firstMessageEnqueued);
|
||||
Assert.IsTrue(secondMessageEnqueued);
|
||||
Assert.IsFalse(thirdMessageEnqueued);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsFalse(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level_QueueOverflow_DropOldestQueuedMessageStrategy()
|
||||
[TestMethod]
|
||||
public async Task Enqueue_Application_Message_At_Session_Level_QueueOverflow_DropOldestQueuedMessageStrategy()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var firstMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build(), out _);
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
var secondMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build(), out _);
|
||||
|
||||
var thirdMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build(), out _);
|
||||
|
||||
// Due to the DropOldestQueuedMessage strategy, all messages will be enqueued initially.
|
||||
// But the second message will eventually be dropped (evicted) to make room for the third one.
|
||||
Assert.IsTrue(firstMessageEnqueued);
|
||||
Assert.IsTrue(secondMessageEnqueued);
|
||||
Assert.IsTrue(thirdMessageEnqueued);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsTrue(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
var receiver1 = await testEnvironment.ConnectClient();
|
||||
var receiver2 = await testEnvironment.ConnectClient();
|
||||
var messageReceivedHandler1 = testEnvironment.CreateApplicationMessageHandler(receiver1);
|
||||
var messageReceivedHandler2 = testEnvironment.CreateApplicationMessageHandler(receiver2);
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
await receiver1.SubscribeAsync("#");
|
||||
await receiver2.SubscribeAsync("#");
|
||||
|
||||
var mqttApplicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("InjectedOne")
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
|
||||
.Build();
|
||||
var injectResult = await clientStatus.DeliverApplicationMessageAsync(mqttApplicationMessage);
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler1.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual(injectResult.PacketIdentifier, messageReceivedHandler1.ReceivedEventArgs[0].PacketIdentifier);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler1.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
|
||||
// The second receiver should NOT receive the message.
|
||||
Assert.AreEqual(0, messageReceivedHandler2.ReceivedEventArgs.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level_QueueOverflow_DropNewMessageStrategy()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropNewMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var firstMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build(), out _);
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
var secondMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build(), out _);
|
||||
|
||||
var thirdMessageEnqueued = clientStatus.TryEnqueueApplicationMessage(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build(), out _);
|
||||
|
||||
// Due to the DropOldestQueuedMessage strategy, all messages will be enqueued initially.
|
||||
// But the second message will eventually be dropped (evicted) to make room for the third one.
|
||||
Assert.IsTrue(firstMessageEnqueued);
|
||||
Assert.IsTrue(secondMessageEnqueued);
|
||||
Assert.IsTrue(thirdMessageEnqueued);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsTrue(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
var receiver1 = await testEnvironment.ConnectClient();
|
||||
var receiver2 = await testEnvironment.ConnectClient();
|
||||
var messageReceivedHandler1 = testEnvironment.CreateApplicationMessageHandler(receiver1);
|
||||
var messageReceivedHandler2 = testEnvironment.CreateApplicationMessageHandler(receiver2);
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
|
||||
await receiver1.SubscribeAsync("#");
|
||||
await receiver2.SubscribeAsync("#");
|
||||
|
||||
var mqttApplicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("InjectedOne")
|
||||
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
|
||||
.Build();
|
||||
var injectResult = await clientStatus.DeliverApplicationMessageAsync(mqttApplicationMessage);
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler1.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual(injectResult.PacketIdentifier, messageReceivedHandler1.ReceivedEventArgs[0].PacketIdentifier);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler1.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
|
||||
// The second receiver should NOT receive the message.
|
||||
Assert.AreEqual(0, messageReceivedHandler2.ReceivedEventArgs.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level_QueueOverflow_DropNewMessageStrategy()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropNewMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
{
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
var firstMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build()));
|
||||
await LongTestDelay();
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var secondMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build()));
|
||||
await LongTestDelay();
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var thirdMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build()));
|
||||
await LongTestDelay();
|
||||
var firstMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build()));
|
||||
await LongTestDelay();
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
Task.WaitAny(firstMessageTask, secondMessageTask, thirdMessageTask);
|
||||
var secondMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
// Due to the DropNewMessage strategy the third message delivery will fail.
|
||||
// As a result, no existing messages in the queue will be dropped (evicted).
|
||||
Assert.AreEqual(firstMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(secondMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(thirdMessageTask.Status, TaskStatus.Faulted);
|
||||
Assert.IsTrue(thirdMessageTask.Exception?.InnerException is MqttPendingMessagesOverflowException);
|
||||
var thirdMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsFalse(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
Task.WaitAny(firstMessageTask, secondMessageTask, thirdMessageTask);
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level_QueueOverflow_DropOldestQueuedMessageStrategy()
|
||||
// Due to the DropNewMessage strategy the third message delivery will fail.
|
||||
// As a result, no existing messages in the queue will be dropped (evicted).
|
||||
Assert.AreEqual(firstMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(secondMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(thirdMessageTask.Status, TaskStatus.Faulted);
|
||||
Assert.IsTrue(thirdMessageTask.Exception?.InnerException is MqttPendingMessagesOverflowException);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsFalse(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Deliver_Application_Message_At_Session_Level_QueueOverflow_DropOldestQueuedMessageStrategy()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment(trackUnobservedTaskException: false);
|
||||
|
||||
var server = await testEnvironment.StartServer(
|
||||
builder => builder
|
||||
.WithMaxPendingMessagesPerClient(1)
|
||||
.WithPendingMessagesOverflowStrategy(MqttPendingMessagesOverflowStrategy.DropOldestQueuedMessage));
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var firstMessageOutboundPacketInterceptedTcs = new TaskCompletionSource();
|
||||
server.InterceptingOutboundPacketAsync += async args =>
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
// - The first message is dequeued normally and calls this delay
|
||||
// - The second message fills the outbound queue
|
||||
// - The third message overflows the outbound queue
|
||||
if (args.Packet is MqttPublishPacket)
|
||||
{
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
firstMessageOutboundPacketInterceptedTcs.SetResult();
|
||||
await Task.Delay(TimeSpan.FromDays(1), args.CancellationToken);
|
||||
}
|
||||
};
|
||||
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
var firstMessageEvicted = false;
|
||||
var secondMessageEvicted = false;
|
||||
var thirdMessageEvicted = false;
|
||||
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
server.QueuedApplicationMessageOverwrittenAsync += args =>
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
if (args.Packet is not MqttPublishPacket publishPacket)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var firstMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build()));
|
||||
await LongTestDelay();
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
var secondMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
var thirdMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
Task.WaitAny(firstMessageTask, secondMessageTask, thirdMessageTask);
|
||||
|
||||
// Due to the DropOldestQueuedMessage strategy, the second message delivery will fail
|
||||
// to make room for the third one.
|
||||
Assert.AreEqual(firstMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(secondMessageTask.Status, TaskStatus.Faulted);
|
||||
Assert.IsTrue(secondMessageTask.Exception?.InnerException is MqttPendingMessagesOverflowException);
|
||||
Assert.AreEqual(thirdMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsTrue(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Inject_ApplicationMessage_At_Server_Level()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var messageReceivedHandler = testEnvironment.CreateApplicationMessageHandler(receiver);
|
||||
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var injectedApplicationMessage = new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build();
|
||||
|
||||
await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(injectedApplicationMessage));
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Intercept_Injected_Application_Message()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttApplicationMessage interceptedMessage = null;
|
||||
server.InterceptingPublishAsync += eventArgs =>
|
||||
switch (publishPacket.Topic)
|
||||
{
|
||||
interceptedMessage = eventArgs.ApplicationMessage;
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
case "InjectedOne":
|
||||
firstMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedTwo":
|
||||
secondMessageEvicted = true;
|
||||
break;
|
||||
case "InjectedThree":
|
||||
thirdMessageEvicted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
var injectedApplicationMessage = new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build();
|
||||
await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(injectedApplicationMessage));
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await LongTestDelay();
|
||||
var status = await server.GetSessionsAsync();
|
||||
var clientStatus = status[0];
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
Assert.IsNotNull(interceptedMessage);
|
||||
}
|
||||
var firstMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build()));
|
||||
await LongTestDelay();
|
||||
await firstMessageOutboundPacketInterceptedTcs.Task;
|
||||
|
||||
var secondMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedTwo").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
var thirdMessageTask = Task.Run(
|
||||
() => clientStatus.DeliverApplicationMessageAsync(
|
||||
new MqttApplicationMessageBuilder().WithTopic("InjectedThree").Build()));
|
||||
await LongTestDelay();
|
||||
|
||||
Task.WaitAny(firstMessageTask, secondMessageTask, thirdMessageTask);
|
||||
|
||||
// Due to the DropOldestQueuedMessage strategy, the second message delivery will fail
|
||||
// to make room for the third one.
|
||||
Assert.AreEqual(firstMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
Assert.AreEqual(secondMessageTask.Status, TaskStatus.Faulted);
|
||||
Assert.IsTrue(secondMessageTask.Exception?.InnerException is MqttPendingMessagesOverflowException);
|
||||
Assert.AreEqual(thirdMessageTask.Status, TaskStatus.WaitingForActivation);
|
||||
|
||||
Assert.IsFalse(firstMessageEvicted);
|
||||
Assert.IsTrue(secondMessageEvicted);
|
||||
Assert.IsFalse(thirdMessageEvicted);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Inject_ApplicationMessage_At_Server_Level()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
var receiver = await testEnvironment.ConnectClient();
|
||||
|
||||
var messageReceivedHandler = testEnvironment.CreateApplicationMessageHandler(receiver);
|
||||
|
||||
await receiver.SubscribeAsync("#");
|
||||
|
||||
var injectedApplicationMessage = new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build();
|
||||
|
||||
await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(injectedApplicationMessage));
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.AreEqual(1, messageReceivedHandler.ReceivedEventArgs.Count);
|
||||
Assert.AreEqual("InjectedOne", messageReceivedHandler.ReceivedEventArgs[0].ApplicationMessage.Topic);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Intercept_Injected_Application_Message()
|
||||
{
|
||||
using var testEnvironment = CreateTestEnvironment();
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttApplicationMessage interceptedMessage = null;
|
||||
server.InterceptingPublishAsync += eventArgs =>
|
||||
{
|
||||
interceptedMessage = eventArgs.ApplicationMessage;
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
var injectedApplicationMessage = new MqttApplicationMessageBuilder().WithTopic("InjectedOne").Build();
|
||||
await server.InjectApplicationMessage(new InjectedMqttApplicationMessage(injectedApplicationMessage));
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
Assert.IsNotNull(interceptedMessage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user