Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Better TaskCompletion Strategy #158

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading