From 99e52d4aa67a7dfbe3cd4af013b5f9be9618f5ac Mon Sep 17 00:00:00 2001 From: ZLoo Date: Fri, 12 Jul 2024 18:45:45 +0300 Subject: [PATCH 1/3] fix CA 1032 --- src/Polly/Polly.csproj | 2 +- .../RateLimit/RateLimitRejectedException.cs | 26 +++++++++++++++++++ src/Polly/Utilities/TimedLock.cs | 22 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index 8e4e61f41c4..931d182967a 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -7,7 +7,7 @@ Library 70 true - $(NoWarn);CA1010;CA1031;CA1032;CA1033;CA1051;CA1062;CA1063;CA1064;CA1710;CA1716;CA1724;CA1805;CA1815;CA1816;CA2211 + $(NoWarn);CA1010;CA1031;CA1033;CA1051;CA1062;CA1063;CA1064;CA1710;CA1716;CA1724;CA1805;CA1815;CA1816;CA2211 $(NoWarn);S2223;S3215;S3246;S3971;S4039;S4049;S4457 $(NoWarn);RS0037; diff --git a/src/Polly/RateLimit/RateLimitRejectedException.cs b/src/Polly/RateLimit/RateLimitRejectedException.cs index 3a7c9a2f5e1..56410c0c74e 100644 --- a/src/Polly/RateLimit/RateLimitRejectedException.cs +++ b/src/Polly/RateLimit/RateLimitRejectedException.cs @@ -21,6 +21,32 @@ public class RateLimitRejectedException : ExecutionRejectedException /// public TimeSpan RetryAfter { get; private set; } + /// + /// Initializes a new instance of the class. + /// + public RateLimitRejectedException() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + public RateLimitRejectedException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + /// The inner exception. + public RateLimitRejectedException(string message, Exception inner) + : base(message, inner) + { + } + /// /// Initializes a new instance of the class. /// diff --git a/src/Polly/Utilities/TimedLock.cs b/src/Polly/Utilities/TimedLock.cs index 901135c8af7..c215ef016f2 100644 --- a/src/Polly/Utilities/TimedLock.cs +++ b/src/Polly/Utilities/TimedLock.cs @@ -85,8 +85,30 @@ private class Sentinel internal class LockTimeoutException : Exception { + /// + /// Initializes a new instance of the class. + /// public LockTimeoutException() : base("Timeout waiting for lock") { } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + public LockTimeoutException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + /// The inner exception. + public LockTimeoutException(string message, Exception innerException) + : base(message, innerException) + { + } } From d88423fe88a101a6aea57cf410c194a4cd380f3e Mon Sep 17 00:00:00 2001 From: ZLoo Date: Fri, 12 Jul 2024 22:47:57 +0300 Subject: [PATCH 2/3] Add test --- .../RateLimit/RateLimitRejectedException.cs | 1 + .../RateLimitRejectedExceptionTests.cs | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 test/Polly.Specs/RateLimit/RateLimitRejectedExceptionTests.cs diff --git a/src/Polly/RateLimit/RateLimitRejectedException.cs b/src/Polly/RateLimit/RateLimitRejectedException.cs index 56410c0c74e..e643d3af028 100644 --- a/src/Polly/RateLimit/RateLimitRejectedException.cs +++ b/src/Polly/RateLimit/RateLimitRejectedException.cs @@ -25,6 +25,7 @@ public class RateLimitRejectedException : ExecutionRejectedException /// Initializes a new instance of the class. /// public RateLimitRejectedException() + : base("The operation could not be executed because it was rejected by the rate limit.") { } diff --git a/test/Polly.Specs/RateLimit/RateLimitRejectedExceptionTests.cs b/test/Polly.Specs/RateLimit/RateLimitRejectedExceptionTests.cs new file mode 100644 index 00000000000..febc177c38f --- /dev/null +++ b/test/Polly.Specs/RateLimit/RateLimitRejectedExceptionTests.cs @@ -0,0 +1,35 @@ +namespace Polly.Specs.RateLimit; + +public class RateLimitRejectedExceptionTests +{ + [Fact] + public void Ctor_Ok() + { + const string Dummy = "dummy"; + var exception = new InvalidOperationException(); + var retryAfter = TimeSpan.FromSeconds(4); + + new RateLimitRejectedException().Message.Should().Be("The operation could not be executed because it was rejected by the rate limit."); + new RateLimitRejectedException(Dummy).Message.Should().Be(Dummy); + + var rate = new RateLimitRejectedException(Dummy, exception); + rate.Message.Should().Be(Dummy); + rate.InnerException.Should().Be(exception); + + new RateLimitRejectedException(retryAfter).RetryAfter.Should().Be(retryAfter); + new RateLimitRejectedException(retryAfter).Message.Should().Be($"The operation has been rate-limited and should be retried after {retryAfter}"); + + rate = new RateLimitRejectedException(retryAfter, exception); + rate.RetryAfter.Should().Be(retryAfter); + rate.InnerException.Should().Be(exception); + + rate = new RateLimitRejectedException(retryAfter, Dummy); + rate.RetryAfter.Should().Be(retryAfter); + rate.Message.Should().Be(Dummy); + + rate = new RateLimitRejectedException(retryAfter, Dummy, exception); + rate.RetryAfter.Should().Be(retryAfter); + rate.Message.Should().Be(Dummy); + rate.InnerException.Should().Be(exception); + } +} From ed3f78d667473cfd6d9b3d5c2b55942e6cd3e9e2 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Fri, 12 Jul 2024 22:51:46 +0300 Subject: [PATCH 3/3] Add test --- .../Utilities/LockTimeoutExceptionTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/Polly.Specs/Utilities/LockTimeoutExceptionTests.cs diff --git a/test/Polly.Specs/Utilities/LockTimeoutExceptionTests.cs b/test/Polly.Specs/Utilities/LockTimeoutExceptionTests.cs new file mode 100644 index 00000000000..3f140045d49 --- /dev/null +++ b/test/Polly.Specs/Utilities/LockTimeoutExceptionTests.cs @@ -0,0 +1,18 @@ +namespace Polly.Specs.Utilities; + +public class LockTimeoutExceptionTests +{ + [Fact] + public void Ctor_Ok() + { + const string Dummy = "dummy"; + var exception = new InvalidOperationException(); + + new LockTimeoutException().Message.Should().Be("Timeout waiting for lock"); + new LockTimeoutException(Dummy).Message.Should().Be(Dummy); + + var rate = new LockTimeoutException(Dummy, exception); + rate.Message.Should().Be(Dummy); + rate.InnerException.Should().Be(exception); + } +}