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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq.git


The following commit(s) were added to refs/heads/main by this push:
     new 18850b79d1 fix(test): update DurableSyncNetworkBridgeTest (#1755)
18850b79d1 is described below

commit 18850b79d1478e4ae8321ee4ed78017566eb5ae8
Author: Jean-Louis Monteiro <[email protected]>
AuthorDate: Wed Mar 11 17:51:05 2026 +0100

    fix(test): update DurableSyncNetworkBridgeTest (#1755)
    
    * fix(test): update DurableSyncNetworkBridgeTest to allow rebinding to the 
same port after restart
    
    * fix(test): enhance DurableSyncNetworkBridgeTest to handle port rebinding 
after broker restart
---
 .../network/DurableSyncNetworkBridgeTest.java      | 46 ++++++++++++++++++----
 1 file changed, 39 insertions(+), 7 deletions(-)

diff --git 
a/activemq-unit-tests/src/test/java/org/apache/activemq/network/DurableSyncNetworkBridgeTest.java
 
b/activemq-unit-tests/src/test/java/org/apache/activemq/network/DurableSyncNetworkBridgeTest.java
index 3a745424bd..a2d26c3bbc 100644
--- 
a/activemq-unit-tests/src/test/java/org/apache/activemq/network/DurableSyncNetworkBridgeTest.java
+++ 
b/activemq-unit-tests/src/test/java/org/apache/activemq/network/DurableSyncNetworkBridgeTest.java
@@ -51,6 +51,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -750,13 +751,42 @@ public class DurableSyncNetworkBridgeTest extends 
DynamicNetworkTestSupport {
     }
 
     protected void restartRemoteBroker() throws Exception {
-        int port = 0;
-        if (remoteBroker != null) {
-            List<TransportConnector> transportConnectors = 
remoteBroker.getTransportConnectors();
-            port = transportConnectors.get(0).getConnectUri().getPort();
-        }
+        final int previousPort = 
remoteBroker.getTransportConnectors().get(0).getConnectUri().getPort();
+        final File dataDir = remoteBroker.getDataDirectoryFile();
         stopRemoteBroker();
-        doSetUpRemoteBroker(false, remoteBroker.getDataDirectoryFile(), port);
+        try {
+            doSetUpRemoteBroker(false, dataDir, previousPort);
+        } catch (final IOException e) {
+            if (e.getCause() instanceof java.net.BindException) {
+                // Previous port still in TIME_WAIT — use a new ephemeral port
+                doSetUpRemoteBroker(false, dataDir, 0);
+                // Update the local broker's network connector to point to the 
new port
+                updateLocalNetworkConnectorUri();
+            } else {
+                throw e;
+            }
+        }
+    }
+
+    /**
+     * When the remote broker restarts on a new ephemeral port (BindException 
fallback),
+     * any existing network connector on the local broker still points to the 
old port.
+     * This method stops the old connector and replaces it with one targeting 
the new URI.
+     */
+    private void updateLocalNetworkConnectorUri() throws Exception {
+        if (localBroker == null) {
+            return;
+        }
+        final List<NetworkConnector> connectors = 
localBroker.getNetworkConnectors();
+        if (connectors.isEmpty()) {
+            return;
+        }
+        final NetworkConnector oldConnector = connectors.get(0);
+        oldConnector.stop();
+        localBroker.removeNetworkConnector(oldConnector);
+        final NetworkConnector newConnector = configureLocalNetworkConnector();
+        localBroker.addNetworkConnector(newConnector);
+        newConnector.start();
     }
 
     protected void doSetUpLocalBroker(boolean deleteAllMessages, boolean 
startNetworkConnector,
@@ -778,8 +808,10 @@ public class DurableSyncNetworkBridgeTest extends 
DynamicNetworkTestSupport {
             // because some tests restart localBroker before remoteBroker is 
running,
             // relying on the bridge connecting later when remoteBroker 
restarts.
             // Tests that need the bridge to be fully started call 
assertBridgeStarted() explicitly.
+            // Keep timeout short (5s) to avoid growing the NC reconnect 
backoff too much,
+            // which would delay bridge formation when the remote broker 
starts later.
             Wait.waitFor(() -> 
localBroker.getNetworkConnectors().get(0).activeBridges().size() == 1,
-                         TimeUnit.SECONDS.toMillis(15), 500);
+                         TimeUnit.SECONDS.toMillis(5), 500);
         }
         localSession = localConnection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to