Skip to content

CAMEL-19549: camel-core: replace Thread.sleep in tests#24270

Open
tmielke wants to merge 1 commit into
apache:mainfrom
tmielke:CAMEL-19549-2
Open

CAMEL-19549: camel-core: replace Thread.sleep in tests#24270
tmielke wants to merge 1 commit into
apache:mainfrom
tmielke:CAMEL-19549-2

Conversation

@tmielke

@tmielke tmielke commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Additional improvement to unit test {{DirectProducerBlockingTest}}. Introducing better coordination between producer blocking and route resumption, eliminating race conditions on slow hardware.

Made with help from AI tools.

Additional improvement to unit test DirectProducerBlockingTest.
Introducing better coordination between producer blocking and
route resumption, eliminating race conditions on slow hardware.

Made with help from AI tools.
@github-actions

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 added the core label Jun 26, 2026

@davsclaus davsclaus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice follow-up — the prior CountDownLatch approach had a real race condition (countDown() fired before sendBody(), so the route could be resumed before the producer ever started blocking). Polling for TIMED_WAITING ensures the producer is truly blocked inside DirectComponent.getConsumer() before resuming.

Verified that Condition.await(rem, TimeUnit.MILLISECONDS) at DirectComponent.java:144 puts the thread in TIMED_WAITING, so the state check is correct.

Other good changes:

  • Error logging instead of silent // ignore
  • Mock expectation moved before action
  • Timeout increase for slow CI

Minor note: the test is now coupled to the implementation detail that DirectComponent uses Condition.await() (producing TIMED_WAITING). If that blocking mechanism ever changes, this test would need updating. Reasonable tradeoff for a component-specific test.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

@orpiske orpiske left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice follow-up — the CountDownLatch approach on main has a real race (the latch fires before sendBody() is called), and this PR fixes it with a practical Awaitility-based solution. The mock-setup reordering, error logging, and timeout bump are all welcome improvements.

I left a couple of minor observations inline — nothing blocking.

Claude Code on behalf of Otavio Rodolfo Piske. This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

await().atMost(2, TimeUnit.SECONDS)
.pollInterval(10, TimeUnit.MILLISECONDS)
.until(() -> mainThread.getState() == Thread.State.TIMED_WAITING);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor observation: TIMED_WAITING is a reliable proxy here (the producer blocks on consumersCondition.await() in DirectComponent.getConsumer), but it's a heuristic — in theory the main thread could transiently enter TIMED_WAITING earlier in the call path before reaching getConsumer(). With the 10ms poll interval this is extremely unlikely in practice, and it's strictly better than the prior CountDownLatch race.

Just something to keep in mind for future maintainers if this test ever becomes flaky again — the Thread.State check is a practical workaround, not a contract.

@github-actions

Copy link
Copy Markdown
Contributor

🧪 CI tested the following changed modules:

  • core/camel-core

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

  • core/camel-core: 2 test(s) disabled on GitHub Actions

⚙️ View full build and test results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants