Skip to content

Commit 7868132

Browse files
authored
Fix CA1062 warnings (#2243)
Fix CA1062 warnings for `AsyncCacheTResultSyntax`.
1 parent cd015e7 commit 7868132

File tree

2 files changed

+211
-19
lines changed

2 files changed

+211
-19
lines changed

src/Polly/Caching/AsyncCacheTResultSyntax.cs

+132-16
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>
@@ -63,13 +62,22 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider
6362
/// <returns>The policy instance.</returns>
6463
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
6564
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
66-
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
65+
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
66+
IAsyncCacheProvider cacheProvider,
67+
TimeSpan ttl,
68+
ICacheKeyStrategy cacheKeyStrategy,
69+
Action<Context, string, Exception>? onCacheError = null)
6770
{
6871
if (cacheProvider == null)
6972
{
7073
throw new ArgumentNullException(nameof(cacheProvider));
7174
}
7275

76+
if (cacheKeyStrategy is null)
77+
{
78+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
79+
}
80+
7381
return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
7482
}
7583

@@ -88,13 +96,22 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider
8896
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
8997
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
9098
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
91-
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
99+
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
100+
IAsyncCacheProvider cacheProvider,
101+
ITtlStrategy ttlStrategy,
102+
ICacheKeyStrategy cacheKeyStrategy,
103+
Action<Context, string, Exception>? onCacheError = null)
92104
{
93105
if (cacheProvider == null)
94106
{
95107
throw new ArgumentNullException(nameof(cacheProvider));
96108
}
97109

110+
if (cacheKeyStrategy is null)
111+
{
112+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
113+
}
114+
98115
return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError);
99116
}
100117

@@ -256,6 +273,11 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
256273
throw new ArgumentNullException(nameof(cacheProvider));
257274
}
258275

276+
if (cacheKeyStrategy is null)
277+
{
278+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
279+
}
280+
259281
return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
260282
}
261283

@@ -296,6 +318,11 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
296318
throw new ArgumentNullException(nameof(cacheProvider));
297319
}
298320

321+
if (cacheKeyStrategy is null)
322+
{
323+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
324+
}
325+
299326
return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
300327
}
301328

@@ -439,8 +466,19 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<
439466
/// <returns>The policy instance.</returns>
440467
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
441468
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
442-
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
443-
CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
469+
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
470+
IAsyncCacheProvider<TResult> cacheProvider,
471+
TimeSpan ttl,
472+
ICacheKeyStrategy cacheKeyStrategy,
473+
Action<Context, string, Exception>? onCacheError = null)
474+
{
475+
if (cacheKeyStrategy is null)
476+
{
477+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
478+
}
479+
480+
return CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
481+
}
444482

445483
/// <summary>
446484
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -457,11 +495,28 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<
457495
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
458496
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
459497
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
460-
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
498+
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
499+
IAsyncCacheProvider<TResult> cacheProvider,
500+
ITtlStrategy ttlStrategy,
501+
ICacheKeyStrategy cacheKeyStrategy,
502+
Action<Context, string, Exception>? onCacheError = null)
461503
{
504+
if (cacheKeyStrategy is null)
505+
{
506+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
507+
}
508+
462509
Action<Context, string> emptyDelegate = (_, _) => { };
463510

464-
return CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
511+
return CacheAsync<TResult>(
512+
cacheProvider,
513+
ttlStrategy,
514+
cacheKeyStrategy.GetCacheKey,
515+
emptyDelegate,
516+
emptyDelegate,
517+
emptyDelegate,
518+
onCacheError,
519+
onCacheError);
465520
}
466521

467522
/// <summary>
@@ -479,11 +534,28 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<
479534
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
480535
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
481536
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
482-
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
537+
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
538+
IAsyncCacheProvider<TResult> cacheProvider,
539+
ITtlStrategy<TResult> ttlStrategy,
540+
ICacheKeyStrategy cacheKeyStrategy,
541+
Action<Context, string, Exception>? onCacheError = null)
483542
{
543+
if (cacheKeyStrategy is null)
544+
{
545+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
546+
}
547+
484548
Action<Context, string> emptyDelegate = (_, _) => { };
485549

486-
return CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
550+
return CacheAsync<TResult>(
551+
cacheProvider,
552+
ttlStrategy,
553+
cacheKeyStrategy.GetCacheKey,
554+
emptyDelegate,
555+
emptyDelegate,
556+
emptyDelegate,
557+
onCacheError,
558+
onCacheError);
487559
}
488560

489561
/// <summary>
@@ -666,9 +738,23 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
666738
Action<Context, string> onCacheMiss,
667739
Action<Context, string> onCachePut,
668740
Action<Context, string, Exception>? onCacheGetError,
669-
Action<Context, string, Exception>? onCachePutError) =>
670-
CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss,
671-
onCachePut, onCacheGetError, onCachePutError);
741+
Action<Context, string, Exception>? onCachePutError)
742+
{
743+
if (cacheKeyStrategy is null)
744+
{
745+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
746+
}
747+
748+
return CacheAsync<TResult>(
749+
cacheProvider,
750+
new RelativeTtl(ttl),
751+
cacheKeyStrategy.GetCacheKey,
752+
onCacheGet,
753+
onCacheMiss,
754+
onCachePut,
755+
onCacheGetError,
756+
onCachePutError);
757+
}
672758

