This is an automated email from the ASF dual-hosted git repository.
jlmonteiro 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 0fa808332f test: use ephemeral ports (#1703)
0fa808332f is described below
commit 0fa808332f87728fc0a0a7f6afa878d59fe7f8a5
Author: Jean-Louis Monteiro <[email protected]>
AuthorDate: Fri Feb 20 17:04:22 2026 +0100
test: use ephemeral ports (#1703)
* Use ephemeral ports in tests to avoid port conflicts
* Refactor tests to remove unnecessary network connector starts and improve
connection handling
---
.../broker/ft/JDBCQueueMasterSlaveTest.java | 6 +--
.../broker/ft/QueueMasterSlaveTestSupport.java | 15 ++++++-
.../ft/kahaDbJdbcLeaseQueueMasterSlaveTest.java | 6 +--
.../broker/ft/mKahaDbQueueMasterSlaveTest.java | 6 +--
.../apache/activemq/bugs/AMQ4485LowLimitTest.java | 52 +++++++++++++---------
...kOfXBrokersWithNDestsFanoutTransactionTest.java | 45 ++++++++++++-------
.../failover/ConnectionHangOnStartupTest.java | 47 +++++++++++--------
...BrokerConnectionDuplexExcludedDestinations.java | 19 ++++----
...TopicSendReceiveUsingJavaConfigurationTest.java | 27 +++++++----
.../apache/activemq/broker/ft/sharedFileMaster.xml | 2 +-
.../apache/activemq/broker/ft/sharedFileSlave.xml | 2 +-
.../apache/activemq/usecases/receiver-duplex.xml | 2 +-
.../org/apache/activemq/usecases/sender-duplex.xml | 4 +-
13 files changed, 143 insertions(+), 90 deletions(-)
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/JDBCQueueMasterSlaveTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/JDBCQueueMasterSlaveTest.java
index 1d987bfeb4..a5b482f682 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/JDBCQueueMasterSlaveTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/JDBCQueueMasterSlaveTest.java
@@ -36,8 +36,6 @@ import org.apache.derby.jdbc.EmbeddedDataSource;
public class JDBCQueueMasterSlaveTest extends QueueMasterSlaveTestSupport {
protected SyncCreateDataSource sharedDs;
- protected String MASTER_URL = "tcp://localhost:62001";
- protected String SLAVE_URL = "tcp://localhost:62002";
protected String findStatement;
protected void setUp() throws Exception {
@@ -55,7 +53,7 @@ public class JDBCQueueMasterSlaveTest extends
QueueMasterSlaveTestSupport {
protected void createMaster() throws Exception {
master = new BrokerService();
master.setBrokerName("master");
- master.addConnector(MASTER_URL);
+ master.addConnector("tcp://localhost:0");
master.setUseJmx(false);
master.setPersistent(true);
master.setDeleteAllMessagesOnStartup(true);
@@ -83,7 +81,7 @@ public class JDBCQueueMasterSlaveTest extends
QueueMasterSlaveTestSupport {
BrokerService broker = new BrokerService();
broker.setBrokerName("slave");
TransportConnector connector = new TransportConnector();
- connector.setUri(new URI(SLAVE_URL));
+ connector.setUri(new URI("tcp://localhost:" + masterPort));
broker.addConnector(connector);
// no need for
broker.setMasterConnectorURI(masterConnectorURI)
// as the db lock provides the slave/master initialisation
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/QueueMasterSlaveTestSupport.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/QueueMasterSlaveTestSupport.java
index adc4a0f4e5..b1aae94f3c 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/QueueMasterSlaveTestSupport.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/QueueMasterSlaveTestSupport.java
@@ -48,10 +48,14 @@ abstract public class QueueMasterSlaveTestSupport extends
JmsTopicSendReceiveWit
protected CountDownLatch slaveStarted;
protected int inflightMessageCount;
protected int failureCount = 50;
- protected String uriString =
"failover://(tcp://localhost:62001,tcp://localhost:62002)?randomize=false&useExponentialBackOff=false";
+ protected int masterPort;
+ protected String uriString;
@Override
protected void setUp() throws Exception {
+ // Use ephemeral port for XML-based broker configs
+ System.setProperty("masterPort", "0");
+
slaveStarted = new CountDownLatch(1);
slave.set(null);
setMaxTestTime(TimeUnit.MINUTES.toMillis(10));
@@ -64,6 +68,15 @@ abstract public class QueueMasterSlaveTestSupport extends
JmsTopicSendReceiveWit
failureCount = super.messageCount / 2;
super.topic = isTopic();
createMaster();
+
+ // Get the actual port assigned by the OS after master starts
+ masterPort =
master.getTransportConnectors().get(0).getConnectUri().getPort();
+ uriString = "failover://(tcp://localhost:" + masterPort
+ + ")?randomize=false&useExponentialBackOff=false";
+
+ // Slave reuses the same port so failover reconnects to the same
address
+ System.setProperty("slavePort", String.valueOf(masterPort));
+
createSlave();
// wait for thing to connect
Thread.sleep(1000);
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/kahaDbJdbcLeaseQueueMasterSlaveTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/kahaDbJdbcLeaseQueueMasterSlaveTest.java
index 89f6f5ca5a..19359d8753 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/kahaDbJdbcLeaseQueueMasterSlaveTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/kahaDbJdbcLeaseQueueMasterSlaveTest.java
@@ -32,8 +32,6 @@ import org.apache.derby.jdbc.EmbeddedDataSource;
public class kahaDbJdbcLeaseQueueMasterSlaveTest extends
QueueMasterSlaveTestSupport {
protected DataSource sharedDs;
- protected String MASTER_URL = "tcp://localhost:62001";
- protected String SLAVE_URL = "tcp://localhost:62002";
File sharedDbDirFile;
@Override
@@ -54,7 +52,7 @@ public class kahaDbJdbcLeaseQueueMasterSlaveTest extends
QueueMasterSlaveTestSup
protected void createMaster() throws Exception {
master = new BrokerService();
master.setBrokerName("master");
- master.addConnector(MASTER_URL);
+ master.addConnector("tcp://localhost:0");
master.setUseJmx(false);
master.setPersistent(true);
master.setDeleteAllMessagesOnStartup(true);
@@ -87,7 +85,7 @@ public class kahaDbJdbcLeaseQueueMasterSlaveTest extends
QueueMasterSlaveTestSup
BrokerService broker = new BrokerService();
broker.setBrokerName("slave");
TransportConnector connector = new TransportConnector();
- connector.setUri(new URI(SLAVE_URL));
+ connector.setUri(new URI("tcp://localhost:" + masterPort));
broker.addConnector(connector);
broker.setUseJmx(false);
broker.setPersistent(true);
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/mKahaDbQueueMasterSlaveTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/mKahaDbQueueMasterSlaveTest.java
index ad9cca1ba0..917b6a3558 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/mKahaDbQueueMasterSlaveTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/ft/mKahaDbQueueMasterSlaveTest.java
@@ -26,13 +26,11 @@ import
org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
import org.apache.activemq.store.kahadb.MultiKahaDBPersistenceAdapter;
public class mKahaDbQueueMasterSlaveTest extends QueueMasterSlaveTestSupport {
- protected String MASTER_URL = "tcp://localhost:62001";
- protected String SLAVE_URL = "tcp://localhost:62002";
protected void createMaster() throws Exception {
master = new BrokerService();
master.setBrokerName("master");
- master.addConnector(MASTER_URL);
+ master.addConnector("tcp://localhost:0");
master.setUseJmx(false);
master.setPersistent(true);
master.setDeleteAllMessagesOnStartup(true);
@@ -59,7 +57,7 @@ public class mKahaDbQueueMasterSlaveTest extends
QueueMasterSlaveTestSupport {
BrokerService broker = new BrokerService();
broker.setBrokerName("slave");
TransportConnector connector = new TransportConnector();
- connector.setUri(new URI(SLAVE_URL));
+ connector.setUri(new URI("tcp://localhost:" + masterPort));
broker.addConnector(connector);
// no need for
broker.setMasterConnectorURI(masterConnectorURI)
// as the db lock provides the slave/master initialisation
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485LowLimitTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485LowLimitTest.java
index cc77fd3880..117c7c7dfa 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485LowLimitTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485LowLimitTest.java
@@ -63,22 +63,30 @@ import org.slf4j.LoggerFactory;
public class AMQ4485LowLimitTest extends JmsMultipleBrokersTestSupport {
static final String payload = new String(new byte[10 * 1024]);
private static final Logger LOG =
LoggerFactory.getLogger(AMQ4485LowLimitTest.class);
- final int portBase = 61600;
int numBrokers = 4;
+ final int[] brokerPorts = new int[numBrokers];
final int numProducers = 10;
final int numMessages = 200;
final int consumerSleepTime = 5;
- StringBuilder brokersUrl = new StringBuilder();
HashMap<ActiveMQQueue, AtomicInteger> accumulators = new
HashMap<ActiveMQQueue, AtomicInteger>();
private ArrayList<Throwable> exceptions = new ArrayList<Throwable>();
- protected void buildUrlList() throws Exception {
+ protected void collectBrokerPorts() throws Exception {
for (int i = 0; i < numBrokers; i++) {
- brokersUrl.append("tcp://localhost:" + (portBase + i));
+ final BrokerService broker = brokers.get("B" + i).broker;
+ brokerPorts[i] =
broker.getTransportConnectors().get(0).getConnectUri().getPort();
+ }
+ }
+
+ protected String buildBrokersUrl() {
+ final StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < numBrokers; i++) {
+ sb.append("tcp://localhost:").append(brokerPorts[i]);
if (i != numBrokers - 1) {
- brokersUrl.append(',');
+ sb.append(',');
}
}
+ return sb.toString();
}
protected BrokerService createBroker(int brokerid) throws Exception {
@@ -95,11 +103,8 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
broker.setUseJmx(true);
broker.setBrokerName("B" + brokerid);
- broker.addConnector(new URI("tcp://localhost:" + (portBase +
brokerid)));
+ broker.addConnector(new URI("tcp://localhost:0"));
- if (addToNetwork) {
- addNetworkConnector(broker);
- }
broker.setSchedulePeriodForDestinationPurge(0);
broker.getSystemUsage().getMemoryUsage().setLimit(256 * 1024 * 1024l);
@@ -132,12 +137,11 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
return broker;
}
- private void addNetworkConnector(BrokerService broker) throws Exception {
- StringBuilder networkConnectorUrl = new
StringBuilder("static:(").append(brokersUrl.toString());
- networkConnectorUrl.append(')');
+ private void addNetworkConnector(BrokerService broker, String brokersUrl)
throws Exception {
+ final String networkConnectorUrl = "static:(" + brokersUrl + ")";
for (int i = 0; i < 2; i++) {
- NetworkConnector nc = new DiscoveryNetworkConnector(new
URI(networkConnectorUrl.toString()));
+ final NetworkConnector nc = new DiscoveryNetworkConnector(new
URI(networkConnectorUrl));
nc.setName("Bridge-" + i);
nc.setNetworkTTL(1);
nc.setDecreaseNetworkConsumerPriority(true);
@@ -146,6 +150,7 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
nc.setDynamicallyIncludedDestinations(
Arrays.asList(new ActiveMQDestination[]{new
ActiveMQQueue("GW.*")}));
broker.addNetworkConnector(nc);
+ broker.startNetworkConnector(nc, null);
}
}
@@ -156,7 +161,8 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
BrokerService b = createBroker(0, false);
b.start();
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:" + (portBase + 0));
+ final int port =
b.getTransportConnectors().get(0).getConnectUri().getPort();
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:" + port);
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection c1 = connectionFactory.createQueueConnection();
@@ -207,13 +213,19 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
public void testBrokers() throws Exception {
- buildUrlList();
-
for (int i = 0; i < numBrokers; i++) {
createBroker(i);
}
startAllBrokers();
+
+ // Get actual ports after brokers start and add network connectors
+ collectBrokerPorts();
+ final String brokersUrl = buildBrokersUrl();
+ for (int i = 0; i < numBrokers; i++) {
+ addNetworkConnector(brokers.get("B" + i).broker, brokersUrl);
+ }
+
waitForBridgeFormation(numBrokers - 1);
verifyPeerBrokerInfos(numBrokers - 1);
@@ -266,7 +278,7 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
private void startConsumer(String brokerName, ActiveMQDestination
destination) throws Exception {
int id = Integer.parseInt(brokerName.substring(1));
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:" + (portBase + id));
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:" + brokerPorts[id]);
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
queueConnection.start();
@@ -303,7 +315,7 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
ActiveMQQueue compositeQ = new ActiveMQQueue(compositeDest.toString());
for (int id = 0; id < nBrokers; id++) {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
@@ -330,7 +342,7 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
private List<ConsumerState> startAllGWConsumers(int nBrokers) throws
Exception {
List<ConsumerState> consumerStates = new LinkedList<ConsumerState>();
for (int id = 0; id < nBrokers; id++) {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
@@ -393,7 +405,7 @@ public class AMQ4485LowLimitTest extends
JmsMultipleBrokersTestSupport {
@Override
public void run() {
try {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
queueConnection.start();
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest.java
index 4c7a4621a5..3867d7687e 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest.java
@@ -59,22 +59,30 @@ import org.slf4j.LoggerFactory;
public class AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends
JmsMultipleBrokersTestSupport {
static final String payload = new String(new byte[10 * 1024]);
private static final Logger LOG =
LoggerFactory.getLogger(AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest.class);
- final int portBase = 61600;
final int numBrokers = 4;
+ final int[] brokerPorts = new int[numBrokers];
final int numProducers = 10;
final int numMessages = 800;
final int consumerSleepTime = 20;
- StringBuilder brokersUrl = new StringBuilder();
HashMap<ActiveMQQueue, AtomicInteger> accumulators = new
HashMap<ActiveMQQueue, AtomicInteger>();
private ArrayList<Throwable> exceptions = new ArrayList<Throwable>();
- protected void buildUrlList() throws Exception {
+ protected void collectBrokerPorts() throws Exception {
for (int i = 0; i < numBrokers; i++) {
- brokersUrl.append("tcp://localhost:" + (portBase + i));
+ final BrokerService broker = brokers.get("B" + i).broker;
+ brokerPorts[i] =
broker.getTransportConnectors().get(0).getConnectUri().getPort();
+ }
+ }
+
+ protected String buildBrokersUrl() {
+ final StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < numBrokers; i++) {
+ sb.append("tcp://localhost:").append(brokerPorts[i]);
if (i != numBrokers - 1) {
- brokersUrl.append(',');
+ sb.append(',');
}
}
+ return sb.toString();
}
protected BrokerService createBroker(int brokerid) throws Exception {
@@ -86,9 +94,8 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
broker.setUseJmx(true);
broker.setBrokerName("B" + brokerid);
- broker.addConnector(new URI("tcp://localhost:" + (portBase +
brokerid)));
+ broker.addConnector(new URI("tcp://localhost:0"));
- addNetworkConnector(broker);
broker.setSchedulePeriodForDestinationPurge(0);
broker.getSystemUsage().setSendFailIfNoSpace(true);
broker.getSystemUsage().getMemoryUsage().setLimit(512 * 1024 * 1024);
@@ -113,12 +120,11 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
return broker;
}
- private void addNetworkConnector(BrokerService broker) throws Exception {
- StringBuilder networkConnectorUrl = new
StringBuilder("static:(").append(brokersUrl.toString());
- networkConnectorUrl.append(')');
+ private void addNetworkConnector(BrokerService broker, String brokersUrl)
throws Exception {
+ final String networkConnectorUrl = "static:(" + brokersUrl + ")";
for (int i = 0; i < 2; i++) {
- NetworkConnector nc = new DiscoveryNetworkConnector(new
URI(networkConnectorUrl.toString()));
+ final NetworkConnector nc = new DiscoveryNetworkConnector(new
URI(networkConnectorUrl));
nc.setName("Bridge-" + i);
nc.setNetworkTTL(1);
nc.setDecreaseNetworkConsumerPriority(true);
@@ -127,18 +133,25 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
nc.setDynamicallyIncludedDestinations(
Arrays.asList(new ActiveMQDestination[]{new
ActiveMQQueue("GW.*")}));
broker.addNetworkConnector(nc);
+ broker.startNetworkConnector(nc, null);
}
}
public void testBrokers() throws Exception {
- buildUrlList();
-
for (int i = 0; i < numBrokers; i++) {
createBroker(i);
}
startAllBrokers();
+
+ // Get actual ports after brokers start and add network connectors
+ collectBrokerPorts();
+ final String brokersUrl = buildBrokersUrl();
+ for (int i = 0; i < numBrokers; i++) {
+ addNetworkConnector(brokers.get("B" + i).broker, brokersUrl);
+ }
+
waitForBridgeFormation(numBrokers - 1);
verifyPeerBrokerInfos(numBrokers - 1);
@@ -192,7 +205,7 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
ActiveMQQueue compositeQ = new ActiveMQQueue(compositeDest.toString());
for (int id = 0; id < nBrokers; id++) {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
@@ -219,7 +232,7 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
private List<ConsumerState> startAllGWConsumers(int nBrokers) throws
Exception {
List<ConsumerState> consumerStates = new LinkedList<ConsumerState>();
for (int id = 0; id < nBrokers; id++) {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
@@ -281,7 +294,7 @@ public class
AMQ4485NetworkOfXBrokersWithNDestsFanoutTransactionTest extends Jms
@Override
public void run() {
try {
- ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + (portBase + id) + ")");
+ ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("failover:(tcp://localhost:" + brokerPorts[id] + ")");
connectionFactory.setWatchTopicAdvisories(false);
QueueConnection queueConnection =
connectionFactory.createQueueConnection();
queueConnection.start();
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/transport/failover/ConnectionHangOnStartupTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/failover/ConnectionHangOnStartupTest.java
index ea604c28ed..65277171c9 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/transport/failover/ConnectionHangOnStartupTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/transport/failover/ConnectionHangOnStartupTest.java
@@ -17,10 +17,13 @@
package org.apache.activemq.transport.failover;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import jakarta.jms.Connection;
+import static org.junit.Assert.assertTrue;
+
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.xbean.BrokerFactoryBean;
@@ -37,10 +40,7 @@ public class ConnectionHangOnStartupTest {
private static final Logger LOG =
LoggerFactory.getLogger(ConnectionHangOnStartupTest.class);
- // short maxInactivityDurationInitalDelay to trigger the bug, short
- // maxReconnectDelay so that the test runs faster (because it will retry
- // connection sooner)
- protected String uriString =
"failover://(tcp://localhost:62001?wireFormat.maxInactivityDurationInitalDelay=1,tcp://localhost:62002?wireFormat.maxInactivityDurationInitalDelay=1)?randomize=false&maxReconnectDelay=200";
+ protected String uriString;
protected BrokerService master = null;
protected AtomicReference<BrokerService> slave = new
AtomicReference<BrokerService>();
@@ -60,10 +60,20 @@ public class ConnectionHangOnStartupTest {
}
protected void createMaster() throws Exception {
+ // Use ephemeral port for XML-based broker config
+ System.setProperty("masterPort", "0");
+
BrokerFactoryBean brokerFactory = new BrokerFactoryBean(new
ClassPathResource(getMasterXml()));
brokerFactory.afterPropertiesSet();
master = brokerFactory.getBroker();
master.start();
+
+ // Get actual port and build failover URI with short
maxInactivityDurationInitalDelay
+ // to trigger the bug, and short maxReconnectDelay so the test runs
faster
+ final int masterPort =
master.getTransportConnectors().get(0).getConnectUri().getPort();
+ System.setProperty("slavePort", String.valueOf(masterPort));
+ uriString = "failover://(tcp://localhost:" + masterPort
+ +
"?wireFormat.maxInactivityDurationInitalDelay=1)?randomize=false&maxReconnectDelay=200";
}
protected void createSlave() throws Exception {
@@ -84,26 +94,25 @@ public class ConnectionHangOnStartupTest {
@Test(timeout=60000)
public void testInitialWireFormatNegotiationTimeout() throws Exception {
- final AtomicReference<Connection> conn = new
AtomicReference<Connection>();
+ final AtomicReference<Connection> conn = new AtomicReference<>();
final CountDownLatch connStarted = new CountDownLatch(1);
- Thread t = new Thread() {
- @Override
- public void run() {
- try {
- conn.set(createConnectionFactory().createConnection());
- conn.get().start();
- } catch (Exception ex) {
- LOG.error("could not create or start connection", ex);
- }
- connStarted.countDown();
+ // Must create master first to get the ephemeral port and build
uriString
+ createMaster();
+
+ final Thread t = new Thread(() -> {
+ try {
+ conn.set(createConnectionFactory().createConnection());
+ conn.get().start();
+ } catch (Exception ex) {
+ LOG.error("could not create or start connection", ex);
}
- };
+ connStarted.countDown();
+ });
t.start();
- createMaster();
- // slave will never start unless the master dies!
- //createSlave();
+ // Wait for connection to be established
+ assertTrue("connection started", connStarted.await(30,
TimeUnit.SECONDS));
conn.get().stop();
}
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TestBrokerConnectionDuplexExcludedDestinations.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TestBrokerConnectionDuplexExcludedDestinations.java
index 2e0f11b6cc..0e26e43fe2 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TestBrokerConnectionDuplexExcludedDestinations.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TestBrokerConnectionDuplexExcludedDestinations.java
@@ -48,33 +48,36 @@ public class TestBrokerConnectionDuplexExcludedDestinations
extends TestCase {
@Override
public void setUp() throws Exception {
- // Hub broker
+ // Hub broker - start first to get its port
String configFileName =
"org/apache/activemq/usecases/receiver-duplex.xml";
URI uri = new URI("xbean:" + configFileName);
receiverBroker = BrokerFactory.createBroker(uri);
receiverBroker.setPersistent(false);
receiverBroker.setBrokerName("Hub");
+ receiverBroker.start();
+
+ // Get hub's actual port and set it for the sender XML network
connector
+ final int hubPort =
receiverBroker.getTransportConnectors().get(0).getConnectUri().getPort();
+ System.setProperty("receiverPort", String.valueOf(hubPort));
- // Spoke broker
+ // Spoke broker - created after hub starts so ${receiverPort} is
resolved
configFileName = "org/apache/activemq/usecases/sender-duplex.xml";
uri = new URI("xbean:" + configFileName);
senderBroker = BrokerFactory.createBroker(uri);
senderBroker.setPersistent(false);
senderBroker.setBrokerName("Spoke");
-
- // Start both Hub and Spoke broker
- receiverBroker.start();
senderBroker.start();
- // create hub session
- ConnectionFactory cfHub = new
ActiveMQConnectionFactory("tcp://localhost:62002");
+ final int spokePort =
senderBroker.getTransportConnectors().get(0).getConnectUri().getPort();
+ // create hub session
+ final ConnectionFactory cfHub = new
ActiveMQConnectionFactory("tcp://localhost:" + hubPort);
hubConnection = cfHub.createConnection();
hubConnection.start();
hubSession = hubConnection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// create spoke session
- ConnectionFactory cfSpoke = new
ActiveMQConnectionFactory("tcp://localhost:62001");
+ final ConnectionFactory cfSpoke = new
ActiveMQConnectionFactory("tcp://localhost:" + spokePort);
spokeConnection = cfSpoke.createConnection();
spokeConnection.start();
spokeSession = spokeConnection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
diff --git
a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java
b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java
index c1f2c949d8..3fe446b911 100644
---
a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java
+++
b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TwoBrokerTopicSendReceiveUsingJavaConfigurationTest.java
@@ -20,13 +20,16 @@ import jakarta.jms.JMSException;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.network.NetworkConnector;
/**
- *
+ *
*/
public class TwoBrokerTopicSendReceiveUsingJavaConfigurationTest extends
TwoBrokerTopicSendReceiveTest {
BrokerService receiveBroker;
BrokerService sendBroker;
+ int sendPort;
+ int receivePort;
protected ActiveMQConnectionFactory createReceiverConnectionFactory()
throws JMSException {
try {
@@ -34,12 +37,18 @@ public class
TwoBrokerTopicSendReceiveUsingJavaConfigurationTest extends TwoBrok
receiveBroker.setBrokerName("receiveBroker");
receiveBroker.setUseJmx(false);
receiveBroker.setPersistent(false);
- receiveBroker.addConnector("tcp://localhost:62002");
-
receiveBroker.addNetworkConnector("static:failover:tcp://localhost:62001");
+ receiveBroker.addConnector("tcp://localhost:0");
receiveBroker.start();
- ActiveMQConnectionFactory factory = new
ActiveMQConnectionFactory("tcp://localhost:62002");
- return factory;
+ receivePort =
receiveBroker.getTransportConnectors().get(0).getConnectUri().getPort();
+
+ // Add and start network connectors now that both ports are known
+ final NetworkConnector sendNc =
sendBroker.addNetworkConnector("static:failover:tcp://localhost:" +
receivePort);
+ sendBroker.startNetworkConnector(sendNc, null);
+ final NetworkConnector receiveNc =
receiveBroker.addNetworkConnector("static:failover:tcp://localhost:" +
sendPort);
+ receiveBroker.startNetworkConnector(receiveNc, null);
+
+ return new ActiveMQConnectionFactory("tcp://localhost:" +
receivePort);
} catch (Exception e) {
e.printStackTrace();
return null;
@@ -52,12 +61,12 @@ public class
TwoBrokerTopicSendReceiveUsingJavaConfigurationTest extends TwoBrok
sendBroker.setBrokerName("sendBroker");
sendBroker.setUseJmx(false);
sendBroker.setPersistent(false);
- sendBroker.addConnector("tcp://localhost:62001");
-
sendBroker.addNetworkConnector("static:failover:tcp://localhost:62002");
+ sendBroker.addConnector("tcp://localhost:0");
sendBroker.start();
- ActiveMQConnectionFactory factory = new
ActiveMQConnectionFactory("tcp://localhost:62001");
- return factory;
+ sendPort =
sendBroker.getTransportConnectors().get(0).getConnectUri().getPort();
+
+ return new ActiveMQConnectionFactory("tcp://localhost:" +
sendPort);
} catch (Exception e) {
e.printStackTrace();
return null;
diff --git
a/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileMaster.xml
b/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileMaster.xml
index f5eef67da2..81ecf2dc64 100644
---
a/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileMaster.xml
+++
b/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileMaster.xml
@@ -26,7 +26,7 @@
<broker brokerName="shared" useJmx="false"
deleteAllMessagesOnStartup="true"
xmlns="http://activemq.apache.org/schema/core">
<transportConnectors>
- <transportConnector uri="tcp://localhost:62001"/>
+ <transportConnector uri="tcp://localhost:0"/>
</transportConnectors>
</broker>
diff --git
a/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileSlave.xml
b/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileSlave.xml
index 7c88a8c689..999366befb 100644
---
a/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileSlave.xml
+++
b/activemq-unit-tests/src/test/resources/org/apache/activemq/broker/ft/sharedFileSlave.xml
@@ -26,7 +26,7 @@
<broker brokerName="shared" useJmx="false"
deleteAllMessagesOnStartup="false"
xmlns="http://activemq.apache.org/schema/core">
<transportConnectors>
- <transportConnector uri="tcp://localhost:62002"/>
+ <transportConnector uri="tcp://localhost:${slavePort}"/>
</transportConnectors>
diff --git
a/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/receiver-duplex.xml
b/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/receiver-duplex.xml
index 9b4750fd69..08471f6f32 100644
---
a/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/receiver-duplex.xml
+++
b/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/receiver-duplex.xml
@@ -33,7 +33,7 @@
<!-- The transport connectors ActiveMQ will listen to -->
<transportConnectors>
- <transportConnector uri="tcp://localhost:62002"/>
+ <transportConnector uri="tcp://localhost:0"/>
</transportConnectors>
diff --git
a/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/sender-duplex.xml
b/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/sender-duplex.xml
index a8195c0745..2d3038b2ba 100644
---
a/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/sender-duplex.xml
+++
b/activemq-unit-tests/src/test/resources/org/apache/activemq/usecases/sender-duplex.xml
@@ -30,7 +30,7 @@
<!-- The store and forward broker networks ActiveMQ will listen to -->
<networkConnectors>
<!-- by default just auto discover the other brokers -->
- <networkConnector name="monitoring" uri="static:(tcp://localhost:62002)"
duplex="true" >
+ <networkConnector name="monitoring"
uri="static:(tcp://localhost:${receiverPort})" duplex="true" >
<dynamicallyIncludedDestinations>
<queue physicalName="include.test.foo"/>
<topic physicalName="include.test.bar"/>
@@ -51,7 +51,7 @@
<!-- The transport connectors ActiveMQ will listen to -->
<transportConnectors>
- <transportConnector uri="tcp://localhost:62001"/>
+ <transportConnector uri="tcp://localhost:0"/>
</transportConnectors>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact