mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 17:22:37 +08:00
Try to fix CI builds.
This commit is contained in:
@@ -153,9 +153,8 @@ namespace MQTTnet.Implementations
|
|||||||
#else
|
#else
|
||||||
await _socket.ConnectAsync(host, port).ConfigureAwait(false);
|
await _socket.ConnectAsync(host, port).ConfigureAwait(false);
|
||||||
#endif
|
#endif
|
||||||
|
_networkStream = new NetworkStream(_socket, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
_networkStream = new NetworkStream(_socket, true);
|
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
catch (ObjectDisposedException)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -295,39 +295,67 @@ namespace MQTTnet.Tests
|
|||||||
{
|
{
|
||||||
using (var testEnvironment = new TestEnvironment(TestContext))
|
using (var testEnvironment = new TestEnvironment(TestContext))
|
||||||
{
|
{
|
||||||
var managedClient = await CreateManagedClientAsync(testEnvironment);
|
await testEnvironment.StartServerAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
var sendingClient = await testEnvironment.ConnectClientAsync();
|
var sendingClient = await testEnvironment.ConnectClientAsync().ConfigureAwait(false);
|
||||||
await sendingClient.PublishAsync(new MqttApplicationMessage
|
await sendingClient.PublishAsync(new MqttApplicationMessage
|
||||||
{ Topic = "topic", Payload = new byte[] { 1 }, Retain = true });
|
{
|
||||||
|
Topic = "topic",
|
||||||
|
Payload = new byte[] { 1 },
|
||||||
|
Retain = true
|
||||||
|
});
|
||||||
|
|
||||||
// Wait a bit for the retained message to be available
|
// Wait a bit for the retained message to be available
|
||||||
await Task.Delay(500);
|
await Task.Delay(500);
|
||||||
|
|
||||||
await managedClient.SubscribeAsync("topic");
|
await sendingClient.DisconnectAsync();
|
||||||
|
|
||||||
await SetupReceivingOfMessages(managedClient, 1);
|
// Now use the managed client and check if subscriptions get cleared properly.
|
||||||
|
|
||||||
await managedClient.StopAsync();
|
|
||||||
|
|
||||||
var clientOptions = new MqttClientOptionsBuilder()
|
var clientOptions = new MqttClientOptionsBuilder()
|
||||||
.WithTcpServer("localhost", testEnvironment.ServerPort);
|
.WithTcpServer("localhost", testEnvironment.ServerPort);
|
||||||
|
|
||||||
|
var managedOptions = new ManagedMqttClientOptionsBuilder()
|
||||||
|
.WithClientOptions(clientOptions)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var receivedManagedMessages = new List<MqttApplicationMessage>();
|
||||||
|
var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger());
|
||||||
|
managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(c =>
|
||||||
|
{
|
||||||
|
receivedManagedMessages.Add(c.ApplicationMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
await managedClient.SubscribeAsync("topic");
|
||||||
|
|
||||||
await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder()
|
await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder()
|
||||||
.WithClientOptions(clientOptions)
|
.WithClientOptions(clientOptions)
|
||||||
.WithAutoReconnectDelay(TimeSpan.FromSeconds(1))
|
.WithAutoReconnectDelay(TimeSpan.FromSeconds(1))
|
||||||
.Build());
|
.Build());
|
||||||
|
|
||||||
var messages = new List<MqttApplicationMessage>();
|
await Task.Delay(500);
|
||||||
managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(r =>
|
|
||||||
{
|
Assert.AreEqual(1, receivedManagedMessages.Count);
|
||||||
messages.Add(r.ApplicationMessage);
|
|
||||||
});
|
await managedClient.StopAsync();
|
||||||
|
|
||||||
await Task.Delay(500);
|
await Task.Delay(500);
|
||||||
|
|
||||||
|
await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder()
|
||||||
|
.WithClientOptions(clientOptions)
|
||||||
|
.WithAutoReconnectDelay(TimeSpan.FromSeconds(1))
|
||||||
|
.Build());
|
||||||
|
|
||||||
|
await Task.Delay(1000);
|
||||||
|
|
||||||
// After reconnect and then some delay, the retained message must not be received,
|
// After reconnect and then some delay, the retained message must not be received,
|
||||||
// showing that the subscriptions were cleared
|
// showing that the subscriptions were cleared
|
||||||
Assert.AreEqual(0, messages.Count);
|
Assert.AreEqual(1, receivedManagedMessages.Count);
|
||||||
|
|
||||||
|
// Make sure that it gets received after subscribing again.
|
||||||
|
await managedClient.SubscribeAsync("topic");
|
||||||
|
await Task.Delay(500);
|
||||||
|
Assert.AreEqual(2, receivedManagedMessages.Count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user