This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch fix/flaky-undertow-jms
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 7800cee1323fbbd860bb24b5531f8083bfe78ca0
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Mar 11 17:41:18 2026 +0100

    Fix test cleanup in Undertow and race condition in JMS tests
    
    Undertow: Add @After cleanup to properly destroy server engines
    between tests, preventing cascading port-bind failures when a
    test creates an engine but doesn't clean up.
    
    JMS: Fix TOCTOU race condition in waitForReceiveInMessage() and
    waitForReceiveDestMessage() where the null check was outside the
    synchronized block. If a message arrived between the check and the
    wait(), the notification was lost, causing the test to wait the
    full timeout before failing.
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
 .../http_undertow/UndertowHTTPServerEngineTest.java   | 19 +++++++------------
 .../apache/cxf/transport/jms/AbstractJMSTester.java   | 18 +++++++-----------
 2 files changed, 14 insertions(+), 23 deletions(-)

diff --git 
a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
 
b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
index 9daa8f2bd7..0b0c07b8e3 100644
--- 
a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
+++ 
b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
@@ -42,6 +42,7 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.management.InstrumentationManager;
 import org.apache.cxf.testutil.common.TestUtil;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -77,7 +78,13 @@ public class UndertowHTTPServerEngineTest {
 
         factory = new UndertowHTTPServerEngineFactory();
         factory.setBus(bus);
+    }
 
+    @After
+    public void tearDown() throws Exception {
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT3);
     }
 
 
@@ -90,8 +97,6 @@ public class UndertowHTTPServerEngineTest {
         assertTrue(
             "Engine references for the same port should point to the same 
instance",
             engine == factory.retrieveUndertowHTTPServerEngine(PORT1));
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
     }
 
     @Test
@@ -129,10 +134,6 @@ public class UndertowHTTPServerEngineTest {
         engine = factory.createUndertowHTTPServerEngine(PORT3, "https");
         assertTrue("Protocol must be https",
                    "https".equals(engine.getProtocol()));
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT3);
     }
 
     @Test
@@ -186,9 +187,6 @@ public class UndertowHTTPServerEngineTest {
         engine.shutdown();
         response = getResponse(urlStr2);
         assertEquals("The undertow http handler did not take effect", 
response, "string2");
-        // set the get request
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-
     }
 
     /**
@@ -233,9 +231,6 @@ public class UndertowHTTPServerEngineTest {
         s = CastUtils.cast(ManagementFactory.getPlatformMBeanServer().
             queryNames(new 
ObjectName("org.xnio:type=Xnio,provider=\"nio\",worker=\"*\""), null));
         assertEquals("Could not find 0 Undertow Server: " + s, 0, s.size());
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
     }
 
     private static String getResponse(String target) throws Exception {
diff --git 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
index dec83281bc..d0f807fbae 100644
--- 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
+++ 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
@@ -298,27 +298,23 @@ public abstract class AbstractJMSTester {
 
 
     protected Message waitForReceiveInMessage() throws InterruptedException {
-        if (null == inMessage.get()) {
-            synchronized (inMessage) {
-                inMessage.wait(MAX_RECEIVE_TIME * 1000L);
-            }
+        synchronized (inMessage) {
             if (null == inMessage.get()) {
-                assertNotNull("Can't receive the Conduit Message in " + 
MAX_RECEIVE_TIME + " seconds", inMessage.get());
+                inMessage.wait(MAX_RECEIVE_TIME * 1000L);
             }
         }
+        assertNotNull("Can't receive the Conduit Message in " + 
MAX_RECEIVE_TIME + " seconds", inMessage.get());
         return inMessage.getAndSet(null);
     }
 
     protected Message waitForReceiveDestMessage() throws InterruptedException {
-        if (null == destMessage.get()) {
-            synchronized (destMessage) {
-                destMessage.wait(MAX_RECEIVE_TIME * 1000L);
-            }
+        synchronized (destMessage) {
             if (null == destMessage.get()) {
-                assertNotNull("Can't receive the Destination message in " + 
MAX_RECEIVE_TIME + " seconds",
-                    destMessage.get());
+                destMessage.wait(MAX_RECEIVE_TIME * 1000L);
             }
         }
+        assertNotNull("Can't receive the Destination message in " + 
MAX_RECEIVE_TIME + " seconds",
+            destMessage.get());
         return destMessage.getAndSet(null);
     }
 

Reply via email to