mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-22 21:03:06 +08:00
fixed tests
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
public static class TestServerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// publishes a message with a client and waits in the server until a message with the same topic is received
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task PublishAndWaitForAsync(this IMqttClient client, IMqttServer server, MqttApplicationMessage message)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
EventHandler<MqttApplicationMessageReceivedEventArgs> handler = (sender, args) =>
|
||||
{
|
||||
if (args.ApplicationMessage.Topic == message.Topic)
|
||||
{
|
||||
tcs.SetResult(true);
|
||||
}
|
||||
};
|
||||
server.ApplicationMessageReceived += handler;
|
||||
|
||||
try
|
||||
{
|
||||
await client.PublishAsync(message);
|
||||
await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
server.ApplicationMessageReceived -= handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user