Repository navigation
feat: add consumer-side support for synchronous (request/response) messages - #562
fabio-andre-rodrigues wants to merge 15 commits into
Conversation
|
Thanks @fabio-andre-rodrigues! Have you reviewed this against #551, which is about creating synchronous messages for plugins? @adamrodger what's your view on how we should approach this? There is also some inspo we can take from https://github.com/YOU54F/pact-net/tree/feat/sync-messaging |
|
Thanks for the pointer, hadn't seen #551 yet. Just went through it. They don't conflict behaviourally. #551 reuses So I don't think the two need a unified API: one is generation-based consumer testing for plain sync messages (message queues, AsyncAPI reply blocks), the other is mock-server-based testing for plugin protocols. A single entry point covering both would need either two different code paths hidden behind one method, or forcing one model to do the other's job - and neither works. A message-queue consumer test has no client to dial a socket with; a gRPC client under test can't be exercised by handing it synthetic generated JSON instead of a real wire call. PactNet already draws this line between What will conflict mechanically, whichever of us merges second:
The YOU54F fork adds @adamrodger, what's your take? |
|
Well, that's certainly a big one 😂 I think this needs a more in depth review, and needs taking into account alongside other PRs to do with sync/async messaging and plugins. I'll confess I've got really lost in the terminology and use cases of all of the interaction modes now on offer, and trying to keep up as a volunteer is very difficult. Perhaps I can lean on @mefellows and @YOU54F to start to distill some of this info a plan - the interaction modes we need to support, the FFI methods that support them and the in-flight PRs that might address these. Then we can start to think about how to bring them all together into a rational API (although the above sounds like some good thought has already gone into that, so thank you @fabio-andre-rodrigues). I want to get this landed, but it's a big old task that we don't really want to fumble, so I think needs more concentrated effort. I'd appreciate any help from the full time members of the Pact crew to map out what needs to happen ❤️ |
|
@adamrodger here's a map of the messaging modes, the FFI behind each one, and where the open PRs fit. Modes
Sync and plugin sync both write FFI → PactNetAsync (master)
Sync (#562)
Plugin sync (#551)
Provider side, all modes: |
|
Happy to help review this PR and others, @fabio-andre-rodrigues has done a really excellent overview of the modes and functions above. I'll put it on my list for this week, as would love to see these changes land in pact-net <3 |
|
Hey @fabio-andre-rodrigues, released 0.5.10 of the ffi with your merged PR bringing in the new function |
|
Updated on top of pact_ffi 0.5.10:
|
YOU54F
left a comment
There was a problem hiding this comment.
Tested e2e with pact-foundation/pact-reference#557
nice work.
Appreciate the docs update, I think the last cherry on top would be updating the sample app to cover a sync message use case.
Merged in Max's changes from #551, there are a copy of minor changes, dupe ffi import, and the namespace for the message drivers.
src/PactNet/Drivers/IMessageInteractionDriver.cs renamed to src/PactNet/Drivers/Message/IMessageInteractionDriver.cs
but apart from that merged cleanly and everything is green, I'll mark as approved once the next ffi change is merged/released, sample app updated.
Hopefully by then Max's change will land, and we can rebase of it, and get this out.
|
Great work @fabio-andre-rodrigues, and thank you for the review Yousaf! @adamrodger The compatibility suite might be a good guide in terms of what should be supported: https://github.com/pact-foundation/pact-compatibility-suite/blob/main/features/V4/synchronous_message_consumer.feature I don't think it includes plugins however, so that's probably something we ought to review and get into the suite (cc @rholshausen @JP-Ellis) What do you think would most help here @adamrodger - perhaps a walk-through / mob programming session? |
|
Latest FFI released https://github.com/pact-foundation/pact-reference/releases/tag/libpact_ffi-v0.5.11 containing your change |
65c27b7 to
05fdef9
Compare
Update native library downloads and refresh Rust version metadata in integration fixtures and the Orders API sample pact. Retain existing repeated-header expectations and pact representations.
…ssages PactNet already supports writing asynchronous (fire-and-forget) message pacts, but had no way to write V4 synchronous message pacts (used by e.g. AsyncAPI 3 operations with a reply block), even though the underlying pact_ffi native library already exposes pactffi_new_sync_message_interaction and can set request/response bodies and metadata on it via the existing generic pactffi_with_body/pactffi_with_metadata functions. This adds: - IPactV4.WithSynchronousMessageInteractions() entry point - ISyncMessagePactBuilderV4 / ISyncMessageBuilderV4 / IConfiguredSyncMessageVerifier public API, mirroring the existing async IMessagePactBuilderV4 API - SyncMessageInteractionDriver, wiring the above onto the native FFI calls The consumer-side Verify<TRequest, TResponse> reads the generated request contents (matchers stripped, generators applied) via the new pactffi_sync_message_reify FFI function and feeds it to the handler under test; the response content is stored but not currently asserted against, matching the existing async message Verify<T> semantics. Depends on pactffi_sync_message_reify, proposed in pact-foundation/pact-reference#555. Until that lands in a released pact_ffi build, calling ConfiguredSyncMessageVerifier.Verify will fail with EntryPointNotFoundException - the corresponding FFI integration test is marked Skip until then.
Removed redundant phrase from summary comment.
Callers of PactNet don't need to know the Pact-internal term "reify" - GenerateContents describes what the method actually returns. Matches the pactffi_sync_message_generate_contents rename on the pact-reference side (pact-foundation/pact-reference#555). The pre-existing async message Reify()/pactffi_message_reify naming is untouched.
Adds a section to messaging-pacts.md covering WithSynchronousMessageInteractions, alongside the existing fire-and-forget message docs. Notes that provider verification isn't covered yet and that this depends on pactffi_sync_message_generate_contents landing upstream.
Removed information about provider verification support and related issues.
MessageInteractionDriver.Reify() and SyncMessageInteractionDriver.GenerateContents() copied the FFI-returned string into managed memory but never freed the native allocation, leaking it on every call.
Verify/VerifyAsync already required the handler to return a TResponse but never checked it, so a consumer test would pass regardless of what the handler returned. Compare it against the FFI-generated response content (matchers stripped, generators applied) via a structural JSON equality check, so a mismatch fails the test instead of being silently written to the pact file.
pact-foundation#551 reorganises Drivers/ into Http/, Message/ and Plugins/ subfolders. The sync message driver belongs in Message/ alongside the async one, since it's the same message-queue-style request/response category, not a plugin. Namespace stays PactNet.Drivers for now since the rest of the tree hasn't moved yet; that follows once pact-foundation#551 lands.
pactffi_with_metadata only applies response metadata to responses that already exist, and the response is created by the terminal WithResponseJsonContent call, so response metadata was always dropped. The builder now buffers it and applies it after the response content. Request/response metadata values are now dynamic and serialised as JSON, so strings, numbers, booleans and matchers keep their type in the pact instead of relying on the FFI's parse-or-fallback-to-string behaviour.
Un-skip the FFI integration test now that 0.5.10 ships pactffi_sync_message_generate_contents, and compare the written pact against a fixture. Add public API tests for WithSynchronousMessageInteractions covering body matchers, typed metadata, response metadata matchers and a mismatching response.
Links a sync message interaction to an external resource such as an
AsyncAPI operation via pactffi_add_interaction_reference, written to
comments.references.{group}.{name}. Same signature as the async
WithReference proposed in pact-foundation#559.
The FFI parses the value as JSON, so the driver encodes it to keep a
string reference value a string in the pact.
Verify/VerifyAsync compared the handler's response to the generated example by exact JSON equality, so a type matcher still required the example value. They now call pactffi_sync_message_match_response_contents, which applies the response's matching rules the same way the verifier does: Match.Type checks the type of the example, Match.Integer and Match.Decimal the kind of number, Match.Regex the format (e.g. datetimes), and unexpected keys are allowed. Mismatches are reported by path. Requires a pact_ffi release with pactffi_sync_message_match_response_contents.
Aligns the sync message integration fixtures with the ffi 0.5.12 / models 1.3.17 metadata from the pact-ffi upgrade.
05fdef9 to
c1acf8d
Compare
|
Check out the comments here You can rebase off #569 Hoping to get this out soon 🤞🏾 |
Summary
PactNet currently only supports writing asynchronous (fire-and-forget) message pacts (
IPactV4.WithMessageInteractions()). There is no way to write a V4 synchronous (request/response) message pact - the type PactFlow uses for AsyncAPI 3 operations that have areplyblock.The underlying
pact_ffinative library this project wraps already has most of what's needed:pactffi_new_sync_message_interactionexists in the native lib, but wasn't yet declared inNativeInterop.cspactffi_with_body/pactffi_with_metadataFFI functions (already used for HTTP interactions in this codebase) work against a synchronous message interaction handle too, since they take anInteractionPart(Request/Response)The one missing piece was a way to retrieve the generated (matchers stripped, generators applied) request/response contents of a synchronous message interaction for the consumer test -
pactffi_message_reifyonly supports async messages. I've proposedpactffi_sync_message_generate_contentsupstream in pact-reference: pact-foundation/pact-reference#555Changes
IPactV4.WithSynchronousMessageInteractions()- new entry point, parallel toWithMessageInteractions()ISyncMessagePactBuilderV4/ISyncMessageBuilderV4/IConfiguredSyncMessageVerifier- public API mirroring the existing asyncIMessagePactBuilderV4/IMessageBuilderV4/IConfiguredMessageVerifier, but with separateWithRequest*/WithResponse*methods for request and response content/metadataSyncMessageInteractionDriver/SyncMessageBuilder/SyncMessagePactBuilder/ConfiguredSyncMessageVerifier- internal implementation, following the existing async message classes as closely as possibleNativeInterop.NewSyncMessageInteraction,NativeInterop.WithMetadata(theInteractionPart-aware variant used for both HTTP and sync messages),NativeInterop.SyncMessageGenerateContentsConfiguredSyncMessageVerifier.Verify<TRequest, TResponse>reads the generated request viaISyncMessageInteractionDriver.GenerateContents()and passes it to the handler under test; the response content is stored in the pact but not currently asserted against the handler's return value, matching the existing asyncVerify<T>(Action<T>)semantics (which likewise doesn't assert anything beyond "the handler didn't throw"). Response-value assertion could be a good follow-up once there's agreement on the right semantics for it.Note: the async message driver/verifier keeps its existing
Reify()naming untouched - only the new synchronous-message members useGenerateContents(), since that's less reliant on Pact-specific "reify" terminology for callers who don't need to know about it.This PR depends on
pactffi_sync_message_generate_contents, proposed in pact-reference#555 and not yet in any pact_ffi release. Until that lands:.Verify/.VerifyAsyncworks today, since it only relies on FFI functions that already exist in the currently pinned pact_ffi version.ConfiguredSyncMessageVerifier.Verify/VerifyAsyncwill throwEntryPointNotFoundExceptionat runtime, sincepactffi_sync_message_generate_contentsisn't in the native library yet.I've marked the one FFI-level integration test (
FfiIntegrationTests.SyncMessageInteraction_v4_CreatesPactFile) asSkipfor the same reason, to keep CI green. Opening this as a draft to get early feedback on the API shape while the pact-reference PR is reviewed; happy to update the pinned pact_ffi version and un-skip that test once a release is available.Test plan
dotnet build(all projects)dotnet test tests/PactNet.Tests- 166 passed, 1 skipped (the FFI-dependent integration test noted above)SyncMessageBuilderTests,SyncMessagePactBuilderTests,ConfiguredSyncMessageVerifierTestsmirroring the existing async message test coveragedocs/messaging-pacts.md