Skip to content

Commit

Permalink
Fix CA2000/redundant suppressions (#1947)
Browse files Browse the repository at this point in the history
- Fix CA2000.
- Remove redundant suppressions.
  • Loading branch information
gintsk authored Feb 4, 2024
1 parent 41322d9 commit e4cebcd
Show file tree
Hide file tree
Showing 32 changed files with 1,515 additions and 1,179 deletions.
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())
{
// 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

0 comments on commit e4cebcd

Please sign in to comment.