72 / 89 · 04 API & Contract Testing with AI · Asynchronous Testing Patterns← prev⊞ allnext →☰ Read as one page
11.5Pattern 4: Trace Correlation
Use a correlation ID to trace an event chain across multiple services.
import uuid
def test_order_flow_end_to_end(api_client, trace_store):
"""Trace an order through the entire event pipeline."""
correlation_id = str(uuid.uuid4())
# Initiate the flow
response = api_client.post("/api/orders", json={
**ORDER_PAYLOAD,
"correlation_id": correlation_id,
})
assert response.status_code == 201
# Wait for all services to process
time.sleep(15)
# Query the trace store for all events with this correlation ID
traces = trace_store.get_traces(correlation_id)
# Verify the expected event chain
event_types = [t["event_type"] for t in traces]
assert "order.created" in event_types
assert "payment.initiated" in event_types
assert "payment.completed" in event_types
assert "inventory.reserved" in event_types
assert "shipping.label_created" in event_types
assert "notification.sent" in event_types
# Verify ordering
order_idx = event_types.index("order.created")
payment_idx = event_types.index("payment.completed")
shipping_idx = event_types.index("shipping.label_created")
assert order_idx < payment_idx < shipping_idx, (
f"Events out of order: order@{order_idx}, "
f"payment@{payment_idx}, shipping@{shipping_idx}"
)
When to use: Complex flows spanning multiple services. Verifying the entire chain completed correctly.