Replace Thread.sleep with Awaitility in camel-kafka tests#24357
Conversation
Replace Thread.sleep() calls with Awaitility-based polling in three camel-kafka test files to improve test reliability and reduce flakiness: - KafkaProducerSaslAuthTypeIT: Replace sleep-then-poll with await().untilAsserted() for message consumption verification - KafkaTransactionIT: Replace manual polling loop with sleep to await().pollInterval().untilAsserted() - KafkaBatchingIntervalResetAfterIdleIT: Replace Thread.sleep for idle period simulation with await().pollDelay().untilAsserted() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
All tested modules (9 modules)
|
There was a problem hiding this comment.
Pull request overview
This PR updates Camel Kafka integration tests to eliminate Thread.sleep() usage and replace it with Awaitility-based polling, improving reliability and reducing flakiness in asynchronous test scenarios.
Changes:
- Replaced manual sleep/poll loops with Awaitility
await().untilAsserted(...)in Kafka consumer verification logic. - Updated SASL auth producer tests to wait for expected records via polling rather than fixed delays.
- Reworked “idle period” simulation in batching regression test using Awaitility delay + assertion.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/KafkaTransactionIT.java | Replaces a manual poll loop and Thread.sleep(100) with Awaitility polling until the latch reaches zero. |
| components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/KafkaProducerSaslAuthTypeIT.java | Removes fixed sleeps and uses Awaitility to poll Kafka until expected messages are received. |
| components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/batching/KafkaBatchingIntervalResetAfterIdleIT.java | Replaces an “idle” Thread.sleep(...) with an Awaitility-based delayed assertion to keep the test deterministic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| for (int i = 0; i < received.size(); i++) { | ||
| assertNotNull(received.get(i).value()); | ||
| assertEquals("test-message-" + i, received.get(i).value()); | ||
| assertEquals("key-" + i, received.get(i).key()); | ||
| } |
There was a problem hiding this comment.
Claude Code on behalf of Guillaume Nodet
Good catch — fixed in 0fa883b. Records are now sorted by key before asserting, making the check order-independent across partitions.
| await().pollDelay(BATCHING_INTERVAL_MS + 600, TimeUnit.MILLISECONDS) | ||
| .atMost(5, TimeUnit.SECONDS) | ||
| .untilAsserted(() -> assertEquals(0, to.getReceivedCounter(), | ||
| "No new messages should arrive during idle period")); |
There was a problem hiding this comment.
Claude Code on behalf of Guillaume Nodet
Good point — fixed in 0fa883b. The atMost is now derived from BATCHING_INTERVAL_MS (idleDelayMs + 4000) so both stay coupled if the constant changes.
…eouts - Sort received records by key before asserting to avoid relying on cross-partition delivery order (KafkaProducerSaslAuthTypeIT) - Derive atMost timeout from BATCHING_INTERVAL_MS to prevent deterministic timeout if the constant changes (KafkaBatchingIntervalResetAfterIdleIT) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Claude Code on behalf of Guillaume Nodet
Replace
Thread.sleep()calls with Awaitility-based polling in camel-kafka test files to improve test reliability and reduce flakiness (SonarCloud rule S2925):Thread.sleep(2000)before message consumption withawait().atMost(20, SECONDS).untilAsserted()that polls until all expected messages arriveThread.sleep(100)withawait().pollInterval(100, MILLISECONDS).untilAsserted()for message latch countdownThread.sleep(BATCHING_INTERVAL_MS + 600)for idle period simulation withawait().pollDelay().untilAsserted()All
Thread.sleep()calls incomponents/camel-kafka/src/test/have been eliminated.Test plan
mvn clean install -Dquickly)mvn formatter:format impsort:sort- no changes needed)🤖 Generated with Claude Code