Skip to content

Commit

Permalink
Tests: Better TaskCompletion Strategy (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored May 17, 2024
1 parent d043390 commit 35f08e4
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Tests/HiveMQtt.Test/HiveMQClient/SubscribeBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ public async Task PerSubHandlerWithSingleLevelWildcardAsync()
var connectResult = await subscribeClient.ConnectAsync().ConfigureAwait(false);
Assert.True(connectResult.ReasonCode == ConnAckReasonCode.Success);

var tcs = new TaskCompletionSource<bool>();
// This is the fix for:
// per-subscription OnMessageReceivedEventLauncher exception: One or more errors occurred. (An attempt was
// made to transition a task to a final state when it had already completed.)
var tcs1 = new TaskCompletionSource<bool>();
var tcs2 = new TaskCompletionSource<bool>();
var tcs3 = new TaskCompletionSource<bool>();

var messageCount = 0;

var subscribeOptions = new SubscribeOptionsBuilder()
Expand All @@ -179,7 +185,18 @@ public async Task PerSubHandlerWithSingleLevelWildcardAsync()

if (messageCount == 3)
{
tcs.SetResult(true);
if (args.PublishMessage.Topic == "tests/PerSubHandlerWithSingleLevelWildcard/0/msg")
{
tcs1.SetResult(true);
}
else if (args.PublishMessage.Topic == "tests/PerSubHandlerWithSingleLevelWildcard/1/msg")
{
tcs2.SetResult(true);
}
else if (args.PublishMessage.Topic == "tests/PerSubHandlerWithSingleLevelWildcard/2/msg")
{
tcs3.SetResult(true);
}
}
})
.Build();
Expand All @@ -200,11 +217,13 @@ public async Task PerSubHandlerWithSingleLevelWildcardAsync()
}

// Wait for the 3 messages to be received by the per-subscription handler
var handlerResult = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false);
Assert.True(handlerResult);
await Task.WhenAll(new Task[] { tcs1.Task, tcs2.Task, tcs3.Task }).ConfigureAwait(false);

var disconnectResult = await subscribeClient.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);

disconnectResult = await pubClient.DisconnectAsync().ConfigureAwait(false);
Assert.True(disconnectResult);
}

[Fact]
Expand Down

0 comments on commit 35f08e4

Please sign in to comment.