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

Fix CA2000/stalled suppressions #1947

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions bench/Polly.Core.Benchmarks/Utils/Helper.RateLimiting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma warning disable S4225 // Extension methods should not extend "object"

using System.Threading.RateLimiting;

namespace Polly.Core.Benchmarks.Utils;
Expand Down
2 changes: 0 additions & 2 deletions bench/Polly.Core.Benchmarks/Utils/Helper.Timeout.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma warning disable S4225 // Extension methods should not extend "object"

namespace Polly.Core.Benchmarks.Utils;

internal static partial class Helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ internal static CircuitBreakerResilienceStrategy<TResult> CreateStrategy<TResult
options.MinimumThroughput,
HealthMetrics.Create(options.SamplingDuration, context.TimeProvider));

#pragma warning disable CA2000 // Dispose objects before losing scope
var controller = new CircuitStateController<TResult>(
options.BreakDuration,
options.OnOpened,
Expand All @@ -87,7 +86,6 @@ internal static CircuitBreakerResilienceStrategy<TResult> CreateStrategy<TResult
context.TimeProvider,
context.Telemetry,
options.BreakDurationGenerator);
#pragma warning restore CA2000 // Dispose objects before losing scope

return new CircuitBreakerResilienceStrategy<TResult>(
options.ShouldHandle!,
Expand Down
2 changes: 0 additions & 2 deletions src/Polly.Core/Registry/ResiliencePipelineProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Polly.Registry;

#pragma warning disable CA1716 // Identifiers should not match keywords

/// <summary>
/// Represents a provider for resilience pipelines that are accessible by <typeparamref name="TKey"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace Polly.Core.Tests.Utils.Pipeline;

#pragma warning disable CA2000 // Dispose objects before losing scope

public class CompositePipelineComponentTests
{
private readonly ResilienceStrategyTelemetry _telemetry;
Expand Down
52 changes: 28 additions & 24 deletions test/Polly.Specs/Caching/CacheAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,24 +450,27 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached(

var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

int delegateInvocations = 0;
Func<Context, CancellationToken, Task<string>> func = async (_, _) =>

using (CancellationTokenSource tokenSource = new CancellationTokenSource())
martincostello marked this conversation as resolved.
Show resolved Hide resolved
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
await TaskHelper.EmptyTask;
return ValueToReturn;
};
Func<Context, CancellationToken, Task<string>> func = async (_, _) =>
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
await TaskHelper.EmptyTask;
return ValueToReturn;
};

(await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);
(await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);

tokenSource.Cancel();
tokenSource.Cancel();

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
}

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
delegateInvocations.Should().Be(1);
}

Expand All @@ -480,18 +483,19 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p
IAsyncCacheProvider stubCacheProvider = new StubCacheProvider();
var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

Func<Context, CancellationToken, Task<string>> func = async (_, ct) =>
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
await TaskHelper.EmptyTask;
return ValueToReturn;
};

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
Func<Context, CancellationToken, Task<string>> func = async (_, ct) =>
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
await TaskHelper.EmptyTask;
return ValueToReturn;
};

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
}

(bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false);
cacheHit.Should().BeFalse();
Expand Down
48 changes: 26 additions & 22 deletions test/Polly.Specs/Caching/CacheSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,23 +442,26 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached()

CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

int delegateInvocations = 0;
Func<Context, CancellationToken, string> func = (_, _) =>

using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
return ValueToReturn;
};
Func<Context, CancellationToken, string> func = (_, _) =>
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
return ValueToReturn;
};

cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);
cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);

tokenSource.Cancel();
tokenSource.Cancel();

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
}

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
delegateInvocations.Should().Be(1);
}

Expand All @@ -471,17 +474,18 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_
ISyncCacheProvider stubCacheProvider = new StubCacheProvider();
CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

Func<Context, CancellationToken, string> func = (_, ct) =>
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
return ValueToReturn;
};

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
Func<Context, CancellationToken, string> func = (_, ct) =>
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
return ValueToReturn;
};

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
}

(bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey);
cacheHit.Should().BeFalse();
Expand Down
52 changes: 28 additions & 24 deletions test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,24 +434,27 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached(

var cache = Policy.CacheAsync<string>(new StubCacheProvider(), TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

int delegateInvocations = 0;
Func<Context, CancellationToken, Task<string>> func = async (_, _) =>

using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
await TaskHelper.EmptyTask;
return ValueToReturn;
};
Func<Context, CancellationToken, Task<string>> func = async (_, _) =>
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
await TaskHelper.EmptyTask;
return ValueToReturn;
};

(await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);
(await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);

tokenSource.Cancel();
tokenSource.Cancel();

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
}

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
delegateInvocations.Should().Be(1);
}

Expand All @@ -464,18 +467,19 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p
IAsyncCacheProvider stubCacheProvider = new StubCacheProvider();
var cache = Policy.CacheAsync<string>(stubCacheProvider, TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

Func<Context, CancellationToken, Task<string>> func = async (_, ct) =>
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
await TaskHelper.EmptyTask;
return ValueToReturn;
};

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
Func<Context, CancellationToken, Task<string>> func = async (_, ct) =>
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
await TaskHelper.EmptyTask;
return ValueToReturn;
};

await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token))
.Should().ThrowAsync<OperationCanceledException>();
}

(bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false);
cacheHit.Should().BeFalse();
Expand Down
48 changes: 26 additions & 22 deletions test/Polly.Specs/Caching/CacheTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,26 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached()

CachePolicy<string> cache = Policy.Cache<string>(new StubCacheProvider(), TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

int delegateInvocations = 0;
Func<Context, CancellationToken, string> func = (_, _) =>

using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
return ValueToReturn;
};
Func<Context, CancellationToken, string> func = (_, _) =>
{
// delegate does not observe cancellation token; test is whether CacheEngine does.
delegateInvocations++;
return ValueToReturn;
};

cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);
cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn);
delegateInvocations.Should().Be(1);

tokenSource.Cancel();
tokenSource.Cancel();

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
}

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
delegateInvocations.Should().Be(1);
}

Expand All @@ -454,17 +457,18 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_
ISyncCacheProvider stubCacheProvider = new StubCacheProvider();
CachePolicy<string> cache = Policy.Cache<string>(stubCacheProvider, TimeSpan.MaxValue);

CancellationTokenSource tokenSource = new CancellationTokenSource();

Func<Context, CancellationToken, string> func = (_, ct) =>
using (CancellationTokenSource tokenSource = new CancellationTokenSource())
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
return ValueToReturn;
};

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
Func<Context, CancellationToken, string> func = (_, ct) =>
{
tokenSource.Cancel(); // simulate cancellation raised during delegate execution
ct.ThrowIfCancellationRequested();
return ValueToReturn;
};

cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token))
.Should().Throw<OperationCanceledException>();
}

(bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey);
cacheHit.Should().BeFalse();
Expand Down
Loading