Skip to content

Replace Thread.sleep with Awaitility in camel-kafka tests#24357

Merged
gnodet merged 2 commits into
apache:mainfrom
gnodet:sonar/s2925-thread-sleep-kafka
Jul 2, 2026
Merged

Replace Thread.sleep with Awaitility in camel-kafka tests#24357
gnodet merged 2 commits into
apache:mainfrom
gnodet:sonar/s2925-thread-sleep-kafka

Conversation

@gnodet

@gnodet gnodet commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

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):

  • KafkaProducerSaslAuthTypeIT: Replace Thread.sleep(2000) before message consumption with await().atMost(20, SECONDS).untilAsserted() that polls until all expected messages arrive
  • KafkaTransactionIT: Replace manual polling loop with Thread.sleep(100) with await().pollInterval(100, MILLISECONDS).untilAsserted() for message latch countdown
  • KafkaBatchingIntervalResetAfterIdleIT: Replace Thread.sleep(BATCHING_INTERVAL_MS + 600) for idle period simulation with await().pollDelay().untilAsserted()

All Thread.sleep() calls in components/camel-kafka/src/test/ have been eliminated.

Test plan

  • Code compiles (mvn clean install -Dquickly)
  • Code formatting verified (mvn formatter:format impsort:sort - no changes needed)
  • CI integration tests pass

🤖 Generated with Claude Code

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>
@gnodet gnodet requested review from davsclaus and oscerd July 1, 2026 09:46
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🧪 CI tested the following changed modules:

  • components/camel-kafka

⚠️ Some tests are disabled on GitHub Actions (@DisabledIfSystemProperty(named = "ci.env.name")) and require manual verification:

  • components/camel-kafka: 2 test(s) disabled on GitHub Actions
All tested modules (9 modules)
  • Camel :: JBang :: MCP
  • Camel :: JBang :: Plugin :: MCP
  • Camel :: JBang :: Plugin :: Route Parser
  • Camel :: JBang :: Plugin :: TUI
  • Camel :: JBang :: Plugin :: Validate
  • Camel :: Kafka
  • Camel :: Launcher :: Container
  • Camel :: YAML DSL :: Validator
  • Camel :: YAML DSL :: Validator Maven Plugin

⚙️ View full build and test results

@gnodet gnodet marked this pull request as ready for review July 1, 2026 18:30
@gnodet gnodet requested a review from Copilot July 1, 2026 18:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +180 to 185

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());
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +128 to +131
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"));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@gnodet gnodet merged commit 5dfdbe9 into apache:main Jul 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants