You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/07/01 09:45:57 UTC
[PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
gnodet opened a new pull request, #24357:
URL: https://github.com/apache/camel/pull/24357
## Summary
_Claude Code on behalf of Guillaume Nodet_
Replace `Thread.sleep()` calls with [Awaitility](https://github.com/awaitility/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
- [x] Code compiles (`mvn clean install -Dquickly`)
- [x] Code formatting verified (`mvn formatter:format impsort:sort` - no changes needed)
- [ ] CI integration tests pass
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #24357:
URL: https://github.com/apache/camel/pull/24357
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #24357:
URL: https://github.com/apache/camel/pull/24357#discussion_r3508399936
##########
components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/KafkaProducerSaslAuthTypeIT.java:
##########
@@ -153,42 +157,39 @@ public void configure() {
@Timeout(30)
@Order(1)
@Test
- public void kafkaProducerWithSaslAuthType() throws InterruptedException {
+ public void kafkaProducerWithSaslAuthType() {
ProducerTemplate producerTemplate = contextExtension.getProducerTemplate();
// Send messages using Camel producer with saslAuthType
for (int i = 0; i < 5; i++) {
producerTemplate.sendBodyAndHeader("direct:start", "test-message-" + i, KafkaConstants.KEY, "key-" + i);
}
- // Allow some time for messages to be sent
- Thread.sleep(2000);
-
// Consume messages with native Kafka consumer to verify they were sent correctly
- int messageCount = 0;
- int maxAttempts = 10;
- int attempt = 0;
-
- while (messageCount < 5 && attempt < maxAttempts) {
- ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
- for (ConsumerRecord<String, String> record : records) {
- LOG.info("Received message: key={}, value={}", record.key(), record.value());
- assertNotNull(record.value());
- assertEquals("test-message-" + messageCount, record.value());
- assertEquals("key-" + messageCount, record.key());
- messageCount++;
- }
- attempt++;
+ List<ConsumerRecord<String, String>> received = new ArrayList<>();
+
+ await().atMost(20, TimeUnit.SECONDS)
+ .untilAsserted(() -> {
+ ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
+ for (ConsumerRecord<String, String> record : records) {
+ LOG.info("Received message: key={}, value={}", record.key(), record.value());
+ received.add(record);
+ }
+ assertEquals(5, received.size(), "Should have received 5 messages");
+ });
+
+ 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());
}
Review 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.
##########
components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/batching/KafkaBatchingIntervalResetAfterIdleIT.java:
##########
@@ -125,7 +125,10 @@ public void intervalWatchShouldResetWhenNewAccumulationCycleBegins() throws Exce
// Idle for longer than batchingIntervalMs so that intervalWatch expires while the
// queue is empty. This is the precondition that triggers the bug on the next cycle.
- Thread.sleep(BATCHING_INTERVAL_MS + 600);
+ 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"));
Review 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24357:
URL: https://github.com/apache/camel/pull/24357#issuecomment-4852929043
:star2: Thank you for your contribution to the Apache Camel project! :star2:
:robot: CI automation will test this PR automatically.
:camel: 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](https://camel.apache.org/community/team/#committers) have access to the summary.
:warning: Be careful when sharing logs. Review their contents before sharing them publicly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #24357:
URL: https://github.com/apache/camel/pull/24357#discussion_r3508265863
##########
components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/KafkaProducerSaslAuthTypeIT.java:
##########
@@ -153,42 +157,39 @@ public void configure() {
@Timeout(30)
@Order(1)
@Test
- public void kafkaProducerWithSaslAuthType() throws InterruptedException {
+ public void kafkaProducerWithSaslAuthType() {
ProducerTemplate producerTemplate = contextExtension.getProducerTemplate();
// Send messages using Camel producer with saslAuthType
for (int i = 0; i < 5; i++) {
producerTemplate.sendBodyAndHeader("direct:start", "test-message-" + i, KafkaConstants.KEY, "key-" + i);
}
- // Allow some time for messages to be sent
- Thread.sleep(2000);
-
// Consume messages with native Kafka consumer to verify they were sent correctly
- int messageCount = 0;
- int maxAttempts = 10;
- int attempt = 0;
-
- while (messageCount < 5 && attempt < maxAttempts) {
- ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
- for (ConsumerRecord<String, String> record : records) {
- LOG.info("Received message: key={}, value={}", record.key(), record.value());
- assertNotNull(record.value());
- assertEquals("test-message-" + messageCount, record.value());
- assertEquals("key-" + messageCount, record.key());
- messageCount++;
- }
- attempt++;
+ List<ConsumerRecord<String, String>> received = new ArrayList<>();
+
+ await().atMost(20, TimeUnit.SECONDS)
+ .untilAsserted(() -> {
+ ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
+ for (ConsumerRecord<String, String> record : records) {
+ LOG.info("Received message: key={}, value={}", record.key(), record.value());
+ received.add(record);
+ }
+ assertEquals(5, received.size(), "Should have received 5 messages");
+ });
+
+ 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());
}
Review Comment:
The assertions assume the Kafka consumer delivers records in the same order they were produced (index-based checks). Record iteration order is not guaranteed (e.g., if multiple partitions are used), which can make this test flaky. Sorting (or validating by key) before asserting makes the check order-independent.
##########
components/camel-kafka/src/test/java/org/apache/camel/component/kafka/integration/batching/KafkaBatchingIntervalResetAfterIdleIT.java:
##########
@@ -125,7 +125,10 @@ public void intervalWatchShouldResetWhenNewAccumulationCycleBegins() throws Exce
// Idle for longer than batchingIntervalMs so that intervalWatch expires while the
// queue is empty. This is the precondition that triggers the bug on the next cycle.
- Thread.sleep(BATCHING_INTERVAL_MS + 600);
+ 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"));
Review Comment:
The Awaitility timeout is a fixed 5 seconds, while the poll delay is derived from BATCHING_INTERVAL_MS. If BATCHING_INTERVAL_MS is adjusted in the future, this can accidentally make pollDelay >= atMost and cause a deterministic timeout. Consider deriving the atMost from the same constant to keep them consistent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Replace Thread.sleep with Awaitility in camel-kafka tests [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24357:
URL: https://github.com/apache/camel/pull/24357#issuecomment-4853242369
<!-- ci-tested-modules -->
:test_tube: **CI tested the following changed modules:**
- `components/camel-kafka`
:warning: **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
<details><summary>All tested modules (9 modules)</summary>
- 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
</details>
---
:gear: [View full build and test results](https://github.com/apache/camel/actions/runs/28508629726)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org