Skip to content

Commit

Permalink
Fix CA1062 warnings (#2240)
Browse files Browse the repository at this point in the history
Fix CA1062 warnings for `CacheSyntax`.
  • Loading branch information
Zombach authored Jul 23, 2024
1 parent 626f6d1 commit 975a640
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 14 deletions.
54 changes: 47 additions & 7 deletions src/Polly/Caching/CacheSyntax.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 @@ -46,8 +45,19 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t
/// <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 Cache(ISyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
public static CachePolicy Cache(
ISyncCacheProvider cacheProvider,
TimeSpan ttl,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
}

/// <summary>
/// <para>Builds a <see cref="Policy"/> that will function like a result cache for delegate executions returning a result.</para>
Expand Down Expand Up @@ -222,8 +232,23 @@ public static CachePolicy Cache(
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
Cache(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(
cacheProvider,
new RelativeTtl(ttl),
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
}

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

/// <summary>
/// <para>Builds a <see cref="Policy"/> that will function like a result cache for delegate executions returning a result.</para>
Expand Down
232 changes: 225 additions & 7 deletions test/Polly.Specs/Caching/CacheSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,244 @@ public void Should_throw_when_action_is_null()
public void Should_throw_when_cache_provider_is_null()
{
ISyncCacheProvider cacheProvider = null!;
Action action = () => Policy.Cache(cacheProvider, TimeSpan.MaxValue);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string CacheProviderExpected = "cacheProvider";

Action action = () => Policy.Cache(cacheProvider, ttl, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

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

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttl, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

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

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);
}

[Fact]
public void Should_throw_when_ttl_strategy_is_null()
{
ISyncCacheProvider cacheProvider = new StubCacheProvider();
ITtlStrategy ttlStrategy = null!;
Action action = () => Policy.Cache(cacheProvider, ttlStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("ttlStrategy");
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string TtlStrategyExpected = "ttlStrategy";

Action action = () => Policy.Cache(cacheProvider, ttlStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);
}

[Fact]
public void Should_throw_when_cache_key_strategy_is_null()
{
ISyncCacheProvider cacheProvider = new StubCacheProvider();
Func<Context, string> cacheKeyStrategy = null!;
Action action = () => Policy.Cache(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> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string CacheKeyStrategyExpected = "cacheKeyStrategy";

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

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

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

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

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

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

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

action = () => Policy.Cache(
cacheProvider,
ttlStrategy,
cacheKeyStrategyFunc,
onCache,
onCache,
onCache,
onCacheError,
onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
}

[Fact]
public void Should_throw_when_on_cache_get_is_null()
{
ISyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCacheGet = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCacheGetExpected = "onCacheGet";

Action action = () => Policy.Cache(cacheProvider, ttl, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);
}

[Fact]
public void Should_throw_when_on_cache_miss_is_null()
{
ISyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCacheMiss = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCacheMissExpected = "onCacheMiss";

Action action = () => Policy.Cache(cacheProvider, ttl, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);
}

[Fact]
public void Should_throw_when_on_cache_put_is_null()
{
ISyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCachePut = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCachePutExpected = "onCachePut";

Action action = () => Policy.Cache(cacheProvider, ttl, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.Cache(cacheProvider, ttl, cacheKeyStrategyFunc, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.Cache(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);
}

[Fact]
Expand Down

0 comments on commit 975a640

Please sign in to comment.