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

Make the ActivityCTS test more reliable #2688

Merged
merged 1 commit into from
Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -13,24 +14,16 @@ public class ActivityCancellationTokenSourceTests
[Fact]
public void ActivityCancellationTokenSource_PoolsSources()
{
// This test can run in parallel with others making use of ActivityCancellationTokenSource
// A different thread could have already added/removed a source from the queue
HashSet<ActivityCancellationTokenSource> sources = [];

for (var i = 0; i < 1000; i++)
for (var i = 0; i < 1_000; i++)
{
var cts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None);
cts.Return();

var cts2 = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None);
cts2.Return();

if (ReferenceEquals(cts, cts2))
{
return;
}
var source = ActivityCancellationTokenSource.Rent(TimeSpan.FromMinutes(10), CancellationToken.None);
source.Return();
sources.Add(source);
}

Assert.Fail("CancellationTokenSources were not pooled");
Assert.True(sources.Count < 1000);
}

[Fact]
Expand Down
Loading