Skip to content

Commit b148542

Browse files
author
Phillip Webb
committed
Use port scanning for JMX tests
Attempt to fix performance build by scanning for a free JMX port.
1 parent 1dff45c commit b148542

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

spring-context/src/test/java/org/springframework/jmx/support/MBeanServerConnectionFactoryBeanTests.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import javax.management.remote.JMXConnectorServerFactory;
2424
import javax.management.remote.JMXServiceURL;
2525

26+
import org.junit.Before;
2627
import org.junit.Test;
2728
import org.springframework.aop.support.AopUtils;
2829
import org.springframework.jmx.AbstractMBeanServerTests;
2930
import org.springframework.tests.Assume;
3031
import org.springframework.tests.TestGroup;
32+
import org.springframework.util.SocketUtils;
3133

3234
import static org.junit.Assert.*;
3335

@@ -37,16 +39,27 @@
3739
*/
3840
public class MBeanServerConnectionFactoryBeanTests extends AbstractMBeanServerTests {
3941

40-
private static final String SERVICE_URL = "service:jmx:jmxmp://localhost:9878";
4142

42-
private JMXServiceURL getServiceUrl() throws MalformedURLException {
43-
return new JMXServiceURL(SERVICE_URL);
43+
private String serviceUrl;
44+
45+
46+
@Before
47+
public void getUrl() {
48+
int port = SocketUtils.findAvailableTcpPort(9800, 9900);
49+
this.serviceUrl = "service:jmx:jmxmp://localhost:" + port;
50+
System.out.println(port);
51+
}
52+
53+
54+
private JMXServiceURL getJMXServiceUrl() throws MalformedURLException {
55+
return new JMXServiceURL(serviceUrl);
4456
}
4557

4658
private JMXConnectorServer getConnectorServer() throws Exception {
47-
return JMXConnectorServerFactory.newJMXConnectorServer(getServiceUrl(), null, getServer());
59+
return JMXConnectorServerFactory.newJMXConnectorServer(getJMXServiceUrl(), null, getServer());
4860
}
4961

62+
5063
@Test
5164
public void testTestValidConnection() throws Exception {
5265
Assume.group(TestGroup.JMXMP);
@@ -55,7 +68,7 @@ public void testTestValidConnection() throws Exception {
5568

5669
try {
5770
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
58-
bean.setServiceUrl(SERVICE_URL);
71+
bean.setServiceUrl(serviceUrl);
5972
bean.afterPropertiesSet();
6073

6174
try {
@@ -87,7 +100,7 @@ public void testWithNoServiceUrl() throws Exception {
87100
public void testTestWithLazyConnection() throws Exception {
88101
Assume.group(TestGroup.JMXMP);
89102
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
90-
bean.setServiceUrl(SERVICE_URL);
103+
bean.setServiceUrl(serviceUrl);
91104
bean.setConnectOnStartup(false);
92105
bean.afterPropertiesSet();
93106

@@ -110,7 +123,7 @@ public void testTestWithLazyConnection() throws Exception {
110123
@Test
111124
public void testWithLazyConnectionAndNoAccess() throws Exception {
112125
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
113-
bean.setServiceUrl(SERVICE_URL);
126+
bean.setServiceUrl(serviceUrl);
114127
bean.setConnectOnStartup(false);
115128
bean.afterPropertiesSet();
116129

0 commit comments

Comments
 (0)