Flaky Async Test Exposed a Real Race Condition in Order Event Handling

A test covering an order-save, event-publish, and confirmation flow failed roughly one in ten CI runs, and developers initially blamed slow CI machines and raised a sleep delay. The author found the real cause was an @Async @EventListener that started before the publishing transaction committed, so it sometimes queried an order not yet visible. Production showed occasional "order not found" warnings under load, matching the test's intermittent failures. The fix switched the listener to @TransactionalEventListener on AFTER_COMMIT and replaced the fixed sleep with Awaitility polling. Across 200 CI runs, the corrected version recorded no failures, compared with 22 failures for the original and 6 for the longer-sleep version.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in