mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 09:12:37 +08:00
Refactor subscriptions manager
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Client;
|
||||
@@ -11,7 +12,7 @@ namespace MQTTnet.Core.Tests
|
||||
{
|
||||
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
|
||||
|
||||
public async Task<IMqttClient> ConnectTestClient(IMqttServer server, string clientId, MqttApplicationMessage willMessage = null)
|
||||
public async Task<IMqttClient> ConnectTestClient(string clientId, MqttApplicationMessage willMessage = null)
|
||||
{
|
||||
var adapterA = new TestMqttCommunicationAdapter();
|
||||
var adapterB = new TestMqttCommunicationAdapter();
|
||||
@@ -22,8 +23,6 @@ namespace MQTTnet.Core.Tests
|
||||
new TestMqttCommunicationAdapterFactory(adapterA),
|
||||
new MqttNetLogger());
|
||||
|
||||
var connected = WaitForClientToConnect(server, clientId);
|
||||
|
||||
FireClientAcceptedEvent(adapterB);
|
||||
|
||||
var options = new MqttClientOptions
|
||||
@@ -34,29 +33,11 @@ namespace MQTTnet.Core.Tests
|
||||
};
|
||||
|
||||
await client.ConnectAsync(options);
|
||||
await connected;
|
||||
SpinWait.SpinUntil(() => client.IsConnected);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
private static Task WaitForClientToConnect(IMqttServer s, string clientId)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
void Handler(object sender, Server.MqttClientConnectedEventArgs args)
|
||||
{
|
||||
if (args.Client.ClientId == clientId)
|
||||
{
|
||||
s.ClientConnected -= Handler;
|
||||
tcs.SetResult(null);
|
||||
}
|
||||
}
|
||||
|
||||
s.ClientConnected += Handler;
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private void FireClientAcceptedEvent(IMqttChannelAdapter adapter)
|
||||
{
|
||||
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(adapter));
|
||||
|
||||
Reference in New Issue
Block a user