Skip to content

Add simulated response latency .After(TimeSpan) - #134

Merged
dennisdoomen merged 2 commits into
mainfrom
dennisdoomen/response-latency-after
Aug 10, 2026
Merged

Add simulated response latency .After(TimeSpan)#134
dennisdoomen merged 2 commits into
mainfrom
dennisdoomen/response-latency-after

Conversation

@dennisdoomen

Copy link
Copy Markdown
Owner

Closes #117

Stacked PR: This builds on #132 (the async-responder pipeline from #120) and targets dennisdoomen/async-responder-pipeline, not main. Review/merge #132 first.

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 the RequestMock and the async response path awaits Task.Delay(delay, cancellationToken) before invoking the responder, reusing the CancellationToken already threaded from MockHttpMessageHandler.SendAsync through HttpMock.HandleRequest into RequestMock.TrackRequestAsync by the stacked async-responder work.

A cancelled token throws TaskCanceledException/OperationCanceledException, exactly as a real HttpClient would (e.g. when HttpClient.Timeout is shorter than the delay, or when the caller's token is cancelled).

mock.ForGet().WithPath("/slow")
    .RespondsWithStatus(HttpStatusCode.OK)
    .After(TimeSpan.FromSeconds(2));

New public API

  • RequestMockResponseBuilder RequestMockResponseBuilder.After(TimeSpan delay)

The change is purely additive and backward-compatible. A negative delay throws ArgumentOutOfRangeException.

Tests

New WhenSimulatingResponseLatency specs (xUnit + FluentAssertions, AAA):

  • response is delayed by the configured amount
  • HttpClient.Timeout shorter than the delay throws TaskCanceledException
  • an externally cancelled token cancels the wait
  • a negative delay is rejected

Verified green on both net472 and net8.0. Updated the public-API approval files (net472.verified.txt, net8.0.verified.txt) via AcceptApiChanges.ps1, and added docs to website/docs/advanced.md, intro.md, README.md, and SKILL.md.

@dennisdoomen dennisdoomen added the enhancement New feature or request label May 30, 2026
Comment thread Mockly.Specs/HttpMockSpecs.cs Fixed
Comment thread Mockly.Specs/HttpMockSpecs.cs Fixed
Comment thread Mockly/RequestMockResponseBuilder.cs Fixed
@github-actions

github-actions Bot commented May 30, 2026

Copy link
Copy Markdown

Test Results

  3 files  ±0    3 suites  ±0   8s ⏱️ +2s
203 tests +4  203 ✅ +4  0 💤 ±0  0 ❌ ±0 
403 runs  +8  403 ✅ +8  0 💤 ±0  0 ❌ ±0 

Results for commit 08c2b4d. ± Comparison against base commit cfe4f64.

♻️ This comment has been updated with latest results.

Base automatically changed from dennisdoomen/async-responder-pipeline to main June 20, 2026 14:27
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>
@dennisdoomen
dennisdoomen force-pushed the dennisdoomen/response-latency-after branch from a2b689e to e5cb9bf Compare August 6, 2026 08:31
Comment thread Mockly.Specs/HttpMockSpecs.cs Fixed
@dennisdoomen
dennisdoomen marked this pull request as ready for review August 10, 2026 11:19
- Avoid capturing a disposed CancellationTokenSource in a closure by capturing the token value instead
- Remove unused async modifier on a synchronous test
- Suppress false-positive unused-return-value warning on the fluent After() builder method

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen merged commit e2fa771 into main Aug 10, 2026
10 checks passed
@dennisdoomen
dennisdoomen deleted the dennisdoomen/response-latency-after branch August 10, 2026 11:31
@dennisdoomen dennisdoomen changed the title Add simulated response latency .After(TimeSpan) (closes #117) Add simulated response latency .After(TimeSpan) Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add simulated response latency (.After(TimeSpan)) for timeout/cancellation/resilience testing

2 participants