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#ContextualTtl #2217

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
6 changes: 5 additions & 1 deletion src/Polly/Caching/ContextualTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Polly.Caching;
/// <summary>
/// Defines a ttl strategy which will cache items for a TimeSpan which may be influenced by data in the execution context.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class ContextualTtl : ITtlStrategy
{
/// <summary>
Expand All @@ -28,6 +27,11 @@ public class ContextualTtl : ITtlStrategy
/// <returns>TimeSpan.</returns>
public Ttl GetTtl(Context context, object? result)
{
if (context is null)
Copy link
Contributor Author

@Zombach Zombach Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martincostello
Which is better to use? == or the pattern is
The is mapping pattern, but mostly the code uses ==

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use is for new code where it is equivalent to == null. Don't worry about updating existing code to match - it was written before is was a thing.

{
throw new ArgumentNullException(nameof(context));
}

if (!context.ContainsKey(TimeSpanKey))
{
return NoTtl;
Expand Down
8 changes: 8 additions & 0 deletions test/Polly.Specs/Caching/ContextualTtlSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public class ContextualTtlSpecs
{
[Fact]
public void Should_throw_when_context_is_null()
{
Context context = null!;
Action action = () => new ContextualTtl().GetTtl(context, null);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("context");
}

[Fact]
public void Should_return_zero_if_no_value_set_on_context() =>
new ContextualTtl().GetTtl(new Context("someOperationKey"), null).Timespan.Should().Be(TimeSpan.Zero);
Expand Down