Skip to content

Commit cd015e7

Browse files
authored
Fix CA1062 warnings (#2241)
Fix CA1062 warnings for `CacheTResultSyntax`.
1 parent 85cd19d commit cd015e7

File tree

2 files changed

+168
-17
lines changed

2 files changed

+168
-17
lines changed

src/Polly/Caching/CacheTResultSyntax.cs

+108-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#nullable enable
22
namespace Polly;
33

4-
#pragma warning disable CA1062 // Validate arguments of public methods
54
public partial class Policy
65
{
76
/// <summary>
@@ -70,6 +69,11 @@ public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvid
7069
throw new ArgumentNullException(nameof(cacheProvider));
7170
}
7271

72+
if (cacheKeyStrategy is null)
73+
{
74+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
75+
}
76+
7377
return Cache<TResult>(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
7478
}
7579

@@ -88,13 +92,22 @@ public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvid
8892
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
8993
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
9094
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
91-
public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
95+
public static CachePolicy<TResult> Cache<TResult>(
96+
ISyncCacheProvider cacheProvider,
97+
ITtlStrategy ttlStrategy,
98+
ICacheKeyStrategy cacheKeyStrategy,
99+
Action<Context, string, Exception>? onCacheError = null)
92100
{
93101
if (cacheProvider == null)
94102
{
95103
throw new ArgumentNullException(nameof(cacheProvider));
96104
}
97105

106+
if (cacheKeyStrategy is null)
107+
{
108+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
109+
}
110+
98111
return Cache<TResult>(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError);
99112
}
100113

@@ -256,6 +269,11 @@ public static CachePolicy<TResult> Cache<TResult>(
256269
throw new ArgumentNullException(nameof(cacheProvider));
257270
}
258271

272+
if (cacheKeyStrategy is null)
273+
{
274+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
275+
}
276+
259277
return Cache<TResult>(cacheProvider.For<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
260278
}
261279

@@ -296,6 +314,11 @@ public static CachePolicy<TResult> Cache<TResult>(
296314
throw new ArgumentNullException(nameof(cacheProvider));
297315
}
298316

317+
if (cacheKeyStrategy is null)
318+
{
319+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
320+
}
321+
299322
return Cache<TResult>(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
300323
}
301324

@@ -439,8 +462,19 @@ public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> ca
439462
/// <returns>The policy instance.</returns>
440463
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
441464
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
442-
public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
443-
Cache<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
465+
public static CachePolicy<TResult> Cache<TResult>(
466+
ISyncCacheProvider<TResult> cacheProvider,
467+
TimeSpan ttl,
468+
ICacheKeyStrategy cacheKeyStrategy,
469+
Action<Context, string, Exception>? onCacheError = null)
470+
{
471+
if (cacheKeyStrategy is null)
472+
{
473+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
474+
}
475+
476+
return Cache<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
477+
}
444478

445479
/// <summary>
446480
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -457,12 +491,28 @@ public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> ca
457491
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
458492
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
459493
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
460-
public static CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
494+
public static CachePolicy<TResult> Cache<TResult>(
495+
ISyncCacheProvider<TResult> cacheProvider,
496+
ITtlStrategy ttlStrategy,
497+
ICacheKeyStrategy cacheKeyStrategy,
498+
Action<Context, string, Exception>? onCacheError = null)
461499
{
500+
if (cacheKeyStrategy is null)
501+
{
502+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
503+
}
504+
462505
Action<Context, string> emptyDelegate = (_, _) => { };
463506

464-
return Cache<TResult>(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey,
465-
emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
507+
return Cache<TResult>(
508+
cacheProvider,
509+
ttlStrategy.For<TResult>(),
510+
cacheKeyStrategy.GetCacheKey,
511+
emptyDelegate,
512+
emptyDelegate,
513+
emptyDelegate,
514+
onCacheError,
515+
onCacheError);
466516
}
467517

468518
/// <summary>
@@ -670,9 +720,23 @@ public static CachePolicy<TResult> Cache<TResult>(
670720
Action<Context, string> onCacheMiss,
671721
Action<Context, string> onCachePut,
672722
Action<Context, string, Exception>? onCacheGetError,
673-
Action<Context, string, Exception>? onCachePutError) =>
674-
Cache<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss,
675-
onCachePut, onCacheGetError, onCachePutError);
723+
Action<Context, string, Exception>? onCachePutError)
724+
{
725+
if (cacheKeyStrategy is null)
726+
{
727+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
728+
}
729+
730+
return Cache<TResult>(
731+
cacheProvider,
732+
new RelativeTtl(ttl),
733+
cacheKeyStrategy.GetCacheKey,
734+
onCacheGet,
735+
onCacheMiss,
736+
onCachePut,
737+
onCacheGetError,
738+
onCachePutError);
739+
}
676740

677741
/// <summary>
678742
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -704,8 +768,23 @@ public static CachePolicy<TResult> Cache<TResult>(
704768
Action<Context, string> onCacheMiss,
705769
Action<Context, string> onCachePut,
706770
Action<Context, string, Exception>? onCacheGetError,
707-
Action<Context, string, Exception>? onCachePutError) =>
708-
Cache<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
771+
Action<Context, string, Exception>? onCachePutError)
772+
{
773+
if (cacheKeyStrategy is null)
774+
{
775+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
776+
}
777+
778+
return Cache<TResult>(
779+
cacheProvider,
780+
ttlStrategy,
781+
cacheKeyStrategy.GetCacheKey,
782+
onCacheGet,
783+
onCacheMiss,
784+
onCachePut,
785+
onCacheGetError,
786+
onCachePutError);
787+
}
709788

710789
/// <summary>
711790
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -737,8 +816,23 @@ public static CachePolicy<TResult> Cache<TResult>(
737816
Action<Context, string> onCacheMiss,
738817
Action<Context, string> onCachePut,
739818
Action<Context, string, Exception>? onCacheGetError,
740-
Action<Context, string, Exception>? onCachePutError) =>
741-
Cache<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
819+
Action<Context, string, Exception>? onCachePutError)
820+
{
821+
if (cacheKeyStrategy is null)
822+
{
823+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
824+
}
825+
826+
return Cache<TResult>(
827+
cacheProvider,
828+
ttlStrategy,
829+
cacheKeyStrategy.GetCacheKey,
830+
onCacheGet,
831+
onCacheMiss,
832+
onCachePut,
833+
onCacheGetError,
834+
onCachePutError);
835+
}
742836

743837
/// <summary>
744838
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>

test/Polly.Specs/Caching/CacheTResultSpecs.cs

+60-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ public void Should_throw_when_action_is_null()
5151
public void Should_throw_when_cache_provider_is_null()
5252
{
5353
ISyncCacheProvider cacheProvider = null!;
54+
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
55+
5456
Action action = () => Policy.Cache<ResultPrimitive>(cacheProvider, TimeSpan.MaxValue);
5557
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");
58+
59+
action = () => Policy.Cache<ResultPrimitive>(cacheProvider, new ContextualTtl(), cacheKeyStrategy);
60+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");
5661
}
5762

5863
[Fact]
@@ -68,9 +73,61 @@ public void Should_throw_when_ttl_strategy_is_null()
6873
public void Should_throw_when_cache_key_strategy_is_null()
6974
{
7075
ISyncCacheProvider cacheProvider = new StubCacheProvider();
71-
Func<Context, string> cacheKeyStrategy = null!;
72-
Action action = () => Policy.Cache<ResultPrimitive>(cacheProvider, TimeSpan.MaxValue, cacheKeyStrategy);
73-
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheKeyStrategy");
76+
var ttl = TimeSpan.MaxValue;
77+
ITtlStrategy ttlStrategy = new ContextualTtl();
78+
ICacheKeyStrategy cacheKeyStrategy = null!;
79+
Func<Context, string> cacheKeyStrategyFunc = null!;
80+
Action<Context, string> onCacheGet = (_, _) => { };
81+
Action<Context, string> onCacheMiss = (_, _) => { };
82+
Action<Context, string> onCachePut = (_, _) => { };
83+
Action<Context, string, Exception>? onCacheGetError = null;
84+
Action<Context, string, Exception>? onCachePutError = null;
85+
const string CacheKeyStrategyExpected = "cacheKeyStrategy";
86+
87+
Action action = () => Policy.Cache<ResultPrimitive>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError);
88+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
89+
90+
action = () => Policy.Cache<ResultPrimitive>(cacheProvider, ttl, cacheKeyStrategyFunc);
91+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
92+
93+
action = () => Policy.Cache<ResultPrimitive>(cacheProvider.For<ResultPrimitive>(), ttl, cacheKeyStrategy, onCacheGetError);
94+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
95+
96+
action = () => Policy.Cache<ResultPrimitive>(cacheProvider.For<ResultPrimitive>(), ttlStrategy, cacheKeyStrategy, onCacheGetError);
97+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
98+
99+
action = () => Policy.Cache<ResultPrimitive>(
100+
cacheProvider.For<ResultPrimitive>(),
101+
ttl,
102+
cacheKeyStrategy,
103+
onCacheGet,
104+
onCacheMiss,
105+
onCachePut,
106+
onCacheGetError,
107+
onCachePutError);
108+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
109+
110+
action = () => Policy.Cache<ResultPrimitive>(
111+
cacheProvider.For<ResultPrimitive>(),
112+
ttlStrategy,
113+
cacheKeyStrategy,
114+
onCacheGet,
115+
onCacheMiss,
116+
onCachePut,
117+
onCacheGetError,
118+
onCachePutError);
119+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
120+
121+
action = () => Policy.Cache<ResultPrimitive>(
122+
cacheProvider.For<ResultPrimitive>(),
123+
ttlStrategy.For<ResultPrimitive>(),
124+
cacheKeyStrategy,
125+
onCacheGet,
126+
onCacheMiss,
127+
onCachePut,
128+
onCacheGetError,
129+
onCachePutError);
130+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
74131
}
75132

76133
#endregion

0 commit comments

Comments
 (0)