Skip to content

Commit

Permalink
wait with timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Dec 6, 2024
1 parent 41af5c5 commit f94e17a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
7 changes: 4 additions & 3 deletions Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using MQTTnet.Internal;
using MQTTnet.Protocol;
using MQTTnet.Server;
using System;

namespace MQTTnet.Tests.MQTTv5
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions Source/MQTTnet.Tests/Server/Events_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
16 changes: 7 additions & 9 deletions Source/MQTTnet.Tests/Server/Session_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
};

Expand All @@ -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);
}
Expand Down

0 comments on commit f94e17a

Please sign in to comment.