Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
271cfd7
Add core RateLimiter implementations
reisenberger Mar 23, 2019
7d70287
Add example async TResult rate-limiter policy implementation
reisenberger Mar 23, 2019
6aa1e2a
Add example syntax
reisenberger Mar 23, 2019
8354e6d
Make the retryAfterFactory take Timespan as an input parameter!
reisenberger Mar 24, 2019
cba0b51
Initial LockFreeTokenBucketRateLimiterTests
reisenberger Jul 4, 2019
193b356
Tidy BulkheadSpecsHelper
reisenberger Jul 4, 2019
5496e66
Factor out test helpers
reisenberger Jul 4, 2019
4b17e56
Factor out common tests; add tests on lock-based rate limiter
reisenberger Jul 4, 2019
bfc3251
Allow for slow-running on CI servers
reisenberger Jul 4, 2019
8fbb193
Add tests on full bucket capacity
reisenberger Jul 4, 2019
d1c7ec8
Fix RateLimitRejectedException
reisenberger Jul 4, 2019
416c6ff
Remove unused configuration overloads
reisenberger Jul 4, 2019
fcaf1ac
Introduce a factory for obtaining the preferred rate-limiter implemen…
reisenberger Jul 4, 2019
ebd8a56
Pull some test helper methods into a common base-class
reisenberger Jul 4, 2019
c7af7e4
Add first specs on async policy syntax
reisenberger Jul 4, 2019
a2f6566
Add full set of specs on rate-limit policies thus far
reisenberger Jul 5, 2019
c92cb6b
Add tests on retryAfterFactory
reisenberger Jul 5, 2019
1532500
Add tests on context passed to retryAfterFactory
reisenberger Jul 5, 2019
56fea85
Add async non-generic syntax and specs
reisenberger Jul 5, 2019
182c635
Improve code layout
reisenberger Jul 5, 2019
2a1c508
Add sync rate-limit policies
reisenberger Jul 5, 2019
fd609ad
Add initial rate-limit doco; bump to v7.2.0
reisenberger Jul 9, 2019
0121b3d
Improve bulkhead doco in readme
reisenberger Jul 9, 2019
e061d50
Minor expressivity refinements
reisenberger Jul 15, 2019
db47862
Neaten bulkhead tests commentary
reisenberger Jul 16, 2019
daa42cb
Control visibility of IRateLimiter components
reisenberger Jul 16, 2019
c916be5
Fix non-generic rate-limit tests to be genuinely non-generic
reisenberger Jul 16, 2019
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
Prev Previous commit
Next Next commit
Add tests on retryAfterFactory
  • Loading branch information
reisenberger committed Jul 5, 2019
commit c92cb6b1dcfa0ac63c1716d6edb77622ac2883f3
21 changes: 21 additions & 0 deletions src/Polly.Specs/Helpers/RateLimit/ResultClassWithRetryAfter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Polly.Specs.Helpers.RateLimit
{
internal class ResultClassWithRetryAfter : ResultClass
{
public TimeSpan RetryAfter { get; }

public ResultClassWithRetryAfter(ResultPrimitive result)
: base(result)
{
RetryAfter = TimeSpan.Zero;
}

public ResultClassWithRetryAfter(TimeSpan retryAfter)
: base(ResultPrimitive.Undefined)
{
RetryAfter = retryAfter;
}
}
}
29 changes: 24 additions & 5 deletions src/Polly.Specs/RateLimit/AsyncRateLimitPolicyTResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Threading.Tasks;
using Polly.RateLimit;
using Polly.Specs.Helpers;
using Polly.Specs.Helpers.RateLimit;
using Polly.Utilities;
using Xunit;

