Skip to content

Commit

Permalink
Make the ActivityCTS test more reliable (#2688)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Dec 19, 2024
1 parent d4f1a5c commit 2d5aace
Showing 1 changed file with 7 additions and 14 deletions.
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

0 comments on commit 2d5aace

Please sign in to comment.