mirror of https://github.com/apache/activemq.git
Fix test failure in CI due to fixed port being in use.
This commit is contained in:
parent
3125caee5b
commit
2518ab2802
|
@ -18,10 +18,24 @@
|
||||||
package org.apache.activemq.proxy;
|
package org.apache.activemq.proxy;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.jms.Connection;
|
||||||
|
import javax.jms.ConnectionFactory;
|
||||||
|
import javax.jms.JMSSecurityException;
|
||||||
|
import javax.jms.Session;
|
||||||
|
import javax.net.ServerSocketFactory;
|
||||||
|
|
||||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||||
import org.apache.activemq.broker.BrokerPlugin;
|
import org.apache.activemq.broker.BrokerPlugin;
|
||||||
import org.apache.activemq.broker.BrokerService;
|
import org.apache.activemq.broker.BrokerService;
|
||||||
import org.apache.activemq.broker.TransportConnector;
|
|
||||||
import org.apache.activemq.security.AuthenticationUser;
|
import org.apache.activemq.security.AuthenticationUser;
|
||||||
import org.apache.activemq.security.SimpleAuthenticationPlugin;
|
import org.apache.activemq.security.SimpleAuthenticationPlugin;
|
||||||
import org.apache.activemq.util.Wait;
|
import org.apache.activemq.util.Wait;
|
||||||
|
@ -31,34 +45,29 @@ import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.jms.Connection;
|
|
||||||
import javax.jms.ConnectionFactory;
|
|
||||||
import javax.jms.JMSSecurityException;
|
|
||||||
import javax.jms.Session;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
public class AMQ4889Test {
|
public class AMQ4889Test {
|
||||||
|
|
||||||
protected static final Logger LOG = LoggerFactory.getLogger(AMQ4889Test.class);
|
protected static final Logger LOG = LoggerFactory.getLogger(AMQ4889Test.class);
|
||||||
|
|
||||||
public static final String USER = "user";
|
public static final String USER = "user";
|
||||||
public static final String GOOD_USER_PASSWORD = "password";
|
public static final String GOOD_USER_PASSWORD = "password";
|
||||||
public static final String WRONG_PASSWORD = "wrongPassword";
|
public static final String WRONG_PASSWORD = "wrongPassword";
|
||||||
public static final String PROXY_URI = "tcp://localhost:6002";
|
public static final String PROXY_URI_PREFIX = "tcp://localhost:";
|
||||||
public static final String LOCAL_URI = "tcp://localhost:6001";
|
public static final String LOCAL_URI_PREFIX = "tcp://localhost:";
|
||||||
|
|
||||||
protected BrokerService brokerService;
|
private String proxyURI;
|
||||||
|
private String localURI;
|
||||||
|
|
||||||
|
private BrokerService brokerService;
|
||||||
private ProxyConnector proxyConnector;
|
private ProxyConnector proxyConnector;
|
||||||
protected TransportConnector transportConnector;
|
private ConnectionFactory connectionFactory;
|
||||||
protected ConnectionFactory connectionFactory;
|
|
||||||
|
|
||||||
private static final Integer ITERATIONS = 100;
|
private static final Integer ITERATIONS = 100;
|
||||||
|
|
||||||
protected BrokerService createBroker() throws Exception {
|
protected BrokerService createBroker() throws Exception {
|
||||||
|
proxyURI = PROXY_URI_PREFIX + findOpenPort();
|
||||||
|
localURI = LOCAL_URI_PREFIX + findOpenPort();
|
||||||
|
|
||||||
brokerService = new BrokerService();
|
brokerService = new BrokerService();
|
||||||
brokerService.setPersistent(false);
|
brokerService.setPersistent(false);
|
||||||
|
|
||||||
|
@ -68,11 +77,11 @@ public class AMQ4889Test {
|
||||||
BrokerPlugin[] array = new BrokerPlugin[plugins.size()];
|
BrokerPlugin[] array = new BrokerPlugin[plugins.size()];
|
||||||
brokerService.setPlugins(plugins.toArray(array));
|
brokerService.setPlugins(plugins.toArray(array));
|
||||||
|
|
||||||
transportConnector = brokerService.addConnector(LOCAL_URI);
|
brokerService.addConnector(localURI);
|
||||||
proxyConnector = new ProxyConnector();
|
proxyConnector = new ProxyConnector();
|
||||||
proxyConnector.setName("proxy");
|
proxyConnector.setName("proxy");
|
||||||
proxyConnector.setBind(new URI(PROXY_URI));
|
proxyConnector.setBind(new URI(proxyURI));
|
||||||
proxyConnector.setRemote(new URI(LOCAL_URI));
|
proxyConnector.setRemote(new URI(localURI));
|
||||||
brokerService.addProxyConnector(proxyConnector);
|
brokerService.addProxyConnector(proxyConnector);
|
||||||
|
|
||||||
brokerService.start();
|
brokerService.start();
|
||||||
|
@ -92,7 +101,7 @@ public class AMQ4889Test {
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
brokerService = createBroker();
|
brokerService = createBroker();
|
||||||
connectionFactory = new ActiveMQConnectionFactory(PROXY_URI);
|
connectionFactory = new ActiveMQConnectionFactory(proxyURI);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@ -101,8 +110,7 @@ public class AMQ4889Test {
|
||||||
brokerService.waitUntilStopped();
|
brokerService.waitUntilStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 60000)
|
||||||
@Test(timeout = 1 * 60 * 1000)
|
|
||||||
public void testForConnectionLeak() throws Exception {
|
public void testForConnectionLeak() throws Exception {
|
||||||
Integer expectedConnectionCount = 0;
|
Integer expectedConnectionCount = 0;
|
||||||
for (int i=0; i < ITERATIONS; i++) {
|
for (int i=0; i < ITERATIONS; i++) {
|
||||||
|
@ -110,15 +118,14 @@ public class AMQ4889Test {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
LOG.debug("Iteration {} adding bad connection", i);
|
LOG.debug("Iteration {} adding bad connection", i);
|
||||||
Connection connection = connectionFactory.createConnection(USER, WRONG_PASSWORD);
|
Connection connection = connectionFactory.createConnection(USER, WRONG_PASSWORD);
|
||||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
fail("createSession should fail");
|
fail("createSession should fail");
|
||||||
} else {
|
} else {
|
||||||
LOG.debug("Iteration {} adding good connection", i);
|
LOG.debug("Iteration {} adding good connection", i);
|
||||||
Connection connection = connectionFactory.createConnection(USER, GOOD_USER_PASSWORD);
|
Connection connection = connectionFactory.createConnection(USER, GOOD_USER_PASSWORD);
|
||||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
expectedConnectionCount++;
|
expectedConnectionCount++;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
} catch (JMSSecurityException e) {
|
} catch (JMSSecurityException e) {
|
||||||
}
|
}
|
||||||
LOG.debug("Iteration {} Connections? {}", i, proxyConnector.getConnectionCount());
|
LOG.debug("Iteration {} Connections? {}", i, proxyConnector.getConnectionCount());
|
||||||
|
@ -132,4 +139,25 @@ public class AMQ4889Test {
|
||||||
}, 20);
|
}, 20);
|
||||||
assertEquals(val, proxyConnector.getConnectionCount());
|
assertEquals(val, proxyConnector.getConnectionCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int findOpenPort() throws IOException {
|
||||||
|
int openPort = 0;
|
||||||
|
ServerSocket ss = null;
|
||||||
|
try {
|
||||||
|
ss = ServerSocketFactory.getDefault().createServerSocket(0);
|
||||||
|
openPort = ss.getLocalPort();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.error("Could not locate an open port: ", e);
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (ss != null ) {
|
||||||
|
ss.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) { // ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return openPort;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue