Skip to content

Commit def7629

Browse files
authored
TaskCompletionSource: Use TaskCreationOptions.RunContinuationsAsynchronously (#228)
1 parent 2308a69 commit def7629

File tree

14 files changed

+29
-30
lines changed

14 files changed

+29
-30
lines changed

Source/HiveMQtt/Client/HiveMQClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public async Task<ConnectResult> ConnectAsync()
8181

8282
await this.Connection.ConnectAsync().ConfigureAwait(true);
8383

84-
var taskCompletionSource = new TaskCompletionSource<ConnAckPacket>();
84+
var taskCompletionSource = new TaskCompletionSource<ConnAckPacket>(TaskCreationOptions.RunContinuationsAsynchronously);
8585
void TaskHandler(object? sender, OnConnAckReceivedEventArgs args) => taskCompletionSource.SetResult(args.ConnAckPacket);
8686

8787
EventHandler<OnConnAckReceivedEventArgs> eventHandler = TaskHandler;
@@ -161,7 +161,7 @@ public async Task<bool> DisconnectAsync(DisconnectOptions? options = null)
161161
// Once this is set, no more incoming packets or outgoing will be accepted
162162
this.Connection.State = ConnectState.Disconnecting;
163163

164-
var taskCompletionSource = new TaskCompletionSource<DisconnectPacket>();
164+
var taskCompletionSource = new TaskCompletionSource<DisconnectPacket>(TaskCreationOptions.RunContinuationsAsynchronously);
165165
void TaskHandler(object? sender, OnDisconnectSentEventArgs args) => taskCompletionSource.SetResult(args.DisconnectPacket);
166166
EventHandler<OnDisconnectSentEventArgs> eventHandler = TaskHandler;
167167
this.OnDisconnectSent += eventHandler;
@@ -315,7 +315,7 @@ public async Task<SubscribeResult> SubscribeAsync(SubscribeOptions options)
315315
var subscribePacket = new SubscribePacket(options, (ushort)packetIdentifier);
316316

317317
// Setup the task completion source to wait for the SUBACK
318-
var taskCompletionSource = new TaskCompletionSource<SubAckPacket>();
318+
var taskCompletionSource = new TaskCompletionSource<SubAckPacket>(TaskCreationOptions.RunContinuationsAsynchronously);
319319
void TaskHandler(object? sender, OnSubAckReceivedEventArgs args)
320320
{
321321
if (args.SubAckPacket.PacketIdentifier == subscribePacket.PacketIdentifier)
@@ -429,7 +429,7 @@ public async Task<UnsubscribeResult> UnsubscribeAsync(UnsubscribeOptions unsubOp
429429
var packetIdentifier = await this.Connection.PacketIDManager.GetAvailablePacketIDAsync().ConfigureAwait(false);
430430
var unsubscribePacket = new UnsubscribePacket(unsubOptions, (ushort)packetIdentifier);
431431

432-
var taskCompletionSource = new TaskCompletionSource<UnsubAckPacket>();
432+
var taskCompletionSource = new TaskCompletionSource<UnsubAckPacket>(TaskCreationOptions.RunContinuationsAsynchronously);
433433
void TaskHandler(object? sender, OnUnsubAckReceivedEventArgs args)
434434
{
435435
if (args.UnsubAckPacket.PacketIdentifier == unsubscribePacket.PacketIdentifier)

Source/HiveMQtt/MQTT5/Packets/PublishPacket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ internal virtual void OnPublishQoS1CompleteEventLauncher(PubAckPacket packet)
119119
/// Valid for outgoing Publish messages QoS 1. A TaskCompletionSource that is set when the QoS 1 publish transaction is complete.
120120
/// </para>
121121
/// </summary>
122-
public TaskCompletionSource<PubAckPacket> OnPublishQoS1CompleteTCS { get; } = new();
122+
public TaskCompletionSource<PubAckPacket> OnPublishQoS1CompleteTCS { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
123123

124124
/// <summary>
125125
/// Valid for outgoing Publish messages QoS 2. An event that is fired after the the QoS 2 PubComp is received.
@@ -150,7 +150,7 @@ internal virtual void OnPublishQoS2CompleteEventLauncher(List<ControlPacket> pac
150150
/// Valid for outgoing Publish messages QoS 2. A TaskCompletionSource that is set when the QoS 2 publish transaction is complete.
151151
/// </para>
152152
/// </summary>
153-
public TaskCompletionSource<List<ControlPacket>> OnPublishQoS2CompleteTCS { get; } = new();
153+
public TaskCompletionSource<List<ControlPacket>> OnPublishQoS2CompleteTCS { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
154154

155155
/// <summary>
156156
/// Decode the received MQTT Publish packet.

Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal virtual void OnCompleteEventLauncher(SubAckPacket packet)
9696
/// Valid for outgoing subscribe packets. A TaskCompletionSource that is set when the subscribe transaction is complete.
9797
/// </para>
9898
/// </summary>
99-
public TaskCompletionSource<SubAckPacket> OnCompleteTCS { get; } = new();
99+
public TaskCompletionSource<SubAckPacket> OnCompleteTCS { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
100100

101101
/// <summary>
102102
/// Encode this packet to be sent on the wire.

Tests/HiveMQtt.Test/HiveMQClient/ConnectTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task TestConnectEventsAsync()
6262
client.OnConnAckReceived += OnConnAckReceivedHandler;
6363

6464
// Set up TaskCompletionSource to wait for event handlers to finish
65-
var taskCompletionSource = new TaskCompletionSource<bool>();
65+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
6666
client.OnDisconnectSent += (sender, args) => taskCompletionSource.SetResult(true);
6767

6868
// Connect and Disconnect
@@ -105,7 +105,7 @@ public async Task Test_AfterDisconnectEvent_Async()
105105
client.AfterDisconnect += AfterDisconnectHandler;
106106

107107
// Set up TaskCompletionSource to wait for event handlers to finish
108-
var taskCompletionSource = new TaskCompletionSource<bool>();
108+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
109109
client.OnDisconnectSent += (sender, args) => taskCompletionSource.SetResult(true);
110110

111111
// Connect and Disconnect

Tests/HiveMQtt.Test/HiveMQClient/LWTTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task Last_Will_With_Properties_Async()
2020
Assert.True(listenerClient.IsConnected());
2121

2222
var messagesReceived = 0;
23-
var taskLWTReceived = new TaskCompletionSource<bool>();
23+
var taskLWTReceived = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
2424

2525
// Set the event handler for the message received event
2626
listenerClient.OnMessageReceived += (sender, args) =>

Tests/HiveMQtt.Test/HiveMQClient/LastWillAndTestamentBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task Last_Will_With_Properties_Async()
4040
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
4141
Assert.True(listenerClient.IsConnected());
4242

43-
var taskLWTReceived = new TaskCompletionSource<bool>();
43+
var taskLWTReceived = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
4444
var correlationData = new byte[] { 1, 2, 3, 4, 5 };
4545

4646
// Set the event handler for the message received event

Tests/HiveMQtt.Test/HiveMQClient/Plan/PacketRestrictionsTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public async Task Unsubscribe_From_NonExistent_Topic_Should_Return_ReasonCode_17
4646
result.Should().NotBeNull();
4747
// result.ReasonCodes.Should().Contain(ReasonCode.NoSubscriptionExisted);
4848
// result.Subscriptions.Count.Should().Be(1);
49-
5049
var disconnectResult = await client.DisconnectAsync().ConfigureAwait(false);
5150
disconnectResult.Should().BeTrue();
5251
}

Tests/HiveMQtt.Test/HiveMQClient/PubSubTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public async Task MostBasicPubSubAsync()
1515
var testPayload = new string(/*lang=json,strict*/ "{\"interference\": \"1029384\"}");
1616
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPubSubAsync").Build();
1717
var client = new HiveMQClient(options);
18-
var taskCompletionSource = new TaskCompletionSource<bool>();
18+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
1919

2020
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
2121
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
@@ -59,7 +59,7 @@ public async Task QoS1PubSubAsync()
5959
var testPayload = new string(/*lang=json,strict*/ "{\"interference\": \"1029384\"}");
6060
var options = new HiveMQClientOptionsBuilder().WithClientId("MostBasicPubSubAsync").Build();
6161
var client = new HiveMQClient(options);
62-
var taskCompletionSource = new TaskCompletionSource<bool>();
62+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
6363
var messagesReceived = 0;
6464

6565
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
@@ -109,7 +109,7 @@ public async Task QoS2PubSubAsync()
109109

110110
var options = new HiveMQClientOptionsBuilder().WithClientId("QoS2PubSubAsync").Build();
111111
var client = new HiveMQClient(options);
112-
var taskCompletionSource = new TaskCompletionSource<bool>();
112+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
113113
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
114114
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
115115
var messagesReceived = 0;
@@ -159,7 +159,7 @@ public async Task LargeMessagePubSubAsync()
159159
var testPayload = "1. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters. 2. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters. 3. A delusion starts like any other idea, as an egg. Identical on the outside, perfectly formed. From the shell, you'd never know anything was wrong. It's what's inside that matters.";
160160
var options = new HiveMQClientOptionsBuilder().WithClientId("LargeMessagePubSubAsync").Build();
161161
var client = new HiveMQClient(options);
162-
var taskCompletionSource = new TaskCompletionSource<bool>();
162+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
163163

164164
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
165165
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);
@@ -212,7 +212,7 @@ public async Task OneMBMessagePubSubAsync()
212212

213213
var options = new HiveMQClientOptionsBuilder().WithClientId("OneMBMessagePubSubAsync").Build();
214214
var client = new HiveMQClient(options);
215-
var taskCompletionSource = new TaskCompletionSource<bool>();
215+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
216216

217217
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
218218
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

Tests/HiveMQtt.Test/HiveMQClient/PublishBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public async Task PublishPayloadFormatIndicatorAsync()
217217
.WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator.UTF8Encoded)
218218
.Build();
219219

220-
var taskCompletionSource = new TaskCompletionSource<bool>();
220+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
221221
client.OnPublishSent += (sender, args) =>
222222
{
223223
Assert.Equal(MQTT5PayloadFormatIndicator.UTF8Encoded, args.PublishPacket.Message.PayloadFormatIndicator);

Tests/HiveMQtt.Test/HiveMQClient/PublishTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public async Task PublishPayloadFormatIndicatorAsync()
188188
Payload = Encoding.ASCII.GetBytes("blah"),
189189
};
190190

191-
var taskCompletionSource = new TaskCompletionSource<bool>();
191+
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
192192
client.OnPublishSent += (sender, args) =>
193193
{
194194
Assert.Equal(MQTT5PayloadFormatIndicator.UTF8Encoded, args.PublishPacket.Message.PayloadFormatIndicator);

0 commit comments

Comments
 (0)