From cd015e7627704932266b98ff17ec78d883c63a67 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Tue, 23 Jul 2024 10:11:43 +0300 Subject: [PATCH] Fix CA1062 warnings (#2241) Fix CA1062 warnings for `CacheTResultSyntax`. --- src/Polly/Caching/CacheTResultSyntax.cs | 122 ++++++++++++++++-- test/Polly.Specs/Caching/CacheTResultSpecs.cs | 63 ++++++++- 2 files changed, 168 insertions(+), 17 deletions(-) diff --git a/src/Polly/Caching/CacheTResultSyntax.cs b/src/Polly/Caching/CacheTResultSyntax.cs index b3c571e5fa2..bd2d9009380 100644 --- a/src/Polly/Caching/CacheTResultSyntax.cs +++ b/src/Polly/Caching/CacheTResultSyntax.cs @@ -1,7 +1,6 @@ #nullable enable namespace Polly; -#pragma warning disable CA1062 // Validate arguments of public methods public partial class Policy { /// @@ -70,6 +69,11 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid throw new ArgumentNullException(nameof(cacheProvider)); } + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -88,13 +92,22 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// Thrown when is . /// Thrown when is . /// Thrown when is . - public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) + public static CachePolicy Cache( + ISyncCacheProvider cacheProvider, + ITtlStrategy ttlStrategy, + ICacheKeyStrategy cacheKeyStrategy, + Action? onCacheError = null) { if (cacheProvider == null) { throw new ArgumentNullException(nameof(cacheProvider)); } + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError); } @@ -256,6 +269,11 @@ public static CachePolicy Cache( throw new ArgumentNullException(nameof(cacheProvider)); } + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + return Cache(cacheProvider.For(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -296,6 +314,11 @@ public static CachePolicy Cache( throw new ArgumentNullException(nameof(cacheProvider)); } + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + return Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } @@ -439,8 +462,19 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// The policy instance. /// Thrown when is . /// Thrown when is . - public static CachePolicy Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) => - Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); + public static CachePolicy Cache( + ISyncCacheProvider cacheProvider, + TimeSpan ttl, + ICacheKeyStrategy cacheKeyStrategy, + Action? onCacheError = null) + { + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + + return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError); + } /// /// Builds a that will function like a result cache for delegate executions returning a . @@ -457,12 +491,28 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// Thrown when is . /// Thrown when is . /// Thrown when is . - public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) + public static CachePolicy Cache( + ISyncCacheProvider cacheProvider, + ITtlStrategy ttlStrategy, + ICacheKeyStrategy cacheKeyStrategy, + Action? onCacheError = null) { + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + Action emptyDelegate = (_, _) => { }; - return Cache(cacheProvider, ttlStrategy.For(), cacheKeyStrategy.GetCacheKey, - emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); + return Cache( + cacheProvider, + ttlStrategy.For(), + cacheKeyStrategy.GetCacheKey, + emptyDelegate, + emptyDelegate, + emptyDelegate, + onCacheError, + onCacheError); } /// @@ -670,9 +720,23 @@ public static CachePolicy Cache( Action onCacheMiss, Action onCachePut, Action? onCacheGetError, - Action? onCachePutError) => - Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, - onCachePut, onCacheGetError, onCachePutError); + Action? onCachePutError) + { + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + + return Cache( + cacheProvider, + new RelativeTtl(ttl), + cacheKeyStrategy.GetCacheKey, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + } /// /// Builds a that will function like a result cache for delegate executions returning a . @@ -704,8 +768,23 @@ public static CachePolicy Cache( Action onCacheMiss, Action onCachePut, Action? onCacheGetError, - Action? onCachePutError) => - Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); + Action? onCachePutError) + { + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + + return Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy.GetCacheKey, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + } /// /// Builds a that will function like a result cache for delegate executions returning a . @@ -737,8 +816,23 @@ public static CachePolicy Cache( Action onCacheMiss, Action onCachePut, Action? onCacheGetError, - Action? onCachePutError) => - Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); + Action? onCachePutError) + { + if (cacheKeyStrategy is null) + { + throw new ArgumentNullException(nameof(cacheKeyStrategy)); + } + + return Cache( + cacheProvider, + ttlStrategy, + cacheKeyStrategy.GetCacheKey, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + } /// /// Builds a that will function like a result cache for delegate executions returning a . diff --git a/test/Polly.Specs/Caching/CacheTResultSpecs.cs b/test/Polly.Specs/Caching/CacheTResultSpecs.cs index 15e7f278ce0..360e330c1e6 100644 --- a/test/Polly.Specs/Caching/CacheTResultSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultSpecs.cs @@ -51,8 +51,13 @@ public void Should_throw_when_action_is_null() public void Should_throw_when_cache_provider_is_null() { ISyncCacheProvider cacheProvider = null!; + ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]); + Action action = () => Policy.Cache(cacheProvider, TimeSpan.MaxValue); action.Should().Throw().And.ParamName.Should().Be("cacheProvider"); + + action = () => Policy.Cache(cacheProvider, new ContextualTtl(), cacheKeyStrategy); + action.Should().Throw().And.ParamName.Should().Be("cacheProvider"); } [Fact] @@ -68,9 +73,61 @@ public void Should_throw_when_ttl_strategy_is_null() public void Should_throw_when_cache_key_strategy_is_null() { ISyncCacheProvider cacheProvider = new StubCacheProvider(); - Func cacheKeyStrategy = null!; - Action action = () => Policy.Cache(cacheProvider, TimeSpan.MaxValue, cacheKeyStrategy); - action.Should().Throw().And.ParamName.Should().Be("cacheKeyStrategy"); + var ttl = TimeSpan.MaxValue; + ITtlStrategy ttlStrategy = new ContextualTtl(); + ICacheKeyStrategy cacheKeyStrategy = null!; + Func cacheKeyStrategyFunc = null!; + Action onCacheGet = (_, _) => { }; + Action onCacheMiss = (_, _) => { }; + Action onCachePut = (_, _) => { }; + Action? onCacheGetError = null; + Action? onCachePutError = null; + const string CacheKeyStrategyExpected = "cacheKeyStrategy"; + + Action action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProvider.For(), ttl, cacheKeyStrategy, onCacheGetError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache(cacheProvider.For(), ttlStrategy, cacheKeyStrategy, onCacheGetError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProvider.For(), + ttl, + cacheKeyStrategy, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProvider.For(), + ttlStrategy, + cacheKeyStrategy, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); + + action = () => Policy.Cache( + cacheProvider.For(), + ttlStrategy.For(), + cacheKeyStrategy, + onCacheGet, + onCacheMiss, + onCachePut, + onCacheGetError, + onCachePutError); + action.Should().Throw().And.ParamName.Should().Be(CacheKeyStrategyExpected); } #endregion