Fix test failure in CI due to fixed port being in use.

This commit is contained in:
Timothy Bish 2015-05-26 13:23:19 -04:00
parent 3125caee5b
commit 2518ab2802
1 changed files with 54 additions and 26 deletions

View File

@ -18,10 +18,24 @@
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.broker.BrokerPlugin;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.security.AuthenticationUser;
import org.apache.activemq.security.SimpleAuthenticationPlugin;
import org.apache.activemq.util.Wait;
@ -31,34 +45,29 @@ import org.junit.Test;
import org.slf4j.Logger;
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 {
protected static final Logger LOG = LoggerFactory.getLogger(AMQ4889Test.class);
public static final String USER = "user";
public static final String GOOD_USER_PASSWORD = "password";
public static final String WRONG_PASSWORD = "wrongPassword";
public static final String PROXY_URI = "tcp://localhost:6002";
public static final String LOCAL_URI = "tcp://localhost:6001";
public static final String PROXY_URI_PREFIX = "tcp://localhost:";
public static final String LOCAL_URI_PREFIX = "tcp://localhost:";
protected BrokerService brokerService;
private String proxyURI;
private String localURI;
private BrokerService brokerService;
private ProxyConnector proxyConnector;
protected TransportConnector transportConnector;
protected ConnectionFactory connectionFactory;
private ConnectionFactory connectionFactory;
private static final Integer ITERATIONS = 100;
protected BrokerService createBroker() throws Exception {
proxyURI = PROXY_URI_PREFIX + findOpenPort();
localURI = LOCAL_URI_PREFIX + findOpenPort();
brokerService = new BrokerService();
brokerService.setPersistent(false);
@ -68,11 +77,11 @@ public class AMQ4889Test {
BrokerPlugin[] array = new BrokerPlugin[plugins.size()];
brokerService.setPlugins(plugins.toArray(array));
transportConnector = brokerService.addConnector(LOCAL_URI);
brokerService.addConnector(localURI);
proxyConnector = new ProxyConnector();
proxyConnector.setName("proxy");
proxyConnector.setBind(new URI(PROXY_URI));
proxyConnector.setRemote(new URI(LOCAL_URI));
proxyConnector.setBind(new URI(proxyURI));
proxyConnector.setRemote(new URI(localURI));
brokerService.addProxyConnector(proxyConnector);
brokerService.start();
@ -92,7 +101,7 @@ public class AMQ4889Test {
@Before
public void setUp() throws Exception {
brokerService = createBroker();
connectionFactory = new ActiveMQConnectionFactory(PROXY_URI);
connectionFactory = new ActiveMQConnectionFactory(proxyURI);
}
@After
@ -101,8 +110,7 @@ public class AMQ4889Test {
brokerService.waitUntilStopped();
}
@Test(timeout = 1 * 60 * 1000)
@Test(timeout = 60000)
public void testForConnectionLeak() throws Exception {
Integer expectedConnectionCount = 0;
for (int i=0; i < ITERATIONS; i++) {
@ -110,15 +118,14 @@ public class AMQ4889Test {
if (i % 2 == 0) {
LOG.debug("Iteration {} adding bad connection", i);
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");
} else {
LOG.debug("Iteration {} adding good connection", i);
Connection connection = connectionFactory.createConnection(USER, GOOD_USER_PASSWORD);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
expectedConnectionCount++;
}
//
} catch (JMSSecurityException e) {
}
LOG.debug("Iteration {} Connections? {}", i, proxyConnector.getConnectionCount());
@ -132,4 +139,25 @@ public class AMQ4889Test {
}, 20);
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;
}
}