mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-29 09:52:55 +08:00
Refactor interceptors etc. to support async.
This commit is contained in:
@@ -21,7 +21,7 @@ namespace MQTTnet.Tests
|
||||
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
|
||||
threads[i] = Task.Run(async () =>
|
||||
{
|
||||
using (var releaser = await @lock.LockAsync(CancellationToken.None))
|
||||
using (var releaser = await @lock.WaitAsync(CancellationToken.None))
|
||||
{
|
||||
var localI = globalI;
|
||||
await Task.Delay(10); // Increase the chance for wrong data.
|
||||
|
||||
@@ -82,9 +82,10 @@ namespace MQTTnet.Tests
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Stop(MqttClientDisconnectType disconnectType)
|
||||
public Task StopAsync(MqttClientDisconnectType disconnectType)
|
||||
{
|
||||
StopCalledCount++;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task SubscribeAsync(IList<TopicFilter> topicFilters)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -52,7 +53,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_WillMessage()
|
||||
public async Task MqttServer_Will_Message()
|
||||
{
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
@@ -84,7 +85,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_SubscribeUnsubscribe()
|
||||
public async Task MqttServer_Subscribe_Unsubscribe()
|
||||
{
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
@@ -230,7 +231,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_SessionTakeover()
|
||||
public async Task MqttServer_Session_Takeover()
|
||||
{
|
||||
var server = new MqttFactory().CreateMqttServer();
|
||||
try
|
||||
@@ -299,38 +300,43 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_HandleCleanDisconnect()
|
||||
public async Task MqttServer_Handle_Clean_Disconnect()
|
||||
{
|
||||
var serverAdapter = new MqttTcpServerAdapter(new MqttNetLogger().CreateChildLogger());
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
var s = new MqttFactory().CreateMqttServer();
|
||||
try
|
||||
{
|
||||
var clientConnectedCalled = 0;
|
||||
var clientDisconnectedCalled = 0;
|
||||
|
||||
var clientConnectedCalled = 0;
|
||||
var clientDisconnectedCalled = 0;
|
||||
s.ClientConnected += (_, __) => clientConnectedCalled++;
|
||||
s.ClientDisconnected += (_, __) => clientDisconnectedCalled++;
|
||||
|
||||
s.ClientConnected += (_, __) => clientConnectedCalled++;
|
||||
s.ClientDisconnected += (_, __) => clientDisconnectedCalled++;
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost")
|
||||
.Build();
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost")
|
||||
.Build();
|
||||
await s.StartAsync(new MqttServerOptions());
|
||||
|
||||
await s.StartAsync(new MqttServerOptions());
|
||||
var c1 = new MqttFactory().CreateMqttClient();
|
||||
|
||||
var c1 = new MqttFactory().CreateMqttClient();
|
||||
await c1.ConnectAsync(clientOptions);
|
||||
|
||||
await c1.ConnectAsync(clientOptions);
|
||||
await Task.Delay(100);
|
||||
|
||||
await Task.Delay(100);
|
||||
await c1.DisconnectAsync();
|
||||
|
||||
await c1.DisconnectAsync();
|
||||
await Task.Delay(100);
|
||||
|
||||
await Task.Delay(100);
|
||||
await s.StopAsync();
|
||||
|
||||
await s.StopAsync();
|
||||
await Task.Delay(100);
|
||||
|
||||
await Task.Delay(100);
|
||||
|
||||
Assert.AreEqual(clientConnectedCalled, clientDisconnectedCalled);
|
||||
Assert.AreEqual(clientConnectedCalled, clientDisconnectedCalled);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await s.StopAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -410,11 +416,13 @@ namespace MQTTnet.Tests
|
||||
.WithPayload("value" + j).WithRetainFlag().Build()).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
Thread.Sleep(100);
|
||||
|
||||
client.DisconnectAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
});
|
||||
|
||||
await Task.Delay(100);
|
||||
await Task.Delay(1000);
|
||||
|
||||
var retainedMessages = server.GetRetainedMessages();
|
||||
|
||||
@@ -432,7 +440,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_RetainedMessagesFlow()
|
||||
public async Task MqttServer_Retained_Messages_Flow()
|
||||
{
|
||||
var retainedMessage = new MqttApplicationMessageBuilder().WithTopic("r").WithPayload("r").WithRetainFlag().Build();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
@@ -440,9 +448,9 @@ namespace MQTTnet.Tests
|
||||
await s.StartAsync(new MqttServerOptions());
|
||||
var c1 = await serverAdapter.ConnectTestClient("c1");
|
||||
await c1.PublishAsync(retainedMessage);
|
||||
Thread.Sleep(500);
|
||||
await Task.Delay(500);
|
||||
await c1.DisconnectAsync();
|
||||
Thread.Sleep(500);
|
||||
await Task.Delay(500);
|
||||
|
||||
var receivedMessages = 0;
|
||||
var c2 = await serverAdapter.ConnectTestClient("c2");
|
||||
@@ -468,7 +476,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_NoRetainedMessage()
|
||||
public async Task MqttServer_No_Retained_Message()
|
||||
{
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
@@ -498,7 +506,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_RetainedMessage()
|
||||
public async Task MqttServer_Retained_Message()
|
||||
{
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
@@ -535,7 +543,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_ClearRetainedMessage()
|
||||
public async Task MqttServer_Clear_Retained_Message()
|
||||
{
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
|
||||
@@ -567,7 +575,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_PersistRetainedMessage()
|
||||
public async Task MqttServer_Persist_Retained_Message()
|
||||
{
|
||||
var storage = new TestStorage();
|
||||
var serverAdapter = new TestMqttServerAdapter();
|
||||
@@ -629,7 +637,7 @@ namespace MQTTnet.Tests
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttServerOptions { ApplicationMessageInterceptor = Interceptor };
|
||||
var options = new MqttServerOptions { ApplicationMessageInterceptor = new MqttServerApplicationMessageInterceptorDelegate(c => Interceptor(c)) };
|
||||
|
||||
await s.StartAsync(options);
|
||||
|
||||
@@ -692,7 +700,7 @@ namespace MQTTnet.Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_ConnectionDenied()
|
||||
public async Task MqttServer_Connection_Denied()
|
||||
{
|
||||
var server = new MqttFactory().CreateMqttServer();
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
@@ -791,7 +799,6 @@ namespace MQTTnet.Tests
|
||||
Assert.AreEqual("cdcd", flow);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_StopAndRestart()
|
||||
{
|
||||
@@ -820,6 +827,66 @@ namespace MQTTnet.Tests
|
||||
await server.StopAsync();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_Close_Idle_Connection()
|
||||
{
|
||||
var server = new MqttFactory().CreateMqttServer();
|
||||
|
||||
try
|
||||
{
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(4)).Build());
|
||||
|
||||
var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
await client.ConnectAsync("localhost", 1883);
|
||||
|
||||
// Don't send anything. The server should close the connection.
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
try
|
||||
{
|
||||
await client.ReceiveAsync(new ArraySegment<byte>(new byte[10]), SocketFlags.Partial);
|
||||
Assert.Fail("Receive should throw an exception.");
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await server.StopAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttServer_Send_Garbage()
|
||||
{
|
||||
var server = new MqttFactory().CreateMqttServer();
|
||||
|
||||
try
|
||||
{
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(4)).Build());
|
||||
|
||||
var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
await client.ConnectAsync("localhost", 1883);
|
||||
await client.SendAsync(Encoding.UTF8.GetBytes("Garbage"), SocketFlags.None);
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
try
|
||||
{
|
||||
await client.ReceiveAsync(new ArraySegment<byte>(new byte[10]), SocketFlags.Partial);
|
||||
Assert.Fail("Receive should throw an exception.");
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await server.StopAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private class TestStorage : IMqttServerStorage
|
||||
{
|
||||
public IList<MqttApplicationMessage> Messages = new List<MqttApplicationMessage>();
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace MQTTnet.Tests
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp);
|
||||
sm.SubscribeAsync(sp).GetAwaiter().GetResult();
|
||||
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
@@ -31,7 +31,7 @@ namespace MQTTnet.Tests
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
|
||||
|
||||
sm.Subscribe(sp);
|
||||
sm.SubscribeAsync(sp).GetAwaiter().GetResult();
|
||||
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
@@ -47,7 +47,7 @@ namespace MQTTnet.Tests
|
||||
sp.TopicFilters.Add(new TopicFilter { Topic = "#", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
|
||||
sp.TopicFilters.Add(new TopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
|
||||
|
||||
sm.Subscribe(sp);
|
||||
sm.SubscribeAsync(sp).GetAwaiter().GetResult();
|
||||
|
||||
var result = sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
@@ -62,7 +62,7 @@ namespace MQTTnet.Tests
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp);
|
||||
sm.SubscribeAsync(sp).GetAwaiter().GetResult();
|
||||
|
||||
Assert.IsFalse(sm.CheckSubscriptions("A/B/X", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
|
||||
}
|
||||
@@ -75,7 +75,7 @@ namespace MQTTnet.Tests
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp);
|
||||
sm.SubscribeAsync(sp).GetAwaiter().GetResult();
|
||||
|
||||
Assert.IsTrue(sm.CheckSubscriptions("A/B/C", MqttQualityOfServiceLevel.AtMostOnce).IsSubscribed);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
ConnectionValidator = new MqttServerConnectionValidatorDelegate(p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
@@ -32,11 +32,11 @@ namespace MQTTnet.TestApp.NetCore
|
||||
p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
Storage = new RetainedMessageHandler(),
|
||||
|
||||
ApplicationMessageInterceptor = context =>
|
||||
ApplicationMessageInterceptor = new MqttServerApplicationMessageInterceptorDelegate(context =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#"))
|
||||
{
|
||||
@@ -50,8 +50,9 @@ namespace MQTTnet.TestApp.NetCore
|
||||
context.AcceptPublish = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
},
|
||||
SubscriptionInterceptor = context =>
|
||||
}),
|
||||
|
||||
SubscriptionInterceptor = new MqttServerSubscriptionInterceptorDelegate(context =>
|
||||
{
|
||||
if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin")
|
||||
{
|
||||
@@ -63,7 +64,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
context.AcceptSubscription = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
// Extend the timestamp for all messages from clients.
|
||||
|
||||
@@ -450,7 +450,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
return;
|
||||
}
|
||||
|
||||
var sessions = _mqttServer.GetClientSessionsStatus();
|
||||
var sessions = _mqttServer.GetClientSessionsStatusAsync().GetAwaiter().GetResult();
|
||||
_sessions.Clear();
|
||||
|
||||
foreach (var session in sessions)
|
||||
@@ -568,7 +568,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
var options = new MqttServerOptions();
|
||||
|
||||
options.ConnectionValidator = c =>
|
||||
options.ConnectionValidator = new MqttServerConnectionValidatorDelegate(c =>
|
||||
{
|
||||
if (c.ClientId.Length < 10)
|
||||
{
|
||||
@@ -589,7 +589,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
|
||||
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
|
||||
};
|
||||
});
|
||||
|
||||
var factory = new MqttFactory();
|
||||
var mqttServer = factory.CreateMqttServer();
|
||||
@@ -633,7 +633,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
};
|
||||
|
||||
options.ConnectionValidator = c =>
|
||||
options.ConnectionValidator = new MqttServerConnectionValidatorDelegate(c =>
|
||||
{
|
||||
if (c.ClientId != "Highlander")
|
||||
{
|
||||
@@ -642,7 +642,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
|
||||
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
|
||||
};
|
||||
});
|
||||
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
await mqttServer.StartAsync(optionsBuilder.Build());
|
||||
@@ -652,7 +652,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
// Setup client validator.
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = c =>
|
||||
ConnectionValidator = new MqttServerConnectionValidatorDelegate(c =>
|
||||
{
|
||||
if (c.ClientId.Length < 10)
|
||||
{
|
||||
@@ -673,7 +673,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
|
||||
c.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user