Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning CA1062#AsyncCacheTResultSyntax #2243

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 132 additions & 16 deletions src/Polly/Caching/AsyncCacheTResultSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable
namespace Polly;

#pragma warning disable CA1062 // Validate arguments of public methods
public partial class Policy
{
/// <summary>
Expand Down Expand Up @@ -63,13 +62,22 @@
/// <returns>The policy instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
IAsyncCacheProvider cacheProvider,
TimeSpan ttl,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheProvider == null)
{
throw new ArgumentNullException(nameof(cacheProvider));
}

if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
}

Expand All @@ -88,13 +96,22 @@
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
IAsyncCacheProvider cacheProvider,
ITtlStrategy ttlStrategy,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheProvider == null)
{
throw new ArgumentNullException(nameof(cacheProvider));
}

if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError);
}

Expand Down Expand Up @@ -256,6 +273,11 @@
throw new ArgumentNullException(nameof(cacheProvider));
}

if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));

Check warning on line 278 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L278

Added line #L278 was not covered by tests
}

return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
}

Expand Down Expand Up @@ -296,6 +318,11 @@
throw new ArgumentNullException(nameof(cacheProvider));
}

if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));

Check warning on line 323 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L323

Added line #L323 was not covered by tests
}

return CacheAsync<TResult>(cacheProvider.AsyncFor<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
}

Expand Down Expand Up @@ -439,8 +466,19 @@
/// <returns>The policy instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
IAsyncCacheProvider<TResult> cacheProvider,
TimeSpan ttl,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);

Check warning on line 480 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L480

Added line #L480 was not covered by tests
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand All @@ -457,11 +495,28 @@
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
IAsyncCacheProvider<TResult> cacheProvider,
ITtlStrategy ttlStrategy,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

Action<Context, string> emptyDelegate = (_, _) => { };

return CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
return CacheAsync<TResult>(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
emptyDelegate,
emptyDelegate,
emptyDelegate,
onCacheError,
onCacheError);

Check warning on line 519 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L511-L519

Added lines #L511 - L519 were not covered by tests
}

/// <summary>
Expand All @@ -479,11 +534,28 @@
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="ttlStrategy"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static AsyncCachePolicy<TResult> CacheAsync<TResult>(
IAsyncCacheProvider<TResult> cacheProvider,
ITtlStrategy<TResult> ttlStrategy,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

Action<Context, string> emptyDelegate = (_, _) => { };

return CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
return CacheAsync<TResult>(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
emptyDelegate,
emptyDelegate,
emptyDelegate,
onCacheError,
onCacheError);

Check warning on line 558 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L550-L558

Added lines #L550 - L558 were not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -666,9 +738,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss,
onCachePut, onCacheGetError, onCachePutError);
Action<Context, string, Exception>? onCachePutError)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(
cacheProvider,
new RelativeTtl(ttl),
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);

Check warning on line 756 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L748-L756

Added lines #L748 - L756 were not covered by tests
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down Expand Up @@ -700,8 +786,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync<TResult>(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
Action<Context, string, Exception>? onCachePutError)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(
cacheProvider,
ttlStrategy.For<TResult>(),
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down Expand Up @@ -733,8 +834,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync<TResult>(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
Action<Context, string, Exception>? onCachePutError)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync<TResult>(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);

Check warning on line 852 in src/Polly/Caching/AsyncCacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/AsyncCacheTResultSyntax.cs#L844-L852

Added lines #L844 - L852 were not covered by tests
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down
82 changes: 79 additions & 3 deletions test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,85 @@ public void Should_throw_when_ttl_strategy_is_null()
public void Should_throw_when_cache_key_strategy_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
Func<Context, string> cacheKeyStrategy = null!;
Action action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, TimeSpan.MaxValue, cacheKeyStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheKeyStrategy");
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = null!;
Func<Context, string> cacheKeyStrategyFunc = null!;
Action<Context, string> onCacheGet = (_, _) => { };
Action<Context, string> onCacheMiss = (_, _) => { };
Action<Context, string> onCachePut = (_, _) => { };
Action<Context, string, Exception>? onCacheGetError = null;
Action<Context, string, Exception>? onCachePutError = null;
const string CacheKeyStrategyExpected = "cacheKeyStrategy";

Action action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttl, cacheKeyStrategy, onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider, ttl, cacheKeyStrategyFunc);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider.AsyncFor<ResultPrimitive>(), ttl, cacheKeyStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(cacheProvider.AsyncFor<ResultPrimitive>(), ttlStrategy, cacheKeyStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttlStrategy.For<ResultPrimitive>(),
cacheKeyStrategy,
onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttl,
cacheKeyStrategy,
onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttl,
cacheKeyStrategy,
onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttl,
cacheKeyStrategy,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttlStrategy,
cacheKeyStrategy,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync<ResultPrimitive>(
cacheProvider.AsyncFor<ResultPrimitive>(),
ttlStrategy.For<ResultPrimitive>(),
cacheKeyStrategy,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
}
#endregion

Expand Down