-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dh: Add Delegate after N requests handler
- Loading branch information
1 parent
2d93045
commit 1912fda
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/rm.DelegatingHandlers/DelegateAfterNRequestsHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace rm.DelegatingHandlers; | ||
|
||
/// <summary> | ||
/// Runs delegates after N requests. | ||
/// </summary> | ||
public class DelegateAfterNRequestsHandler : DelegatingHandler | ||
{ | ||
private readonly IDelegateAfterNRequestsHandlerSettings delegateAfterNRequestsHandlerSettings; | ||
|
||
private long n; | ||
|
||
/// <inheritdoc cref="DelegateAfterNRequestsHandler" /> | ||
public DelegateAfterNRequestsHandler( | ||
IDelegateAfterNRequestsHandlerSettings delegateAfterNRequestsHandlerSettings) | ||
{ | ||
this.delegateAfterNRequestsHandlerSettings = delegateAfterNRequestsHandlerSettings | ||
?? throw new ArgumentNullException(nameof(delegateAfterNRequestsHandlerSettings)); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (Interlocked.Read(ref n) >= delegateAfterNRequestsHandlerSettings.N) | ||
{ | ||
await delegateAfterNRequestsHandlerSettings.PreDelegate(request) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
var response = await base.SendAsync(request, cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
if (Interlocked.Read(ref n) < delegateAfterNRequestsHandlerSettings.N) | ||
{ | ||
Interlocked.Increment(ref n); | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
|
||
public interface IDelegateAfterNRequestsHandlerSettings | ||
{ | ||
long N { get; } | ||
Func<HttpRequestMessage, Task> PreDelegate { get; } | ||
} | ||
|
||
public record class DelegateAfterNRequestsHandlerSettings : IDelegateAfterNRequestsHandlerSettings | ||
{ | ||
public long N { get; init; } | ||
public Func<HttpRequestMessage, Task> PreDelegate { get; init; } | ||
} |
99 changes: 99 additions & 0 deletions
99
tests/rm.DelegatingHandlersTest/DelegateAfterNRequestsHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System.Net.Http; | ||
using AutoFixture; | ||
using AutoFixture.AutoMoq; | ||
using NUnit.Framework; | ||
using rm.DelegatingHandlers; | ||
|
||
namespace rm.DelegatingHandlersTest; | ||
|
||
[TestFixture] | ||
public class DelegateAfterNRequestsHandlerTests | ||
{ | ||
[Test] | ||
public async Task Runs_PreDelegate() | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
|
||
var n = 5; | ||
var preDelegateExecutedCount = 0; | ||
var delegateAfterNRequestsHandler = new DelegateAfterNRequestsHandler( | ||
new DelegateAfterNRequestsHandlerSettings | ||
{ | ||
N = n, | ||
PreDelegate = (request) => | ||
{ | ||
preDelegateExecutedCount++; | ||
return Task.CompletedTask; | ||
} | ||
}); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
fixture.Create<HttpMessageHandler>(), delegateAfterNRequestsHandler); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
using var _ = await invoker.SendAsync(fixture.Create<HttpRequestMessage>(), CancellationToken.None); | ||
} | ||
using var __ = await invoker.SendAsync(fixture.Create<HttpRequestMessage>(), CancellationToken.None); | ||
|
||
Assert.AreEqual(1, preDelegateExecutedCount); | ||
} | ||
|
||
[Test] | ||
public async Task Runs_PreDelegate_After() | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
|
||
var n = 5; | ||
var preDelegateExecutedCount = 0; | ||
var delegateAfterNRequestsHandler = new DelegateAfterNRequestsHandler( | ||
new DelegateAfterNRequestsHandlerSettings | ||
{ | ||
N = n, | ||
PreDelegate = (request) => | ||
{ | ||
preDelegateExecutedCount++; | ||
return Task.CompletedTask; | ||
} | ||
}); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
fixture.Create<HttpMessageHandler>(), delegateAfterNRequestsHandler); | ||
|
||
for (int i = 0; i < n + 1; i++) | ||
{ | ||
using var _ = await invoker.SendAsync(fixture.Create<HttpRequestMessage>(), CancellationToken.None); | ||
} | ||
using var __ = await invoker.SendAsync(fixture.Create<HttpRequestMessage>(), CancellationToken.None); | ||
|
||
Assert.AreEqual(2, preDelegateExecutedCount); | ||
} | ||
|
||
[Test] | ||
public async Task Does_Not_Run_PreDelegate() | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
|
||
var n = 5; | ||
var preDelegateExecutedCount = 0; | ||
var delegateAfterNRequestsHandler = new DelegateAfterNRequestsHandler( | ||
new DelegateAfterNRequestsHandlerSettings | ||
{ | ||
N = n, | ||
PreDelegate = (request) => | ||
{ | ||
preDelegateExecutedCount++; | ||
return Task.CompletedTask; | ||
} | ||
}); | ||
|
||
using var invoker = HttpMessageInvokerFactory.Create( | ||
fixture.Create<HttpMessageHandler>(), delegateAfterNRequestsHandler); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
using var _ = await invoker.SendAsync(fixture.Create<HttpRequestMessage>(), CancellationToken.None); | ||
} | ||
Assert.AreEqual(0, preDelegateExecutedCount); | ||
} | ||
} |