mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Refactor async/await and ConcurrentDictionary usage.
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using System.Threading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Server;
|
||||
@@ -12,39 +16,31 @@ namespace MQTTnet.Core.Tests
|
||||
[TestMethod]
|
||||
public void KeepAlive_Timeout()
|
||||
{
|
||||
var timeoutCalledCount = 0;
|
||||
var clientSession = new TestClientSession();
|
||||
var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
|
||||
|
||||
var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
|
||||
{
|
||||
timeoutCalledCount++;
|
||||
}, new MqttNetLogger().CreateChildLogger(""));
|
||||
|
||||
Assert.AreEqual(0, timeoutCalledCount);
|
||||
Assert.AreEqual(0, clientSession.StopCalledCount);
|
||||
|
||||
monitor.Start(1, CancellationToken.None);
|
||||
|
||||
Assert.AreEqual(0, timeoutCalledCount);
|
||||
Assert.AreEqual(0, clientSession.StopCalledCount);
|
||||
|
||||
Thread.Sleep(2000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
|
||||
|
||||
Assert.AreEqual(1, timeoutCalledCount);
|
||||
Assert.AreEqual(1, clientSession.StopCalledCount);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void KeepAlive_NoTimeout()
|
||||
{
|
||||
var timeoutCalledCount = 0;
|
||||
var clientSession = new TestClientSession();
|
||||
var monitor = new MqttClientKeepAliveMonitor(clientSession, new MqttNetLogger().CreateChildLogger());
|
||||
|
||||
var monitor = new MqttClientKeepAliveMonitor(string.Empty, delegate
|
||||
{
|
||||
timeoutCalledCount++;
|
||||
}, new MqttNetLogger().CreateChildLogger(""));
|
||||
|
||||
Assert.AreEqual(0, timeoutCalledCount);
|
||||
Assert.AreEqual(0, clientSession.StopCalledCount);
|
||||
|
||||
monitor.Start(1, CancellationToken.None);
|
||||
|
||||
Assert.AreEqual(0, timeoutCalledCount);
|
||||
Assert.AreEqual(0, clientSession.StopCalledCount);
|
||||
|
||||
// Simulate traffic.
|
||||
Thread.Sleep(1000); // Internally the keep alive timeout is multiplied with 1.5 as per protocol specification.
|
||||
@@ -53,11 +49,57 @@ namespace MQTTnet.Core.Tests
|
||||
monitor.PacketReceived(new MqttPublishPacket());
|
||||
Thread.Sleep(1000);
|
||||
|
||||
Assert.AreEqual(0, timeoutCalledCount);
|
||||
Assert.AreEqual(0, clientSession.StopCalledCount);
|
||||
|
||||
Thread.Sleep(2000);
|
||||
|
||||
Assert.AreEqual(1, timeoutCalledCount);
|
||||
Assert.AreEqual(1, clientSession.StopCalledCount);
|
||||
}
|
||||
|
||||
private class TestClientSession : IMqttClientSession
|
||||
{
|
||||
public string ClientId { get; }
|
||||
|
||||
public int StopCalledCount { get; set; }
|
||||
|
||||
public void FillStatus(MqttClientSessionStatus status)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void EnqueueApplicationMessage(MqttClientSession senderClientSession, MqttPublishPacket publishPacket)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void ClearPendingApplicationMessages()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public Task<bool> RunAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Stop(MqttClientDisconnectType disconnectType)
|
||||
{
|
||||
StopCalledCount++;
|
||||
}
|
||||
|
||||
public Task SubscribeAsync(IList<TopicFilter> topicFilters)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public Task UnsubscribeAsync(IList<string> topicFilters)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
var result = sm.CheckSubscriptions(pp);
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
|
||||
}
|
||||
@@ -41,13 +35,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
|
||||
};
|
||||
|
||||
var result = sm.CheckSubscriptions(pp);
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
|
||||
}
|
||||
@@ -63,13 +51,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
|
||||
};
|
||||
|
||||
var result = sm.CheckSubscriptions(pp);
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
}
|
||||
@@ -84,13 +66,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/X",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsFalse(sm.CheckSubscriptions("A/B/X", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -103,19 +79,13 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
sm.Subscribe(sp);
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsTrue(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsTrue(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
|
||||
|
||||
var up = new MqttUnsubscribePacket();
|
||||
up.TopicFilters.Add("A/B/C");
|
||||
sm.Unsubscribe(up);
|
||||
|
||||
Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsFalse(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,48 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class PerformanceTest
|
||||
{
|
||||
public static void Run()
|
||||
public static void RunClientOnly()
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "127.0.0.1"
|
||||
},
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
client.ConnectAsync(options).GetAwaiter().GetResult();
|
||||
|
||||
var message = CreateMessage();
|
||||
var stopwatch = new Stopwatch();
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var sentMessagesCount = 0;
|
||||
|
||||
stopwatch.Restart();
|
||||
while (stopwatch.ElapsedMilliseconds < 1000)
|
||||
{
|
||||
client.PublishAsync(message).GetAwaiter().GetResult();
|
||||
sentMessagesCount++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second. #" + (i + 1));
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RunClientAndServer()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -22,6 +22,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
Console.WriteLine("5 = Start public broker test");
|
||||
Console.WriteLine("6 = Start server & client");
|
||||
Console.WriteLine("7 = Client flow test");
|
||||
Console.WriteLine("8 = Start performance test (client only)");
|
||||
Console.WriteLine("9 = Start server (no trace)");
|
||||
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.KeyChar == '1')
|
||||
@@ -34,7 +36,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
else if (pressedKey.KeyChar == '3')
|
||||
{
|
||||
PerformanceTest.Run();
|
||||
PerformanceTest.RunClientAndServer();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == '4')
|
||||
@@ -53,6 +55,16 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Task.Run(ClientFlowTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '8')
|
||||
{
|
||||
PerformanceTest.RunClientOnly();
|
||||
return;
|
||||
}
|
||||
else if (pressedKey.KeyChar == '9')
|
||||
{
|
||||
ServerTest.RunEmptyServer();
|
||||
return;
|
||||
}
|
||||
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
|
||||
@@ -8,12 +8,19 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class ServerTest
|
||||
{
|
||||
public static void RunEmptyServer()
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
mqttServer.StartAsync(new MqttServerOptions()).GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
MqttNetConsoleLogger.ForwardToConsole();
|
||||
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
|
||||
Reference in New Issue
Block a user