This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new d9e70aba6289 CAMEL-27240: Fix flaky KubernetesInformerLoopTest (#30432)
d9e70aba6289 is described below
commit d9e70aba6289135123476abe93c90066a8caf6d9
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 23 15:13:18 2026 +0100
CAMEL-27240: Fix flaky KubernetesInformerLoopTest (#30432)
This makes the teardown of KubernetesInformerLoopTest robust and adds
diagnostic information to its failure messages. It does not yet fix the root
cause of the "watch 2 was not opened" failure; that needs a failing run with
the new diagnostic output.
WatchControlDispatcher: awaitOpenWatches() and awaitClosedWatches() use
Lock+Condition monotonic counters and a deadline loop instead of a Semaphore,
and the failure message reports how many watches were opened and how many watch
requests were made. A set of the open sockets counts each close once, whichever
of onClosing, onFailure or onClosed fires, and activeWatchCount() exposes its
size.
Teardown: the loop is stopped and its thread joined, then the test waits
with Awaitility until every opened watch has closed, before client.close(), so
it verifies that the loop closed its watches. client.close() and
server.destroy() run in a finally block, and an IllegalStateException from the
Vert.x httpServer.close() timeout is logged at WARN.
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
.../kubernetes/KubernetesInformerLoopTest.java | 35 +++++-
.../kubernetes/WatchControlDispatcher.java | 136 ++++++++++++++++++---
2 files changed, 152 insertions(+), 19 deletions(-)
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesInformerLoopTest.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesInformerLoopTest.java
index 5b0ef35aafe8..77ed331c89fa 100644
---
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesInformerLoopTest.java
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesInformerLoopTest.java
@@ -32,6 +32,8 @@ import org.apache.camel.support.service.ServiceSupport;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -44,6 +46,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
class KubernetesInformerLoopTest {
+ private static final Logger LOG =
LoggerFactory.getLogger(KubernetesInformerLoopTest.class);
+
private WatchControlDispatcher dispatcher;
private KubernetesMockServer server;
private NamespacedKubernetesClient client;
@@ -66,12 +70,33 @@ class KubernetesInformerLoopTest {
@AfterEach
void tearDown() throws Exception {
service.stop();
- if (thread != null) {
- loop.stop();
- thread.join(20000);
+ // After loop.stop() and thread.join(), the informer has stopped, but
fabric8 cancels its
+ // HTTP streaming request asynchronously on a ForkJoin pool thread.
Wait for every opened
+ // watch to close before closing the client: this verifies that
loop.stop() actually closed
+ // its watches, not client.close(). Even after the WebSocket protocol
closes, the underlying
+ // Vert.x HTTP connection may still be draining; if httpServer.close()
times out in that
+ // narrow window, suppress the teardown exception since the test
already asserted.
+ try {
+ if (thread != null) {
+ // Stop the loop (stops the informer, releases the wait latch)
and join the thread.
+ loop.stop();
+ thread.join(20000);
+ assertFalse(thread.isAlive(), "Loop thread still alive after
20s — possible deadlock in stop()");
+ }
+ if (dispatcher.openWatchCount() > 0) {
+ await().atMost(Duration.ofSeconds(10))
+ .until(() -> dispatcher.activeWatchCount() == 0);
+ }
+ } finally {
+ client.close();
+ try {
+ server.destroy();
+ } catch (IllegalStateException e) {
+ // Vert.x httpServer.close() timed out: the TCP layer was
still draining.
+ // The test body has already asserted correctly.
+ LOG.warn("Mock server teardown timed out (expected async TCP
drain race): {}", e.getMessage());
+ }
}
- client.close();
- server.destroy();
}
@Test
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/WatchControlDispatcher.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/WatchControlDispatcher.java
index 141e17b7b949..2f3c4564139f 100644
---
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/WatchControlDispatcher.java
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/WatchControlDispatcher.java
@@ -17,12 +17,17 @@
package org.apache.camel.component.kubernetes;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Queue;
+import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.StatusBuilder;
@@ -41,6 +46,7 @@ import io.fabric8.mockwebserver.http.WebSocketListener;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
/**
* A Kubernetes mock server dispatcher in CRUD mode that lets a test fail the
next requests for a kind of resources,
@@ -55,8 +61,14 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
private final String resourcesPath;
private final Queue<Integer> failures = new ConcurrentLinkedQueue<>();
private final AtomicInteger watches = new AtomicInteger();
- private final Semaphore openWatches = new Semaphore(0);
- private final Semaphore closedWatches = new Semaphore(0);
+ private final Lock watchLock = new ReentrantLock();
+ private final Condition watchOpened = watchLock.newCondition();
+ private final Condition watchClosed = watchLock.newCondition();
+ // totalOpenWatches and totalClosedWatches count watches cumulatively,
guarded by watchLock
+ private int totalOpenWatches;
+ private int totalClosedWatches;
+ // openSockets tracks currently active sockets: added in onOpen, removed
in onClosed
+ private final Set<WebSocket> openSockets = new HashSet<>();
private final Semaphore listRequests = new Semaphore(0);
private final CompletableFuture<WebSocket> heldWatch = new
CompletableFuture<>();
private volatile boolean holdFirstWatch;
@@ -114,20 +126,78 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
}
/**
- * Waits until the given number of watches of the resources have been
opened in total.
+ * Waits until the given cumulative number of watches of the resources
have been opened in total.
*/
public void awaitOpenWatches(int count) throws InterruptedException {
- // the permits are released once per open watch
- assertTrue(openWatches.tryAcquire(count, 20, TimeUnit.SECONDS), "watch
" + count + " was not opened");
- openWatches.release(count);
+ long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(20);
+ watchLock.lock();
+ try {
+ while (totalOpenWatches < count) {
+ long remaining = deadline - System.nanoTime();
+ if (remaining <= 0) {
+ fail("watch " + count + " was not opened (only " +
totalOpenWatches + " opened, "
+ + watches.get() + " requests so far)");
+ }
+ watchOpened.await(remaining, TimeUnit.NANOSECONDS);
+ }
+ } finally {
+ watchLock.unlock();
+ }
+ }
+
+ /**
+ * The total number of watches opened so far (cumulative, including the
held one).
+ */
+ public int openWatchCount() {
+ watchLock.lock();
+ try {
+ return totalOpenWatches;
+ } finally {
+ watchLock.unlock();
+ }
}
/**
- * Waits until the given number of watches of the CRUD dispatcher (not the
held one) have been closed in total.
+ * The number of watches that are currently active (opened but not yet
closed).
+ */
+ public int activeWatchCount() {
+ watchLock.lock();
+ try {
+ return openSockets.size();
+ } finally {
+ watchLock.unlock();
+ }
+ }
+
+ /**
+ * The total number of watches closed so far (cumulative).
+ */
+ public int closedWatchCount() {
+ watchLock.lock();
+ try {
+ return totalClosedWatches;
+ } finally {
+ watchLock.unlock();
+ }
+ }
+
+ /**
+ * Waits until the given cumulative number of watches have been closed in
total.
*/
public void awaitClosedWatches(int count) throws InterruptedException {
- assertTrue(closedWatches.tryAcquire(count, 20, TimeUnit.SECONDS),
"watch " + count + " was not closed");
- closedWatches.release(count);
+ long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(20);
+ watchLock.lock();
+ try {
+ while (totalClosedWatches < count) {
+ long remaining = deadline - System.nanoTime();
+ if (remaining <= 0) {
+ fail("watch " + count + " was not closed (only " +
totalClosedWatches + " so far)");
+ }
+ watchClosed.await(remaining, TimeUnit.NANOSECONDS);
+ }
+ } finally {
+ watchLock.unlock();
+ }
}
/**
@@ -192,7 +262,22 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
@Override
public void onOpen(WebSocket webSocket, Response response) {
heldWatch.complete(webSocket);
- openWatches.release();
+ signalWatchOpened(webSocket);
+ }
+
+ @Override
+ public void onClosing(WebSocket webSocket, int code, String
reason) {
+ signalWatchClosed(webSocket);
+ }
+
+ @Override
+ public void onClosed(WebSocket webSocket, int code, String
reason) {
+ signalWatchClosed(webSocket);
+ }
+
+ @Override
+ public void onFailure(WebSocket webSocket, Throwable t,
Response response) {
+ signalWatchClosed(webSocket);
}
});
}
@@ -204,6 +289,29 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
return response;
}
+ private void signalWatchOpened(WebSocket webSocket) {
+ watchLock.lock();
+ try {
+ totalOpenWatches++;
+ openSockets.add(webSocket);
+ watchOpened.signalAll();
+ } finally {
+ watchLock.unlock();
+ }
+ }
+
+ private void signalWatchClosed(WebSocket webSocket) {
+ watchLock.lock();
+ try {
+ if (openSockets.remove(webSocket)) {
+ totalClosedWatches++;
+ watchClosed.signalAll();
+ }
+ } finally {
+ watchLock.unlock();
+ }
+ }
+
/**
* Tells when a watch of the CRUD dispatcher is open, so an update made
after that is sent to it.
*/
@@ -224,7 +332,7 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
public void onOpen(WebSocket webSocket, Response response) {
delegate.onOpen(webSocket, response);
lastOpenWatch = webSocket;
- openWatches.release();
+ signalWatchOpened(webSocket);
}
@Override
@@ -250,19 +358,19 @@ public class WatchControlDispatcher extends
KubernetesCrudDispatcher {
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
delegate.onClosing(webSocket, code, reason);
- closedWatches.release();
+ signalWatchClosed(webSocket);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
delegate.onClosed(webSocket, code, reason);
+ signalWatchClosed(webSocket);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response
response) {
delegate.onFailure(webSocket, t, response);
- // a watch the client drops without a close frame
- closedWatches.release();
+ signalWatchClosed(webSocket);
}
}
}