gnodet-bot commented on code in PR #30787:
URL: https://github.com/apache/camel/pull/30787#discussion_r4695376020
##########
components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerPushConsumerDeletedTest.java:
##########
@@ -251,6 +253,47 @@ void
removesItsConnectionListenerWhenTheRouteStopsWhileItSubscribes() throws Exc
() -> assertEquals(List.of(), server.listeners, "no keeper
listens to the connection after the stop"));
}
+ @Test
+ void aDisconnectWaitsUntilTheConsumerListensToTheConnection() throws
Exception {
+ // the consumer subscribes on its own executor and listens to the
connection once the subscribe returned: hold
+ // that back, to force what a busy CI machine does (a disconnect while
the consumer is still subscribing)
+ CountDownLatch subscribed = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ // the route of the test may still be subscribing: wait for it, so
that the hook below applies to the route
+ // that replaces it
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
!server.listeners.isEmpty());
+ server.beforeAddListener = () -> {
+ server.beforeAddListener = null;
+ subscribed.countDown();
+ try {
+ release.await(30, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ };
+ consumeWith("exceptionHandler=#handler");
+ assertTrue(subscribed.await(30, TimeUnit.SECONDS), "the consumer
subscribed");
+
+ Thread disconnecting = new Thread(server::disconnect, "disconnecting");
+ disconnecting.start();
+ // the disconnect must wait for the listener, else it deletes the
consumer that is being subscribed and the
+ // reconnect reaches nobody
+ await().atMost(10, TimeUnit.SECONDS)
+ .until(() -> disconnecting.getState() ==
Thread.State.TIMED_WAITING);
Review Comment:
⚠️ **Low severity — test robustness**: Polling `disconnecting.getState() ==
Thread.State.TIMED_WAITING` works in practice because Awaitility uses
`Thread.sleep()` between condition checks (putting the thread in
`TIMED_WAITING`), and the 10-second `atMost` window makes it overwhelmingly
likely the check catches the thread mid-sleep. However, it's not strictly
deterministic: the thread could be momentarily `RUNNABLE` while evaluating the
predicate between sleeps, causing the outer `await()` to never see
`TIMED_WAITING` and time out with a `ConditionTimeoutException`.
A fully deterministic alternative: add a `CountDownLatch waitingForListener`
to `FakeServer`, count it down at the start of `disconnect()` before entering
the Awaitility loop, and have the test wait on that latch instead of polling
thread state. This eliminates the state-inspection race entirely.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]