673759
/// <summary>
674760
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -700,8 +786,23 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
700786
Action<Context, string> onCacheMiss,
701787
Action<Context, string> onCachePut,
702788
Action<Context, string, Exception>? onCacheGetError,
703-
Action<Context, string, Exception>? onCachePutError) =>
704-
CacheAsync<TResult>(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
789+
Action<Context, string, Exception>? onCachePutError)
790+
{
791+
if (cacheKeyStrategy is null)
792+
{
793+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
794+
}
795+
796+
return CacheAsync<TResult>(
797+
cacheProvider,
798+
ttlStrategy.For<TResult>(),
799+
cacheKeyStrategy.GetCacheKey,
800+
onCacheGet,
801+
onCacheMiss,
802+
onCachePut,
803+
onCacheGetError,
804+
onCachePutError);
805+
}
705806

706807
/// <summary>
707808
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
@@ -733,8 +834,23 @@ public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
733834
Action<Context, string> onCacheMiss,
734835
Action<Context, string> onCachePut,
735836
Action<Context, string, Exception>? onCacheGetError,
736-
Action<Context, string, Exception>? onCachePutError) =>
737-
CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
837+
Action<Context, string, Exception>? onCachePutError)
838+
{
839+
if (cacheKeyStrategy is null)
840+
{
841+
throw new ArgumentNullException(nameof(cacheKeyStrategy));
842+
}
843+
844+
return CacheAsync<TResult>(
845+
cacheProvider,
846+
ttlStrategy,
847+
cacheKeyStrategy.GetCacheKey,
848+
onCacheGet,
849+
onCacheMiss,
850+
onCachePut,
851+
onCacheGetError,
852+
onCachePutError);
853+
}
738854

739855
/// <summary>
740856
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>

test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs

+79-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,85 @@ public void Should_throw_when_ttl_strategy_is_null()
6868
public void Should_throw_when_cache_key_strategy_is_null()
6969
{
7070
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
71-
Func<Context, string> cacheKeyStrategy = null!;
72-
Action action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, TimeSpan.MaxValue, cacheKeyStrategy);
73-
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheKeyStrategy");
71+
var ttl = TimeSpan.MaxValue;
72+
ITtlStrategy ttlStrategy = new ContextualTtl();
73+
ICacheKeyStrategy cacheKeyStrategy = null!;
74+
Func<Context, string> cacheKeyStrategyFunc = null!;
75+
Action<Context, string> onCacheGet = (_, _) => { };
76+
Action<Context, string> onCacheMiss = (_, _) => { };
77+
Action<Context, string> onCachePut = (_, _) => { };
78+
Action<Context, string, Exception>? onCacheGetError = null;
79+
Action<Context, string, Exception>? onCachePutError = null;
80+
const string CacheKeyStrategyExpected = "cacheKeyStrategy";
81+
82+
Action action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttl, cacheKeyStrategy, onCacheGetError);
83+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
84+
85+
action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError);
86+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
87+
88+
action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttl, cacheKeyStrategyFunc);
89+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
90+
91+
action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider.AsyncFor<ResultPrimitive>(), ttl, cacheKeyStrategy);
92+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
93+
94+
action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider.AsyncFor<ResultPrimitive>(), ttlStrategy, cacheKeyStrategy);
95+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
96+
97+
action = () => Policy.CacheAsync<ResultPrimitive>(
98+
cacheProvider.AsyncFor<ResultPrimitive>(),
99+
ttlStrategy.For<ResultPrimitive>(),
100+
cacheKeyStrategy,
101+
onCacheGetError);
102+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
103+
104+
action = () => Policy.CacheAsync<ResultPrimitive>(
105+
cacheProvider.AsyncFor<ResultPrimitive>(),
106+
ttl,
107+
cacheKeyStrategy,
108+
onCacheGetError);
109+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
110+
111+
action = () => Policy.CacheAsync<ResultPrimitive>(
112+
cacheProvider.AsyncFor<ResultPrimitive>(),
113+
ttl,
114+
cacheKeyStrategy,
115+
onCacheGetError);
116+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
117+
118+
action = () => Policy.CacheAsync<ResultPrimitive>(
119+
cacheProvider.AsyncFor<ResultPrimitive>(),
120+
ttl,
121+
cacheKeyStrategy,
122+
onCacheGet,
123+
onCacheMiss,
124+
onCachePut,
125+
onCacheGetError,
126+
onCachePutError);
127+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
128+
129+
action = () => Policy.CacheAsync<ResultPrimitive>(
130+
cacheProvider.AsyncFor<ResultPrimitive>(),
131+
ttlStrategy,
132+
cacheKeyStrategy,
133+
onCacheGet,
134+
onCacheMiss,
135+
onCachePut,
136+
onCacheGetError,
137+
onCachePutError);
138+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
139+
140+
action = () => Policy.CacheAsync<ResultPrimitive>(
141+
cacheProvider.AsyncFor<ResultPrimitive>(),
142+
ttlStrategy.For<ResultPrimitive>(),
143+
cacheKeyStrategy,
144+
onCacheGet,
145+
onCacheMiss,
146+
onCachePut,
147+
onCacheGetError,
148+
onCachePutError);
149+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
74150
}
75151
#endregion
76152

0 commit comments

Comments
 (0)