11. Integration and messaging: Integration QA¶
The API returned 201. Did the consumer actually reserve the stock?
In event-driven and batch systems the bugs live between the parts. The event is published with the wrong quantity. A consumer drops a message it can't read instead of dead-lettering it. A nightly job double-counts when it's retried. Unit tests pass and API tests pass. k11 Integration QA tests the whole chain in one flow: the API call, the Kafka message, the consumer's effect in the database, the dead-letter queue and the batch job.
Flows¶
Integration flows are API Functional QA flows (Chapter 9) with more step types. They live in tests/integration/*.yaml:
name: An order reserves stock through the inventory consumer
fixtures: [sku] # a fresh SKU in the test database, deleted afterwards
steps:
- listen: { broker: main, topic: orders.events, as: events } # subscribe BEFORE acting
- call: POST /orders
body: { sku: "${fixture.sku.sku}", qty: 3 }
expect: { status: 201 }
save: { orderId: "$.id" }
- name: OrderCreated is published and matches the AsyncAPI contract
expect_message:
from: events
match: { "$.type": OrderCreated, "$.orderId": "${orderId}" }
schema: OrderCreated
within: 10s
- name: the inventory consumer reserves all 3
expect_message: { from: events, match: { "$.type": StockReserved, "$.orderId": "${orderId}", "$.qty": 3 }, within: 10s }
- name: the reservation is in the database
wait_until:
db: SELECT reserved FROM inventory WHERE sku = :sku
params: { sku: "${fixture.sku.sku}" }
expect: { row: { reserved: 3 } }
within: 10s
| Step | What it does |
|---|---|
call, db |
as in API Functional QA, with the same fixtures, auth, variables and cleanup |
listen |
subscribe to a topic from now on, before the action that should produce the event |
publish |
send a message (JSON or text, key, headers): a command, or a poison message for the dead-letter queue |
expect_message |
a matching message arrives within the time limit; match on JSON paths, key, header:Name, value; count, save and an AsyncAPI schema |
expect_no_message |
nothing matching arrives for the given time (no OrderFailed, no duplicate) |
run_job |
run a batch job: a command (a list, never a shell) or an HTTP trigger with polling (until, fail_if) |
wait_until |
poll a SELECT or GET until the expectation holds: eventual consistency without sleeps |
Listen first. A listener only sees messages published after it subscribed, so start listening before the call that should produce the event. Listeners use no consumer group, so they never take messages from your real consumers or move their offsets.
The closest message¶
When no message matches, the report doesn't just say "timed out". It shows the closest message that did arrive and how it differed:
the inventory consumer reserves all 3: message on orders.events: 0 of 1 matching message(s) within 10s
(2 message(s) on orders.events); closest: $.qty: expected 3, got 2
"The event never came" and "the event came with the wrong quantity" are different bugs with different fixes; the evidence tells them apart.
Configure: .qa-mcp/integration.yaml¶
brokers:
main: { type: kafka, bootstrap: "${ENV:KAFKA_BOOTSTRAP}" } # extra keys go to kafka-python (security_protocol, sasl_*…)
jobs:
sku_totals:
command: [python, jobs/sku_totals.py] # a list: no shell
env: { SHOP_DB: shop.db }
timeout: 60s
reindex:
http: { call: POST /admin/jobs/reindex, save: { jobId: "$.id" } }
poll: { call: "GET /admin/jobs/${jobId}", until: { "$.status": done }, fail_if: { "$.status": failed } }
asyncapi: asyncapi.yaml
protected_topics: ["prod.*", "*.prod.*", "*production*"]
Environments, auth, the test database and fixtures come from api-test.yaml. Brokers are Apache Kafka (install
k11-integration-qa-mcp[kafka]) or an in-process broker for tests where the system under test runs in the same process.
k11-integration-qa-mcp init writes a starter config and an example flow.
Ask first¶
/integration-check starts with review_flow_plan (Chapter 4), which asks the questions integration tests most often get wrong:
| Rule | Asks |
|---|---|
time-limit |
"eventually" by when? A consumer effect needs a time limit |
dead-letter |
what happens to a bad message: how many retries, which dead-letter topic, what payload? |
duplicate |
what should a duplicate or redelivered message do? |
ordering |
several events for the same key: is order guaranteed, and does it matter? |
batch |
when does the job run, and how many rows should it produce? |
contract-mismatch |
a topic, event or field the AsyncAPI contract doesn't have |
Time limits are never lengthened to make a flow pass: a slow consumer is a finding.
Other tools: watch_topic listens read-only for a few seconds to learn a topic's message shape; validate_flow checks a flow's
structure without running it; get_report returns one flow's full evidence.
Safety¶
- Publishing and running jobs change data, so they follow API Functional QA's write rules: refused on production-like environments.
- Protected topics (production-named by default) are never published to, on any environment.
- Jobs run without a shell, inside the project, with a time limit.
- Every wait has a time limit; listeners are closed and cleanup always runs.
In CI¶
Run it against a real broker. In GitHub Actions, Kafka runs as a service container; the K11 Shop demo's workflow (Chapter 17) has a complete, working configuration, including creating topics before the service starts.
| Option | Default |
|---|---|
[paths] |
tests/integration/ |
--env NAME |
from api-test.yaml |
--tags a,b |
all flows |
--report-dir PATH |
.qa-mcp/integration-report |
--fail-on never |
exit 1 when a flow fails |
Evidence for the gate¶
integration-report.json is the integration kind.
| Rule | Outcome | When |
|---|---|---|
integration.failed |
block | a flow failed: event, consumer, dead-letter queue, job or database |
integration.flows_not_run |
needs a person | flows were refused (writes not allowed, protected topic) or errored |
integration.not_run |
needs a person | no integration flows ran |
Changes to consumers, producers, listeners, messaging, jobs, schedulers, workers, middleware and AsyncAPI documents require it.