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#CacheTResultSyntax #2241

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
122 changes: 108 additions & 14 deletions src/Polly/Caching/CacheTResultSyntax.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 @@ -70,6 +69,11 @@
throw new ArgumentNullException(nameof(cacheProvider));
}

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

Check warning on line 74 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L74

Added line #L74 was not covered by tests
}

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

Expand All @@ -88,13 +92,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 CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static CachePolicy<TResult> Cache<TResult>(
ISyncCacheProvider 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 Cache<TResult>(cacheProvider.For<TResult>(), ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheError);
}

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

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

Check warning on line 274 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L274

Added line #L274 was not covered by tests
}

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

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

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

Check warning on line 319 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L319

Added line #L319 was not covered by tests
}

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

Expand Down Expand Up @@ -439,8 +462,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 CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
Cache<TResult>(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
public static CachePolicy<TResult> Cache<TResult>(
ISyncCacheProvider<TResult> cacheProvider,
TimeSpan ttl,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

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

Check warning on line 476 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L476

Added line #L476 was not covered by tests
}

/// <summary>
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand All @@ -457,12 +491,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 CachePolicy<TResult> Cache<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
public static CachePolicy<TResult> Cache<TResult>(
ISyncCacheProvider<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 Cache<TResult>(cacheProvider, ttlStrategy.For<TResult>(), cacheKeyStrategy.GetCacheKey,
emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
return Cache<TResult>(
cacheProvider,
ttlStrategy.For<TResult>(),
cacheKeyStrategy.GetCacheKey,
emptyDelegate,
emptyDelegate,
emptyDelegate,
onCacheError,
onCacheError);

Check warning on line 515 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L507-L515

Added lines #L507 - L515 were not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -670,9 +720,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
Cache<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 Cache<TResult>(
cacheProvider,
new RelativeTtl(ttl),
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);

Check warning on line 738 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L730-L738

Added lines #L730 - L738 were not covered by tests
}

/// <summary>
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down Expand Up @@ -704,8 +768,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
Cache<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 Cache<TResult>(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
}

/// <summary>
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down Expand Up @@ -737,8 +816,23 @@
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
Cache<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 Cache<TResult>(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);

Check warning on line 834 in src/Polly/Caching/CacheTResultSyntax.cs

View check run for this annotation

Codecov / codecov/patch

src/Polly/Caching/CacheTResultSyntax.cs#L826-L834

Added lines #L826 - L834 were not covered by tests
}

/// <summary>
/// <para>Builds a <see cref="Policy{TResult}" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para>
Expand Down
63 changes: 60 additions & 3 deletions test/Polly.Specs/Caching/CacheTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ public class CacheTResultSpecs : IDisposable
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<ResultPrimitive>(cacheProvider, TimeSpan.MaxValue);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");

action = () => Policy.Cache<ResultPrimitive>(cacheProvider, new ContextualTtl(), cacheKeyStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");
}

[Fact]
Expand All @@ -26,9 +31,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<Context, string> cacheKeyStrategy = null!;
Action action = () => Policy.Cache<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.Cache<ResultPrimitive>(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGetError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

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

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

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

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

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

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

#endregion
Expand Down