From 19839210172f743c463bdf84f297ec65e10202d7 Mon Sep 17 00:00:00 2001 From: martincostello Date: Wed, 27 Sep 2023 21:41:06 +0100 Subject: [PATCH 1/4] Add banned API analyzers Add to detect usage of problematic code patterns. --- Directory.Packages.props | 1 + eng/Analyzers.targets | 2 ++ eng/analyzers/BannedSymbols.txt | 5 +++++ 3 files changed, 8 insertions(+) create mode 100644 eng/analyzers/BannedSymbols.txt diff --git a/Directory.Packages.props b/Directory.Packages.props index dceab74524d..73fa08c1b9a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,6 +10,7 @@ + diff --git a/eng/Analyzers.targets b/eng/Analyzers.targets index 798450220ca..ebd3872744c 100644 --- a/eng/Analyzers.targets +++ b/eng/Analyzers.targets @@ -1,7 +1,9 @@ + + diff --git a/eng/analyzers/BannedSymbols.txt b/eng/analyzers/BannedSymbols.txt new file mode 100644 index 00000000000..1c8d9041801 --- /dev/null +++ b/eng/analyzers/BannedSymbols.txt @@ -0,0 +1,5 @@ +P:System.DateTime.Now; Use TimeProvider.GetLocalNow().DateTime instead. +P:System.DateTime.Today; Use TimeProvider.GetLocalNow().DateTime.Date instead. +P:System.DateTimeOffset.Now; Use TimeProvider.GetLocalNow() instead. +P:System.DateTimeOffset.Today; Use TimeProvider.GetLocalNow().Date instead. +M:System.DateTimeOffset.op_Implicit(System.DateTime); Do not implicitly cast DateTime to DateTimeOffset. From fd92c41cb9cb6d4b1130b695b6be2442e95a4819 Mon Sep 17 00:00:00 2001 From: martincostello Date: Wed, 27 Sep 2023 21:41:31 +0100 Subject: [PATCH 2/4] Fix StyleCop warning Fix StyleCop warning by updating constructor documentation. --- src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index 168a34eac52..5333dd90b1e 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -19,7 +19,7 @@ internal sealed class LockFreeTokenBucketRateLimiter : IRateLimiter #endif /// - /// Creates an instance of + /// Initializes a new instance of the class. /// /// How often one execution is permitted. /// The capacity of the token bucket. From 023a8648dce791ebe6eb2557aa940fbc1d7081ca Mon Sep 17 00:00:00 2001 From: David Pine Date: Wed, 27 Sep 2023 14:18:19 -0500 Subject: [PATCH 3/4] Fix issue I was experiencing in my time zone (#1650) --- .../CircuitBreaker/Controller/CircuitStateController.cs | 2 +- .../CircuitBreaker/Controller/CircuitStateControllerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Polly.Core/CircuitBreaker/Controller/CircuitStateController.cs b/src/Polly.Core/CircuitBreaker/Controller/CircuitStateController.cs index fcd35117e06..37c0c70db7f 100644 --- a/src/Polly.Core/CircuitBreaker/Controller/CircuitStateController.cs +++ b/src/Polly.Core/CircuitBreaker/Controller/CircuitStateController.cs @@ -241,7 +241,7 @@ internal static async ValueTask ExecuteScheduledTaskAsync(Task? task, Resilience private static bool IsDateTimeOverflow(DateTimeOffset utcNow, TimeSpan breakDuration) { - TimeSpan maxDifference = DateTime.MaxValue - utcNow; + TimeSpan maxDifference = DateTimeOffset.MaxValue - utcNow; // stryker disable once equality : no means to test this return breakDuration > maxDifference; diff --git a/test/Polly.Core.Tests/CircuitBreaker/Controller/CircuitStateControllerTests.cs b/test/Polly.Core.Tests/CircuitBreaker/Controller/CircuitStateControllerTests.cs index 75bcfc367d5..4c0784098a5 100644 --- a/test/Polly.Core.Tests/CircuitBreaker/Controller/CircuitStateControllerTests.cs +++ b/test/Polly.Core.Tests/CircuitBreaker/Controller/CircuitStateControllerTests.cs @@ -312,7 +312,7 @@ public async Task OnActionFailureAsync_EnsureBreakDurationNotOverflow(bool overf using var controller = CreateController(); var shouldBreak = true; await TransitionToState(controller, CircuitState.HalfOpen); - var utcNow = DateTime.MaxValue - _options.BreakDuration; + var utcNow = DateTimeOffset.MaxValue - _options.BreakDuration; if (overflow) { utcNow += TimeSpan.FromMilliseconds(10); From fd2f2ef8c30996a4b34c2e8cc36e222860c3649d Mon Sep 17 00:00:00 2001 From: martincostello Date: Wed, 27 Sep 2023 21:49:11 +0100 Subject: [PATCH 4/4] Update CHANGELOG Add this PR to the changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14de858fde2..d96d758172f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ * Update docs source by [@martincostello](https://github.com/martincostello) in https://github.com/App-vNext/Polly/pull/1641 * Stabilize `AddHedging_IntegrationTest` test by [@martintmk](https://github.com/martintmk) in https://github.com/App-vNext/Polly/pull/1644 * Release v8 docs by [@martintmk](https://github.com/martintmk) in https://github.com/App-vNext/Polly/pull/1599 +* Add banned API analyzers and fix time zone issue by [@martincostello](https://github.com/martincostello) and [@IEvangelist](https://github.com/IEvangelist) in https://github.com/App-vNext/Polly/pull/1651 ## 8.0.0-beta.2