Skip to content

Commit

Permalink
Amend exception message on incorrect expiration, throw on double.Nan …
Browse files Browse the repository at this point in the history
…passed to Trim() and test for that case
  • Loading branch information
neon-sunset committed Jul 2, 2022
1 parent 8e6911b commit cfd1d47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/FastCache.Cached/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void QueueFullClear<K, V>() where K : notnull
/// <returns>True: trim is performed inline. False: the count to trim is above threshold and removal is queued to thread pool.</returns>
public static bool Trim<K, V>(double percentage) where K : notnull
{
if (percentage is > 100.0 or <= double.Epsilon)
if (percentage is > 100.0 or <= double.Epsilon or double.NaN)
{
ThrowHelpers.ArgumentOutOfRange(percentage, nameof(percentage));
}
Expand Down
2 changes: 1 addition & 1 deletion src/FastCache.Cached/Helpers/ThrowHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void ArgumentOutOfRange<T>(T argument, string argumentName)
#endif
public static void InvalidExpiration(TimeSpan expiration)
{
throw new ArgumentOutOfRangeException(nameof(expiration), expiration, "Expiration must not be negative, zero or exceed multiple years.");
throw new ArgumentOutOfRangeException(nameof(expiration), expiration, "Expiration must not be negative or zero.");
}

#if NETSTANDARD2_0
Expand Down
22 changes: 22 additions & 0 deletions tests/FastCache.CachedTests/Internals/CacheManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FastCache.Services;

namespace FastCache.CachedTests.Internals;

public sealed class CacheManagerTests
{
[Theory]
[InlineData(double.NaN)]
[InlineData(double.MinValue)]
[InlineData(double.NegativeInfinity)]
[InlineData(-double.Epsilon)]
[InlineData(100.00001)]
[InlineData(100.1D)]
[InlineData(double.MaxValue)]
[InlineData(double.PositiveInfinity)]
public void Trim_Throws_OnInvalidPercentage(double percentage)
{
void Trim() => CacheManager.Trim<int, object>(percentage);

Assert.Throws<ArgumentOutOfRangeException>(Trim);
}
}

0 comments on commit cfd1d47

Please sign in to comment.