Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Oct 23, 2022
1 parent d2ce9d6 commit 8cf5537
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Source/MQTTnet.Tests/Client/ManagedMqttClient_Tests.cs
Expand Up @@ -279,7 +279,7 @@ public async Task Subscriptions_Are_Cleared_At_Logout()
});

// Wait a bit for the retained message to be available
await Task.Delay(1000);
await LongTestDelay();

await sendingClient.DisconnectAsync();

Expand All @@ -288,7 +288,8 @@ public async Task Subscriptions_Are_Cleared_At_Logout()
var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort);

var receivedManagedMessages = new List<MqttApplicationMessage>();
var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetEventLogger());

var managedClient = testEnvironment.Factory.CreateManagedMqttClient(testEnvironment.CreateClient());
managedClient.ApplicationMessageReceivedAsync += e =>
{
receivedManagedMessages.Add(e.ApplicationMessage);
Expand All @@ -299,25 +300,27 @@ public async Task Subscriptions_Are_Cleared_At_Logout()

await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder().WithClientOptions(clientOptions).WithAutoReconnectDelay(TimeSpan.FromSeconds(1)).Build());

await Task.Delay(1000);
await LongTestDelay();

Assert.AreEqual(1, receivedManagedMessages.Count);

await managedClient.StopAsync();

await Task.Delay(500);
await LongTestDelay();

await managedClient.StartAsync(new ManagedMqttClientOptionsBuilder().WithClientOptions(clientOptions).WithAutoReconnectDelay(TimeSpan.FromSeconds(1)).Build());

await Task.Delay(1000);
await LongTestDelay();

// After reconnect and then some delay, the retained message must not be received,
// showing that the subscriptions were cleared
Assert.AreEqual(1, receivedManagedMessages.Count);

// Make sure that it gets received after subscribing again.
await managedClient.SubscribeAsync("topic");
await Task.Delay(500);

await LongTestDelay();

Assert.AreEqual(2, receivedManagedMessages.Count);
}
}
Expand Down

0 comments on commit 8cf5537

Please sign in to comment.