improved api surface of mqttserver and fixed unittests

This commit is contained in:
JanEggers
2017-10-02 22:55:56 +02:00
parent 3079ad37ce
commit 1d78d675f7
9 changed files with 90 additions and 181 deletions
@@ -2,16 +2,52 @@
using System.Threading.Tasks;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Server;
using MQTTnet.Core.Client;
namespace MQTTnet.Core.Tests
{
public class TestMqttServerAdapter : IMqttServerAdapter
{
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
public event Action<IMqttCommunicationAdapter> ClientAccepted;
public void FireClientConnectedEvent(MqttClientConnectedEventArgs eventArgs)
public async Task<MqttClient> ConnectTestClient(IMqttServer server, string clientId, MqttApplicationMessage willMessage = null)
{
ClientConnected?.Invoke(this, eventArgs);
var adapterA = new TestMqttCommunicationAdapter();
var adapterB = new TestMqttCommunicationAdapter();
adapterA.Partner = adapterB;
adapterB.Partner = adapterA;
var client = new MqttClient(new MqttClientOptions() { ClientId = clientId }, adapterA);
var connected = WaitForClientToConnect(server, clientId);
FireClientAcceptedEvent(adapterB);
await client.ConnectAsync(willMessage);
await connected;
return client;
}
private static Task WaitForClientToConnect(IMqttServer s, string clientId)
{
var tcs = new TaskCompletionSource<object>();
EventHandler<MqttClientConnectedEventArgs> handler = null;
handler = (sender, args) =>
{
if (args.Client.ClientId == clientId)
{
s.ClientConnected -= handler;
tcs.SetResult(null);
}
};
s.ClientConnected += handler;
return tcs.Task;
}
private void FireClientAcceptedEvent(IMqttCommunicationAdapter adapter)
{
ClientAccepted?.Invoke(adapter);
}
public Task StartAsync(MqttServerOptions options)