namespace Polly.Specs.RateLimit
{
[Collection(Polly.Specs.Helpers.Constants.SystemClockDependentTestCollection)]
public class AsyncRateLimitPolicyTResultSpecs : RateLimitPolicySpecsBase, IDisposable
public class AsyncRateLimitPolicyTResultSpecs : RateLimitPolicyTResultSpecsBase, IDisposable
{
public void Dispose()
{
Expand All @@ -17,21 +18,27 @@ public void Dispose()

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan)
{
return Policy.RateLimitAsync<ResultClass>(numberOfExecutions, perTimeSpan);
return Policy.RateLimitAsync<ResultClassWithRetryAfter>(numberOfExecutions, perTimeSpan);
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst)
{
return Policy.RateLimitAsync<ResultClass>(numberOfExecutions, perTimeSpan, maxBurst);
return Policy.RateLimitAsync<ResultClassWithRetryAfter>(numberOfExecutions, perTimeSpan, maxBurst);
}

protected override IRateLimitPolicy<TResult> GetPolicyViaSyntax<TResult>(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst,
Func<TimeSpan, Context, TResult> retryAfterFactory)
{
return Policy.RateLimitAsync<TResult>(numberOfExecutions, perTimeSpan, maxBurst, retryAfterFactory);
}

protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy policy)
{
if (policy is AsyncRateLimitPolicy<ResultClass> typedPolicy)
if (policy is AsyncRateLimitPolicy<ResultClassWithRetryAfter> typedPolicy)
{
try
{
typedPolicy.ExecuteAsync(() => Task.FromResult(new ResultClass(ResultPrimitive.Good))).GetAwaiter().GetResult();
typedPolicy.ExecuteAsync(() => Task.FromResult(new ResultClassWithRetryAfter(ResultPrimitive.Good))).GetAwaiter().GetResult();
return (true, TimeSpan.Zero);
}
catch (RateLimitRejectedException e)
Expand All @@ -44,5 +51,17 @@ protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy pol
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}

protected override TResult TryExecuteThroughPolicy<TResult>(IRateLimitPolicy<TResult> policy, TResult resultIfExecutionPermitted)
{
if (policy is AsyncRateLimitPolicy<TResult> typedPolicy)
{
return typedPolicy.ExecuteAsync(() => Task.FromResult(resultIfExecutionPermitted)).GetAwaiter().GetResult();
}
else
{
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}
}
}
47 changes: 47 additions & 0 deletions src/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using FluentAssertions;
using Polly.RateLimit;
using Polly.Specs.Helpers;
using Polly.Specs.Helpers.RateLimit;
using Xunit;

namespace Polly.Specs.RateLimit
{
public abstract class RateLimitPolicyTResultSpecsBase : RateLimitPolicySpecsBase
{
protected abstract IRateLimitPolicy<TResult> GetPolicyViaSyntax<TResult>(
int numberOfExecutions,
TimeSpan perTimeSpan,
int maxBurst,
Func<TimeSpan, Context, TResult> retryAfterFactory);

protected abstract TResult TryExecuteThroughPolicy<TResult>(IRateLimitPolicy<TResult> policy, TResult resultIfExecutionPermitted);

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(5)]
public void Ratelimiter_specifies_correct_wait_until_next_execution_by_custom_factory(int onePerSeconds)
{
FixClock();

// Arrange
TimeSpan onePer = TimeSpan.FromSeconds(onePerSeconds);
Func<TimeSpan, Context, ResultClassWithRetryAfter> retryAfterFactory = (t, ctx) => new ResultClassWithRetryAfter(t);
var rateLimiter = GetPolicyViaSyntax<ResultClassWithRetryAfter>(1, onePer, 1, retryAfterFactory);

// Assert - first execution after initialising should always be permitted.
ShouldPermitAnExecution(rateLimiter);

// Arrange
// (do nothing - time not advanced)

// Act - try another execution.
var resultExpectedBlocked = TryExecuteThroughPolicy(rateLimiter, new ResultClassWithRetryAfter(ResultPrimitive.Good));

// Assert - should be blocked - time not advanced. Result should be expressed per the retryAfterFactory.
resultExpectedBlocked.ResultCode.Should().NotBe(ResultPrimitive.Good);
resultExpectedBlocked.RetryAfter.Should().Be(onePer);
}
}
}