Convert to JUnit 4 test and add a timeout, also remove the tcp transport

connector on a fixed port as its not needed.
This commit is contained in:
Timothy Bish 2014-06-05 17:52:01 -04:00
parent 707940f1a6
commit b8830ddab1
1 changed files with 26 additions and 22 deletions

View File

@ -16,28 +16,32 @@
*/ */
package org.apache.activemq.transport.vm; package org.apache.activemq.transport.vm;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.net.URI; import java.net.URI;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.jms.JMSException; import javax.jms.JMSException;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.junit.Test;
public class VMTransportWaitForTest extends TestCase { public class VMTransportWaitForTest {
private static final String VM_BROKER_URI_NO_WAIT = private static final String VM_BROKER_URI_NO_WAIT =
"vm://localhost?broker.persistent=false&create=false"; "vm://localhost?broker.persistent=false&create=false";
private static final String VM_BROKER_URI_WAIT_FOR_START = private static final String VM_BROKER_URI_WAIT_FOR_START =
VM_BROKER_URI_NO_WAIT + "&waitForStart=20000"; VM_BROKER_URI_NO_WAIT + "&waitForStart=20000";
CountDownLatch started = new CountDownLatch(1); CountDownLatch started = new CountDownLatch(1);
CountDownLatch gotConnection = new CountDownLatch(1); CountDownLatch gotConnection = new CountDownLatch(1);
@Test(timeout=90000)
public void testWaitFor() throws Exception { public void testWaitFor() throws Exception {
try { try {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_NO_WAIT)); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_NO_WAIT));
@ -45,32 +49,32 @@ public class VMTransportWaitForTest extends TestCase {
fail("expect broker not exist exception"); fail("expect broker not exist exception");
} catch (JMSException expectedOnNoBrokerAndNoCreate) { } catch (JMSException expectedOnNoBrokerAndNoCreate) {
} }
// spawn a thread that will wait for an embedded broker to start via vm://.. // spawn a thread that will wait for an embedded broker to start via
// vm://..
Thread t = new Thread() { Thread t = new Thread() {
@Override
public void run() { public void run() {
try { try {
started.countDown(); started.countDown();
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_WAIT_FOR_START)); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(new URI(VM_BROKER_URI_WAIT_FOR_START));
cf.createConnection(); cf.createConnection();
gotConnection.countDown(); gotConnection.countDown();
} catch (Exception e) {
} catch (Exception e) { e.printStackTrace();
e.printStackTrace(); fail("unexpected exception: " + e);
fail("unexpected exception: " + e); }
}
} }
}; };
t.start(); t.start();
started.await(20, TimeUnit.SECONDS); started.await(20, TimeUnit.SECONDS);
Thread.yield(); Thread.yield();
assertFalse("has not got connection", gotConnection.await(2, TimeUnit.SECONDS)); assertFalse("has not got connection", gotConnection.await(2, TimeUnit.SECONDS));
BrokerService broker = new BrokerService(); BrokerService broker = new BrokerService();
broker.setPersistent(false); broker.setPersistent(false);
broker.addConnector("tcp://localhost:61616");
broker.start(); broker.start();
assertTrue("has got connection", gotConnection.await(400, TimeUnit.MILLISECONDS)); assertTrue("has got connection", gotConnection.await(400, TimeUnit.MILLISECONDS));
broker.stop(); broker.stop();
} }
} }