From f94e17ad6054e5856efac3c239c4b7b19ecc5f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Fri, 6 Dec 2024 18:27:35 +0800 Subject: [PATCH] wait with timeout. --- Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs | 7 ++++--- Source/MQTTnet.Tests/Server/Events_Tests.cs | 14 +++++++------- Source/MQTTnet.Tests/Server/Session_Tests.cs | 16 +++++++--------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs b/Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs index b6edba296..bdf3cf43c 100644 --- a/Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs +++ b/Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs @@ -10,6 +10,7 @@ using MQTTnet.Internal; using MQTTnet.Protocol; using MQTTnet.Server; +using System; namespace MQTTnet.Tests.MQTTv5 { @@ -42,7 +43,7 @@ public async Task Will_Message_Send() var c2 = await testEnvironment.ConnectClient(clientOptions); c2.Dispose(); // Dispose will not send a DISCONNECT packet first so the will message must be sent. - await taskSource.Task; + await taskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); Assert.AreEqual(1, receivedMessagesCount); } @@ -210,14 +211,14 @@ public async Task Disconnect_with_Reason() // Test client should be connected now - var testClientId = await testClientIdTaskSource.Task; + var testClientId = await testClientIdTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsTrue(testClientId != null); // Have the server disconnect the client with AdministrativeAction reason await testEnvironment.Server.DisconnectClientAsync(testClientId, MqttDisconnectReasonCode.AdministrativeAction); - var disconnectReason = await disconnectReasonTaskSource.Task; + var disconnectReason = await disconnectReasonTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; // The reason should be returned to the client in the DISCONNECT packet Assert.AreEqual(MqttClientDisconnectReason.AdministrativeAction, disconnectReason); diff --git a/Source/MQTTnet.Tests/Server/Events_Tests.cs b/Source/MQTTnet.Tests/Server/Events_Tests.cs index 0ea75cc63..9e67e1f86 100644 --- a/Source/MQTTnet.Tests/Server/Events_Tests.cs +++ b/Source/MQTTnet.Tests/Server/Events_Tests.cs @@ -33,7 +33,7 @@ public async Task Fire_Client_Connected_Event() await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser")); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); @@ -63,7 +63,7 @@ public async Task Fire_Client_Disconnected_Event() var client = await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser")); await client.DisconnectAsync(); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); @@ -92,7 +92,7 @@ public async Task Fire_Client_Subscribed_Event() var client = await testEnvironment.ConnectClient(); await client.SubscribeAsync("The/Topic", MqttQualityOfServiceLevel.AtLeastOnce); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); @@ -121,7 +121,7 @@ public async Task Fire_Client_Unsubscribed_Event() var client = await testEnvironment.ConnectClient(); await client.UnsubscribeAsync("The/Topic"); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); @@ -149,7 +149,7 @@ public async Task Fire_Application_Message_Received_Event() var client = await testEnvironment.ConnectClient(); await client.PublishStringAsync("The_Topic", "The_Payload"); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); @@ -176,7 +176,7 @@ public async Task Fire_Started_Event() await server.StartAsync(); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); } @@ -199,7 +199,7 @@ public async Task Fire_Stopped_Event() await server.StopAsync(); - var eventArgs = await eventArgsTaskSource.Task; + var eventArgs = await eventArgsTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.IsNotNull(eventArgs); } diff --git a/Source/MQTTnet.Tests/Server/Session_Tests.cs b/Source/MQTTnet.Tests/Server/Session_Tests.cs index d626a2f23..262f9abff 100644 --- a/Source/MQTTnet.Tests/Server/Session_Tests.cs +++ b/Source/MQTTnet.Tests/Server/Session_Tests.cs @@ -114,7 +114,7 @@ public async Task Fire_Deleted_Event() // Act: Disconnect the client -> Event must be fired. await client.DisconnectAsync(); - var deletedEventFired = await deletedEventFiredTaskSource.Task; + var deletedEventFired = await deletedEventFiredTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; // Assert that the event was fired properly. Assert.IsTrue(deletedEventFired); @@ -182,7 +182,7 @@ void OnReceive() var sendClient = await testEnvironment.ConnectClient(option2); await sendClient.PublishStringAsync("aaa", "1"); - var hasReceive = await hasReceiveTaskSource.Task; + var hasReceive = await hasReceiveTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.AreEqual(true, hasReceive); } @@ -255,7 +255,7 @@ public async Task Session_Takeover() Assert.IsFalse(client1.IsConnected); Assert.IsTrue(client2.IsConnected); - var disconnectReason = await disconnectReasonTaskSource.Task; + var disconnectReason = await disconnectReasonTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(10d)); ; Assert.AreEqual(MqttClientDisconnectReason.SessionTakenOver, disconnectReason); } } @@ -318,7 +318,7 @@ public async Task Set_Session_Item() [TestMethod] public async Task Use_Clean_Session() - { + { using var testEnvironments = CreateMixedTestEnvironment(); foreach (var testEnvironment in testEnvironments) { @@ -348,12 +348,10 @@ public async Task Will_Message_Do_Not_Send_On_Takeover() await testEnvironment.StartServer(); // C1 will receive the last will! - var c1 = await testEnvironment.ConnectClient(); - var taskCompletionSource = new TaskCompletionSource(); + var c1 = await testEnvironment.ConnectClient(); c1.ApplicationMessageReceivedAsync += e => { - Interlocked.Increment(ref receivedMessagesCount); - taskCompletionSource.TrySetResult(); + Interlocked.Increment(ref receivedMessagesCount); return CompletedTask.Instance; }; @@ -367,7 +365,7 @@ public async Task Will_Message_Do_Not_Send_On_Takeover() // C3 will do the connection takeover. await testEnvironment.ConnectClient(clientOptions); - await taskCompletionSource.Task; + await LongTestDelay(); Assert.AreEqual(0, receivedMessagesCount); }