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 3c0a25582170 CAMEL-21122: camel-test - release
AvailablePortFinder.Port after each test
3c0a25582170 is described below
commit 3c0a255821706e74cab1699987b0a68491f48d14
Author: Ravi <[email protected]>
AuthorDate: Tue May 26 10:49:17 2026 +0530
CAMEL-21122: camel-test - release AvailablePortFinder.Port after each test
AvailablePortFinder.Port leaked global port-mapping entries when used as a
non-static @RegisterExtension field (PER_METHOD lifecycle). The Port
extension
now implements BeforeAllCallback + AfterEachCallback to properly release
ports
after each test method, preventing port exhaustion in test suites.
Also removes the BaseJettyTest workaround that manually managed port
cleanup.
Changes mirrored in both camel-test-junit5 and camel-test-junit6.
Closes #23491
---
.../camel/component/jetty/BaseJettyTest.java | 8 --
components/camel-test/camel-test-junit5/pom.xml | 5 +
.../org/apache/camel/test/AvailablePortFinder.java | 26 +++-
.../test/AvailablePortFinderLifecycleTest.java | 134 +++++++++++++++++++++
.../org/apache/camel/test/AvailablePortFinder.java | 26 +++-
.../test/AvailablePortFinderLifecycleTest.java | 134 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_21.adoc | 10 ++
7 files changed, 333 insertions(+), 10 deletions(-)
diff --git
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
index 15aa087932dc..a8ab97862c43 100644
---
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
+++
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
@@ -23,7 +23,6 @@ import org.apache.camel.CamelContext;
import org.apache.camel.http.common.HttpHeaderFilterStrategy;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.junit6.CamelTestSupport;
-import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;
public abstract class BaseJettyTest extends CamelTestSupport {
@@ -36,13 +35,6 @@ public abstract class BaseJettyTest extends CamelTestSupport
{
@RegisterExtension
protected AvailablePortFinder.Port port2 = AvailablePortFinder.find();
- // Due to CAMEL-21122 ports are never released. So, force them to be
released.
- @AfterEach
- void cleanupPorts() {
- port1.release();
- port2.release();
- }
-
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
diff --git a/components/camel-test/camel-test-junit5/pom.xml
b/components/camel-test/camel-test-junit5/pom.xml
index e29c218377d3..6c6ef24e2f75 100644
--- a/components/camel-test/camel-test-junit5/pom.xml
+++ b/components/camel-test/camel-test-junit5/pom.xml
@@ -163,6 +163,11 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
diff --git
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
index aba383889c04..a33f4ba03bd6 100644
---
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
+++
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/AvailablePortFinder.java
@@ -22,6 +22,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
@@ -36,10 +38,12 @@ public final class AvailablePortFinder {
private static final AvailablePortFinder INSTANCE = new
AvailablePortFinder();
- public class Port implements BeforeEachCallback, AfterAllCallback,
AutoCloseable {
+ public class Port
+ implements BeforeAllCallback, BeforeEachCallback,
AfterEachCallback, AfterAllCallback, AutoCloseable {
final int port;
String testClass;
final Throwable creation;
+ private volatile boolean classScoped;
public Port(int port) {
this.port = port;
@@ -59,12 +63,28 @@ public final class AvailablePortFinder {
return Integer.toString(port);
}
+ @Override
+ public void beforeAll(ExtensionContext context) throws Exception {
+ // beforeAll only fires when @RegisterExtension is on a static
field,
+ // so use it as the signal that this Port lives for the whole test
class.
+ classScoped = true;
+ }
+
@Override
public void beforeEach(ExtensionContext context) throws Exception {
testClass =
context.getTestClass().map(Class::getName).orElse(null);
LOG.info("Registering port {} for test {}", port, testClass);
}
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ if (!classScoped) {
+ // Non-static @RegisterExtension: afterAll is never invoked by
JUnit Jupiter,
+ // so release here to keep the port mapping bounded.
+ release();
+ }
+ }
+
@Override
public void afterAll(ExtensionContext context) throws Exception {
release();
@@ -120,6 +140,10 @@ public final class AvailablePortFinder {
INSTANCE.portMapping.remove(port.getPort(), port);
}
+ static boolean isRegistered(Port port) {
+ return INSTANCE.portMapping.get(port.getPort()) == port;
+ }
+
/**
* Gets the next available port.
*
diff --git
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
new file mode 100644
index 000000000000..fb936508fdfc
--- /dev/null
+++
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.platform.engine.discovery.DiscoverySelectors;
+import org.junit.platform.launcher.LauncherDiscoveryRequest;
+import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
+import org.junit.platform.launcher.core.LauncherFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * Drives nested test classes through the JUnit Platform launcher to verify
that {@link AvailablePortFinder.Port} reacts
+ * correctly to all three combinations of {@code @RegisterExtension} placement
and {@code @TestInstance} lifecycle. The
+ * pre-existing leak (CAMEL-21122) only manifested under the real JUnit
lifecycle, so this test exercises it directly
+ * instead of calling callbacks by hand.
+ */
+public class AvailablePortFinderLifecycleTest {
+
+ static final List<AvailablePortFinder.Port> SEEN = new ArrayList<>();
+
+ public static class PerMethodNonStaticContainer {
+ @RegisterExtension
+ AvailablePortFinder.Port port = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(port);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(port);
+ }
+ }
+
+ @TestInstance(TestInstance.Lifecycle.PER_CLASS)
+ public static class PerClassNonStaticContainer {
+ @RegisterExtension
+ AvailablePortFinder.Port port = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(port);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(port);
+ }
+ }
+
+ public static class StaticContainer {
+ @RegisterExtension
+ static AvailablePortFinder.Port PORT = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(PORT);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(PORT);
+ }
+ }
+
+ @Test
+ public void perMethodNonStaticPortReleasesAfterEach() {
+ SEEN.clear();
+ run(PerMethodNonStaticContainer.class);
+
+ // Default lifecycle: JUnit constructs a fresh test instance per
method,
+ // so each method sees its own Port. Both must be released after their
test.
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertNotSame(SEEN.get(0), SEEN.get(1), "PER_METHOD lifecycle must
allocate a new Port per method");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "first
method's port must be released");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(1)), "second
method's port must be released");
+ }
+
+ @Test
+ public void perClassNonStaticPortSurvivesAndReleasesOnAfterAll() {
+ SEEN.clear();
+ run(PerClassNonStaticContainer.class);
+
+ // PER_CLASS reuses the single test instance, so both methods share
the same Port.
+ // JUnit also delivers BeforeAllCallback/AfterAllCallback to instance
extensions
+ // under PER_CLASS, so classScoped is set, afterEach is a no-op, and
afterAll
+ // releases the port exactly once.
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertSame(SEEN.get(0), SEEN.get(1), "PER_CLASS lifecycle must share a
single Port across methods");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "shared
port must be released on afterAll");
+ }
+
+ @Test
+ public void staticPortSurvivesAndReleasesOnAfterAll() {
+ SEEN.clear();
+ run(StaticContainer.class);
+
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertSame(SEEN.get(0), SEEN.get(1), "static @RegisterExtension must
share a single Port across methods");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "static
port must be released on afterAll");
+ }
+
+ private static void run(Class<?> testClass) {
+ LauncherDiscoveryRequest request =
LauncherDiscoveryRequestBuilder.request()
+ .selectors(DiscoverySelectors.selectClass(testClass))
+ .build();
+ LauncherFactory.create().execute(request);
+ }
+}
diff --git
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
index aba383889c04..a33f4ba03bd6 100644
---
a/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
+++
b/components/camel-test/camel-test-junit6/src/main/java/org/apache/camel/test/AvailablePortFinder.java
@@ -22,6 +22,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
@@ -36,10 +38,12 @@ public final class AvailablePortFinder {
private static final AvailablePortFinder INSTANCE = new
AvailablePortFinder();
- public class Port implements BeforeEachCallback, AfterAllCallback,
AutoCloseable {
+ public class Port
+ implements BeforeAllCallback, BeforeEachCallback,
AfterEachCallback, AfterAllCallback, AutoCloseable {
final int port;
String testClass;
final Throwable creation;
+ private volatile boolean classScoped;
public Port(int port) {
this.port = port;
@@ -59,12 +63,28 @@ public final class AvailablePortFinder {
return Integer.toString(port);
}
+ @Override
+ public void beforeAll(ExtensionContext context) throws Exception {
+ // beforeAll only fires when @RegisterExtension is on a static
field,
+ // so use it as the signal that this Port lives for the whole test
class.
+ classScoped = true;
+ }
+
@Override
public void beforeEach(ExtensionContext context) throws Exception {
testClass =
context.getTestClass().map(Class::getName).orElse(null);
LOG.info("Registering port {} for test {}", port, testClass);
}
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ if (!classScoped) {
+ // Non-static @RegisterExtension: afterAll is never invoked by
JUnit Jupiter,
+ // so release here to keep the port mapping bounded.
+ release();
+ }
+ }
+
@Override
public void afterAll(ExtensionContext context) throws Exception {
release();
@@ -120,6 +140,10 @@ public final class AvailablePortFinder {
INSTANCE.portMapping.remove(port.getPort(), port);
}
+ static boolean isRegistered(Port port) {
+ return INSTANCE.portMapping.get(port.getPort()) == port;
+ }
+
/**
* Gets the next available port.
*
diff --git
a/components/camel-test/camel-test-junit6/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
b/components/camel-test/camel-test-junit6/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
new file mode 100644
index 000000000000..fb936508fdfc
--- /dev/null
+++
b/components/camel-test/camel-test-junit6/src/test/java/org/apache/camel/test/AvailablePortFinderLifecycleTest.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.platform.engine.discovery.DiscoverySelectors;
+import org.junit.platform.launcher.LauncherDiscoveryRequest;
+import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
+import org.junit.platform.launcher.core.LauncherFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * Drives nested test classes through the JUnit Platform launcher to verify
that {@link AvailablePortFinder.Port} reacts
+ * correctly to all three combinations of {@code @RegisterExtension} placement
and {@code @TestInstance} lifecycle. The
+ * pre-existing leak (CAMEL-21122) only manifested under the real JUnit
lifecycle, so this test exercises it directly
+ * instead of calling callbacks by hand.
+ */
+public class AvailablePortFinderLifecycleTest {
+
+ static final List<AvailablePortFinder.Port> SEEN = new ArrayList<>();
+
+ public static class PerMethodNonStaticContainer {
+ @RegisterExtension
+ AvailablePortFinder.Port port = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(port);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(port);
+ }
+ }
+
+ @TestInstance(TestInstance.Lifecycle.PER_CLASS)
+ public static class PerClassNonStaticContainer {
+ @RegisterExtension
+ AvailablePortFinder.Port port = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(port);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(port);
+ }
+ }
+
+ public static class StaticContainer {
+ @RegisterExtension
+ static AvailablePortFinder.Port PORT = AvailablePortFinder.find();
+
+ @Test
+ void t1() {
+ SEEN.add(PORT);
+ }
+
+ @Test
+ void t2() {
+ SEEN.add(PORT);
+ }
+ }
+
+ @Test
+ public void perMethodNonStaticPortReleasesAfterEach() {
+ SEEN.clear();
+ run(PerMethodNonStaticContainer.class);
+
+ // Default lifecycle: JUnit constructs a fresh test instance per
method,
+ // so each method sees its own Port. Both must be released after their
test.
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertNotSame(SEEN.get(0), SEEN.get(1), "PER_METHOD lifecycle must
allocate a new Port per method");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "first
method's port must be released");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(1)), "second
method's port must be released");
+ }
+
+ @Test
+ public void perClassNonStaticPortSurvivesAndReleasesOnAfterAll() {
+ SEEN.clear();
+ run(PerClassNonStaticContainer.class);
+
+ // PER_CLASS reuses the single test instance, so both methods share
the same Port.
+ // JUnit also delivers BeforeAllCallback/AfterAllCallback to instance
extensions
+ // under PER_CLASS, so classScoped is set, afterEach is a no-op, and
afterAll
+ // releases the port exactly once.
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertSame(SEEN.get(0), SEEN.get(1), "PER_CLASS lifecycle must share a
single Port across methods");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "shared
port must be released on afterAll");
+ }
+
+ @Test
+ public void staticPortSurvivesAndReleasesOnAfterAll() {
+ SEEN.clear();
+ run(StaticContainer.class);
+
+ assertEquals(2, SEEN.size(), "two test methods should have observed a
Port each");
+ assertSame(SEEN.get(0), SEEN.get(1), "static @RegisterExtension must
share a single Port across methods");
+ assertFalse(AvailablePortFinder.isRegistered(SEEN.get(0)), "static
port must be released on afterAll");
+ }
+
+ private static void run(Class<?> testClass) {
+ LauncherDiscoveryRequest request =
LauncherDiscoveryRequestBuilder.request()
+ .selectors(DiscoverySelectors.selectClass(testClass))
+ .build();
+ LauncherFactory.create().execute(request);
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_21.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_21.adoc
index da3bd2aae50e..0d9af829173c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_21.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_21.adoc
@@ -1417,6 +1417,16 @@ for the embedded HTTP server:
Both default to unset. When both are unset, JWT validation behaviour is
unchanged (signature plus the
default `exp` / `nbf` checks).
+=== camel-test
+
+`org.apache.camel.test.AvailablePortFinder.Port` now also implements
+`AfterEachCallback` and `BeforeAllCallback`. When the `Port` is registered as a
+non-static `@RegisterExtension` field, it is automatically released after each
+test method, instead of leaking until JVM exit. The previous behaviour for
+static `@RegisterExtension` fields (port reserved for the whole class, released
+on `afterAll`) is preserved. Tests that manually called `port.release()` from
an
+`@AfterEach` method to work around CAMEL-21122 no longer need that workaround.
+
=== camel-mongodb-gridfs
The Exchange header values exposed by `GridFsConstants` have been renamed to
follow the standard