Skip to content

Commit

Permalink
Don't assert the sources are pooled before 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Oct 19, 2021
1 parent cf02b85 commit a588b68
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,40 @@ namespace Yarp.ReverseProxy.Utilities.Tests
{
public class ActivityCancellationTokenSourceTests
{
#if NET6_0_OR_GREATER // CancellationTokenSource.TryReset() is only available in 6.0+
[Fact]
public void ActivityCancellationTokenSource_PoolsSources()
{
var cts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None);
// 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

cts.Return();
for (var i = 0; i < 1000; 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;
}
}

Assert.True(false, "CancellationTokenSources were not pooled");
}
#endif

[Fact]
public void ActivityCancellationTokenSource_DoesNotPoolsCanceledSources()
{
var cts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None);
cts.Cancel();

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

Assert.Same(cts, cts2);
Assert.NotSame(cts, cts2);
}

[Fact]
Expand Down Expand Up @@ -61,7 +85,7 @@ public async Task ActivityCancellationTokenSource_RespectsTimeout()
await Task.Delay(1);
}

throw new TimeoutException("Cts was not canceled");
Assert.True(false, "Cts was not canceled");
}
}
}

0 comments on commit a588b68

Please sign in to comment.