Skip to content

Commit c75b43b

Browse files
authored
Fix CA1062 warning (#2217)
Fix for `ContextualTtl`.
1 parent e4ffcd4 commit c75b43b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Polly/Caching/ContextualTtl.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace Polly.Caching;
44
/// <summary>
55
/// Defines a ttl strategy which will cache items for a TimeSpan which may be influenced by data in the execution context.
66
/// </summary>
7-
#pragma warning disable CA1062 // Validate arguments of public methods
87
public class ContextualTtl : ITtlStrategy
98
{
109
/// <summary>
@@ -28,6 +27,11 @@ public class ContextualTtl : ITtlStrategy
2827
/// <returns>TimeSpan.</returns>
2928
public Ttl GetTtl(Context context, object? result)
3029
{
30+
if (context is null)
31+
{
32+
throw new ArgumentNullException(nameof(context));
33+
}
34+
3135
if (!context.ContainsKey(TimeSpanKey))
3236
{
3337
return NoTtl;

test/Polly.Specs/Caching/ContextualTtlSpecs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
public class ContextualTtlSpecs
44
{
5+
[Fact]
6+
public void Should_throw_when_context_is_null()
7+
{
8+
Context context = null!;
9+
Action action = () => new ContextualTtl().GetTtl(context, null);
10+
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("context");
11+
}
12+
513
[Fact]
614
public void Should_return_zero_if_no_value_set_on_context() =>
715
new ContextualTtl().GetTtl(new Context("someOperationKey"), null).Timespan.Should().Be(TimeSpan.Zero);

0 commit comments

Comments
 (0)