Now accepting guest editorial from SAP, Oracle, Workday and ServiceNow practitioners.Pitch a guide
The Sign-Off Table

Lesson 5 of 5

Testing asynchronous event flows

10 min

Listen to this lesson

0:00 / 7:58

A synchronous test is easy to read: call, get an answer, compare. An asynchronous flow gives no answer. The test creates a sales order in CMQ and then has to find out, somewhere else and somewhat later, whether the right thing happened. Two techniques make that reliable: correlation, so you can follow one business change through every hop, and eventual assertions, so you wait for an outcome within a time limit instead of guessing.

  1. ArrangePick a unique business key: a new test order for Great Lakes Fleet Services with a known reference
  2. ActCreate or change the order in CMQ client 100
  3. CorrelateFind the event for that order: type, source, id and time in the test broker or consumer log
  4. Assert eventuallyPoll the fleet portal test API until the update appears, or fail after 120 seconds
  5. RecordKeep order number, event id, timestamps and the final state as evidence
The shape of an asynchronous end-to-end test.
The Sign-Off Table · thesignofftable.com/academy/sap-btp-303

Correlation

Every hop must log something that ties back to the same business change. The business key (the sales order number) is the anchor testers know. The CloudEvents id and source identify the event. SAP events can also carry SAP-specific tracing information; SAP's documentation shows a sappassport extension attribute used as a tracing identifier in some event packages. Contoso's consumer logs order number, event type, source, id and outcome on one line, so Priya can search the logs, or SAP Cloud ALM's monitoring, by order number and see the whole path.

Correlation across hops: the order number anchors the test, and the CloudEvents source and id tie each log line to the same business change.
The Sign-Off Table · thesignofftable.com/academy/sap-btp-303

Eventual assertions, not sleeps

The tempting test waits 30 seconds and then checks. It is slow when the system is fast and flaky when it is slow. Instead, poll for the expected outcome at short intervals until it appears or a timeout expires, and make the timeout match a stated expectation: Contoso's non-functional requirement says a fleet portal update appears within two minutes of the order change in production, so the test fails at 120 seconds. Assert the final state (the portal shows the new delivery date), not intermediate steps nobody promised.

An eventual assertion polls at short intervals and fails only when the stated two-minute expectation is missed, so it is neither slow nor flaky.
The Sign-Off Table · thesignofftable.com/academy/sap-btp-303
Unit testsHandler logic with sample payloads from the AsyncAPI specification: mapping, filtering, idempotency
Contract testsIncoming events and outgoing calls validated against the published schemas and API definitions
Component testsConsumer plus a test broker queue: duplicates, out-of-order, poison messages
End-to-end testsCMQ to test broker to cmc-ext-test to fleet portal test API, with correlation and eventual assertions
Operational testsConsumer down, S/4HANA down, broker backlog, replay from the dead message queue
Test levels for an event-driven extension. Most cases belong low in the stack, where they are fast.
The Sign-Off Table · thesignofftable.com/academy/sap-btp-303

The negative cases are the real test

Happy-path events will work in the first demo. The defects live in the cases lessons 3 and 4 described, and they can only be tested if the test team can produce them on demand. That means test hooks: a way to publish a crafted event to the test broker (duplicate id, old time, broken payload), and a way to stop and start the consumer in cmc-ext-test. Those hooks exist only in test stages and are documented as test tools.

Test caseHow to produce itExpected result
DuplicatePublish the same event twice with the same source and idOne portal update, one email; second event acknowledged and skipped
Out of orderPublish Changed, then Created for a new orderPortal shows current order data
Consumer outageStop the consumer 30 minutes; create 50 orders; restartAll 50 processed; queue drains; none lost
Poison messagePublish an event with a broken payloadLands in the dead message queue; alert raised; others continue
Dependency downBlock the S/4HANA destination in testRetries, then events kept in queue; processed when restored
Wrong stageCreate an order in CMQNo event reaches the production broker or production consumer

Contoso's asynchronous test catalog for the fleet order notifier.

What would you do?

An end-to-end test of the fleet notifier fails about one run in five with "portal not updated". The test waits a fixed 20 seconds after creating the order. What should the test engineer do first?

Key points
  • Correlate by business key plus event source and id; log them at every hop.
  • Use eventual assertions with polling and a timeout tied to a stated requirement; avoid fixed sleeps.
  • Test most cases at unit, contract and component level; keep end-to-end tests few and meaningful.
  • Build test hooks to produce duplicates, out-of-order events, outages and poison messages.
  • Sign off on negative cases and stage pairing, not on a happy path.

Check your understanding · not graded

Lesson check

  1. 1. Why are fixed sleeps a poor way to test asynchronous flows?

  2. 2. What should the timeout of an eventual assertion be based on?

  3. 3. Which identifiers let you follow one change through every hop?

  4. 4. Where should most idempotency and ordering cases be tested?