Skip to content

Commit bbd35de

Browse files
committed
Stop using SocketUtils in ActiveMQ STOMP broker tests
See gh-28052
1 parent 5fbd01f commit bbd35de

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/ReactorNettyTcpStompClientTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package org.springframework.messaging.simp.stomp;
1818

1919
import java.lang.reflect.Type;
20+
import java.net.URI;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.List;
2324
import java.util.concurrent.CountDownLatch;
2425
import java.util.concurrent.TimeUnit;
2526

2627
import org.apache.activemq.broker.BrokerService;
28+
import org.apache.activemq.broker.TransportConnector;
2729
import org.apache.commons.logging.Log;
2830
import org.apache.commons.logging.LogFactory;
2931
import org.junit.jupiter.api.AfterEach;
@@ -60,11 +62,9 @@ public class ReactorNettyTcpStompClientTests {
6062
public void setup(TestInfo testInfo) throws Exception {
6163
logger.debug("Setting up before '" + testInfo.getTestMethod().get().getName() + "'");
6264

63-
@SuppressWarnings("deprecation")
64-
int port = org.springframework.util.SocketUtils.findAvailableTcpPort(61613);
65-
65+
TransportConnector stompConnector = createStompConnector();
6666
this.activeMQBroker = new BrokerService();
67-
this.activeMQBroker.addConnector("stomp://127.0.0.1:" + port);
67+
this.activeMQBroker.addConnector(stompConnector);
6868
this.activeMQBroker.setStartAsync(false);
6969
this.activeMQBroker.setPersistent(false);
7070
this.activeMQBroker.setUseJmx(false);
@@ -75,11 +75,17 @@ public void setup(TestInfo testInfo) throws Exception {
7575
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
7676
taskScheduler.afterPropertiesSet();
7777

78-
this.client = new ReactorNettyTcpStompClient("127.0.0.1", port);
78+
this.client = new ReactorNettyTcpStompClient("127.0.0.1", stompConnector.getServer().getSocketAddress().getPort());
7979
this.client.setMessageConverter(new StringMessageConverter());
8080
this.client.setTaskScheduler(taskScheduler);
8181
}
8282

83+
private TransportConnector createStompConnector() throws Exception {
84+
TransportConnector connector = new TransportConnector();
85+
connector.setUri(new URI("stomp://127.0.0.1:0"));
86+
return connector;
87+
}
88+
8389
@AfterEach
8490
public void shutdown() throws Exception {
8591
try {

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.messaging.simp.stomp;
1818

19+
import java.net.URI;
1920
import java.nio.charset.StandardCharsets;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
@@ -26,6 +27,7 @@
2627
import java.util.concurrent.TimeUnit;
2728

2829
import org.apache.activemq.broker.BrokerService;
30+
import org.apache.activemq.broker.TransportConnector;
2931
import org.apache.commons.logging.Log;
3032
import org.apache.commons.logging.LogFactory;
3133
import org.junit.jupiter.api.AfterEach;
@@ -70,15 +72,15 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
7072

7173
private TestEventPublisher eventPublisher;
7274

73-
private int port;
75+
// initial value of zero implies that a random ephemeral port should be used
76+
private int port = 0;
7477

7578

7679
@BeforeEach
7780
@SuppressWarnings("deprecation")
7881
public void setup(TestInfo testInfo) throws Exception {
7982
logger.debug("Setting up before '" + testInfo.getTestMethod().get().getName() + "'");
8083

81-
this.port = org.springframework.util.SocketUtils.findAvailableTcpPort(61613);
8284
this.responseChannel = new ExecutorSubscribableChannel();
8385
this.responseHandler = new TestMessageHandler();
8486
this.responseChannel.subscribe(this.responseHandler);
@@ -88,14 +90,25 @@ public void setup(TestInfo testInfo) throws Exception {
8890
}
8991

9092
private void startActiveMQBroker() throws Exception {
93+
TransportConnector stompConnector = createStompConnector(this.port);
9194
this.activeMQBroker = new BrokerService();
92-
this.activeMQBroker.addConnector("stomp://localhost:" + this.port);
95+
this.activeMQBroker.addConnector(stompConnector);
9396
this.activeMQBroker.setStartAsync(false);
9497
this.activeMQBroker.setPersistent(false);
9598
this.activeMQBroker.setUseJmx(false);
9699
this.activeMQBroker.getSystemUsage().getMemoryUsage().setLimit(1024 * 1024 * 5);
97100
this.activeMQBroker.getSystemUsage().getTempUsage().setLimit(1024 * 1024 * 5);
98101
this.activeMQBroker.start();
102+
103+
// Reuse existing ephemeral port on restart (i.e., the next time this method
104+
// is invoked) since it will already be configured in the relay
105+
this.port = (this.port != 0 ? this.port : stompConnector.getServer().getSocketAddress().getPort());
106+
}
107+
108+
private TransportConnector createStompConnector(int port) throws Exception {
109+
TransportConnector connector = new TransportConnector();
110+
connector.setUri(new URI("stomp://localhost:" + port));
111+
return connector;
99112
}
100113

101114
private void createAndStartRelay() throws InterruptedException {

0 commit comments

Comments
 (0)