Add async responder overloads and CancellationToken flow#132
Merged
Conversation
| .RespondsWith(async (_, ct) => | ||
| { | ||
| #if NET8_0_OR_GREATER | ||
| await cts.CancelAsync(); |
| #if NET8_0_OR_GREATER | ||
| await cts.CancelAsync(); | ||
| #else | ||
| cts.Cancel(); |
| var client = mock.GetClient(); | ||
|
|
||
| // Act | ||
| Func<Task> act = () => client.GetAsync("https://localhost/api/cancel", cts.Token); |
| { | ||
| // Arrange | ||
| var mock = new HttpMock(); | ||
| CancellationToken observedToken = default; |
| var client = mock.GetClient(); | ||
|
|
||
| // Act | ||
| await client.GetAsync("https://localhost/api/observe"); |
Coverage Report for CI Build 27873525411Coverage decreased (-0.7%) to 83.889%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions21 previously-covered lines in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
This was referenced May 30, 2026
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>
e3fc9d6 to
7df3f66
Compare
| // Assert | ||
| capturedInfo.Should().NotBeNull(); | ||
| capturedInfo.Method.Should().Be(HttpMethod.Post); | ||
| capturedInfo.Uri.AbsolutePath.Should().Be("/api/info"); |
| var builder = mock.ForGet().WithPath("/api/null"); | ||
|
|
||
| // Act | ||
| Action act = () => builder.RespondsWith((Func<RequestInfo, Task<HttpResponseMessage>>)null!); |
| var builder = mock.ForGet().WithPath("/api/null-ct"); | ||
|
|
||
| // Act | ||
| Action act = () => builder.RespondsWith((Func<RequestInfo, CancellationToken, Task<HttpResponseMessage>>)null!); |
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; |
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; |
This was referenced Jun 29, 2026
This was referenced Jul 6, 2026
1 task
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 #120
Summary
Adds asynchronous responder support to Mockly and flows the previously-dropped
CancellationTokenthrough the mock pipeline. This is the foundation of an async-pipeline stack — issues #117 (latency) and #116 (simulated failures) will stack on top of this branch and build on the single async execution path introduced here.What changed
RequestMockgains an internal async responder (Func<RequestInfo, CancellationToken, Task<HttpResponseMessage>>). The existing synchronousResponderis adapted onto this path so both sync and async responders converge in a new internalTrackRequestAsync.MockHttpMessageHandler.SendAsync→HttpMock.HandleRequest→RequestMock.TrackRequestAsync(it was previously ignored).OperationCanceledExceptionpropagates out of the pipeline instead of being swallowed into a 500.RequestMock.Responderproperty andRequestMock.TrackRequest(RequestInfo)method are unchanged and still work.New public API
The approved API verification files (
Mockly.ApiVerificationTests/ApprovedApi/*.verified.txt) were regenerated viaAcceptApiChanges.ps1and committed.Tests
New
WhenUsingAsyncRespondersspec class covers: async responder is awaited; async +CancellationTokenoverload is awaited; the token is passed to the responder; a cancelled token is observed and propagates; async responders work with invocation limits and request collection; exceptions surface as 500; and existing synchronous responders still work.Compatibility note
Overloading
RespondsWithon the delegate return type makes a throw-only lambda (RespondsWith(_ => throw ...)) ambiguous, requiring an explicit delegate cast. One existing test was updated accordingly. Lambdas with an actual return value remain unambiguous.Verification
dotnet testgreen for bothnet8.0(95 passed) andnet472(94 passed).Stacking
#117 and #116 will stack on this branch and layer latency/failure simulation onto the async responder pipeline. Opened as draft for maintainer review — do not merge yet.