Add simulated response latency .After(TimeSpan) (closes #117) - #134
Draft
dennisdoomen wants to merge 2 commits into
Draft
Add simulated response latency .After(TimeSpan) (closes #117)#134dennisdoomen wants to merge 2 commits into
dennisdoomen wants to merge 2 commits into
Conversation
Introduce an internal async responder path on RequestMock and converge the synchronous Responder onto a single async execution path (TrackRequestAsync). Thread the CancellationToken from MockHttpMessageHandler.SendAsync through HttpMock.HandleRequest into RequestMock. Add public RespondsWith overloads accepting Func<RequestInfo, Task<HttpResponseMessage>> and Func<RequestInfo, CancellationToken, Task<HttpResponseMessage>>. The existing synchronous Responder property and TrackRequest method are preserved. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add an After(TimeSpan) method on RequestMockResponseBuilder that stores a delay on the RequestMock and awaits Task.Delay before invoking the responder on the async response path, honoring the CancellationToken flowing from SendAsync. A cancelled token (e.g. via HttpClient.Timeout or an external token) throws TaskCanceledException/OperationCanceledException as a real HttpClient would. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| } | ||
|
|
||
| [Fact] | ||
| public async Task A_negative_delay_is_rejected() |
| using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); | ||
|
|
||
| // Act | ||
| Func<Task> act = () => client.GetAsync("https://localhost/slow", cts.Token); |
| /// </remarks> | ||
| /// <param name="delay">The amount of time to wait before producing the response.</param> | ||
| /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="delay"/> is negative.</exception> | ||
| public RequestMockResponseBuilder After(TimeSpan delay) |
dennisdoomen
force-pushed
the
dennisdoomen/async-responder-pipeline
branch
from
June 20, 2026 14:06
e3fc9d6 to
7df3f66
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #117
Summary
Adds a way to simulate a slow endpoint so tests can exercise timeout, cancellation, and resilience (e.g. Polly) behavior.
After(TimeSpan delay)stores the delay on theRequestMockand the async response path awaitsTask.Delay(delay, cancellationToken)before invoking the responder, reusing theCancellationTokenalready threaded fromMockHttpMessageHandler.SendAsyncthroughHttpMock.HandleRequestintoRequestMock.TrackRequestAsyncby the stacked async-responder work.A cancelled token throws
TaskCanceledException/OperationCanceledException, exactly as a realHttpClientwould (e.g. whenHttpClient.Timeoutis shorter than the delay, or when the caller's token is cancelled).New public API
RequestMockResponseBuilder RequestMockResponseBuilder.After(TimeSpan delay)The change is purely additive and backward-compatible. A negative delay throws
ArgumentOutOfRangeException.Tests
New
WhenSimulatingResponseLatencyspecs (xUnit + FluentAssertions, AAA):HttpClient.Timeoutshorter than the delay throwsTaskCanceledExceptionVerified green on both
net472andnet8.0. Updated the public-API approval files (net472.verified.txt,net8.0.verified.txt) viaAcceptApiChanges.ps1, and added docs towebsite/docs/advanced.md,intro.md,README.md, andSKILL.md.