Clean up some tests, reduce resources used and reduce runtime, convert

to JUnit 4 when possible and add timeouts.
This commit is contained in:
Timothy Bish 2015-02-23 19:53:14 -05:00
parent 05b401993b
commit 02dc6ce982
4 changed files with 138 additions and 130 deletions

View File

@ -16,14 +16,15 @@
*/ */
package org.apache.activemq; package org.apache.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic; import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.JmsTemplate;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
/** /**
* A useful base class which creates and closes an embedded broker * A useful base class which creates and closes an embedded broker
@ -40,6 +41,7 @@ public abstract class EmbeddedBrokerTestSupport extends CombinationTestSupport {
protected ActiveMQDestination destination; protected ActiveMQDestination destination;
protected JmsTemplate template; protected JmsTemplate template;
@Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
if (broker == null) { if (broker == null) {
broker = createBroker(); broker = createBroker();
@ -56,6 +58,7 @@ public abstract class EmbeddedBrokerTestSupport extends CombinationTestSupport {
template.afterPropertiesSet(); template.afterPropertiesSet();
} }
@Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
if (broker != null) { if (broker != null) {
try { try {
@ -119,6 +122,7 @@ public abstract class EmbeddedBrokerTestSupport extends CombinationTestSupport {
protected BrokerService createBroker() throws Exception { protected BrokerService createBroker() throws Exception {
BrokerService answer = new BrokerService(); BrokerService answer = new BrokerService();
answer.setPersistent(isPersistent()); answer.setPersistent(isPersistent());
answer.getManagementContext().setCreateConnector(false);
answer.addConnector(bindAddress); answer.addConnector(bindAddress);
return answer; return answer;
} }

View File

@ -16,6 +16,9 @@
*/ */
package org.apache.activemq; package org.apache.activemq;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.JMSException; import javax.jms.JMSException;
@ -24,26 +27,35 @@ import javax.jms.MessageConsumer;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import junit.framework.TestCase; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQQueue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ExclusiveConsumerTest extends TestCase { public class ExclusiveConsumerTest {
private static final String VM_BROKER_URL = "vm://localhost?broker.persistent=false&broker.useJmx=true"; private static final String VM_BROKER_URL = "vm://localhost";
public ExclusiveConsumerTest(String name) { private BrokerService brokerService;
super(name);
@Before
public void setUp() throws Exception {
brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.setUseJmx(false);
brokerService.setSchedulerSupport(false);
brokerService.setAdvisorySupport(false);
brokerService.start();
} }
@Override @After
protected void setUp() throws Exception { public void tearDown() throws Exception {
super.setUp(); if (brokerService != null) {
} brokerService.stop();
brokerService = null;
@Override }
protected void tearDown() throws Exception {
super.tearDown();
} }
private Connection createConnection(final boolean start) throws JMSException { private Connection createConnection(final boolean start) throws JMSException {
@ -55,6 +67,7 @@ public class ExclusiveConsumerTest extends TestCase {
return conn; return conn;
} }
@Test(timeout = 60000)
public void testExclusiveConsumerSelectedCreatedFirst() throws JMSException, InterruptedException { public void testExclusiveConsumerSelectedCreatedFirst() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
@ -63,7 +76,6 @@ public class ExclusiveConsumerTest extends TestCase {
Session senderSession = null; Session senderSession = null;
try { try {
exclusiveSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -87,15 +99,14 @@ public class ExclusiveConsumerTest extends TestCase {
// Verify exclusive consumer receives the message. // Verify exclusive consumer receives the message.
assertNotNull(exclusiveConsumer.receive(100)); assertNotNull(exclusiveConsumer.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
} finally { } finally {
fallbackSession.close(); fallbackSession.close();
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
@Test(timeout = 60000)
public void testExclusiveConsumerSelectedCreatedAfter() throws JMSException, InterruptedException { public void testExclusiveConsumerSelectedCreatedAfter() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
@ -104,7 +115,6 @@ public class ExclusiveConsumerTest extends TestCase {
Session senderSession = null; Session senderSession = null;
try { try {
exclusiveSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -132,11 +142,10 @@ public class ExclusiveConsumerTest extends TestCase {
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
public void testFailoverToAnotherExclusiveConsumerCreatedFirst() throws JMSException, @Test(timeout = 60000)
InterruptedException { public void testFailoverToAnotherExclusiveConsumerCreatedFirst() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
Session exclusiveSession1 = null; Session exclusiveSession1 = null;
@ -145,14 +154,12 @@ public class ExclusiveConsumerTest extends TestCase {
Session senderSession = null; Session senderSession = null;
try { try {
exclusiveSession1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
exclusiveSession2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// This creates the exclusive consumer first which avoids AMQ-1024 // This creates the exclusive consumer first which avoids AMQ-1024 bug.
// bug.
ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE2?consumer.exclusive=true"); ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE2?consumer.exclusive=true");
MessageConsumer exclusiveConsumer1 = exclusiveSession1.createConsumer(exclusiveQueue); MessageConsumer exclusiveConsumer1 = exclusiveSession1.createConsumer(exclusiveQueue);
MessageConsumer exclusiveConsumer2 = exclusiveSession2.createConsumer(exclusiveQueue); MessageConsumer exclusiveConsumer2 = exclusiveSession2.createConsumer(exclusiveQueue);
@ -173,8 +180,7 @@ public class ExclusiveConsumerTest extends TestCase {
assertNull(exclusiveConsumer2.receive(100)); assertNull(exclusiveConsumer2.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
// Close the exclusive consumer to verify the non-exclusive consumer // Close the exclusive consumer to verify the non-exclusive consumer takes over
// takes over
exclusiveConsumer1.close(); exclusiveConsumer1.close();
producer.send(msg); producer.send(msg);
@ -188,11 +194,10 @@ public class ExclusiveConsumerTest extends TestCase {
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
public void testFailoverToAnotherExclusiveConsumerCreatedAfter() throws JMSException, @Test(timeout = 60000)
InterruptedException { public void testFailoverToAnotherExclusiveConsumerCreatedAfter() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
Session exclusiveSession1 = null; Session exclusiveSession1 = null;
@ -201,14 +206,12 @@ public class ExclusiveConsumerTest extends TestCase {
Session senderSession = null; Session senderSession = null;
try { try {
exclusiveSession1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
exclusiveSession2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); exclusiveSession2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// This creates the exclusive consumer first which avoids AMQ-1024 // This creates the exclusive consumer first which avoids AMQ-1024 bug.
// bug.
ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE6?consumer.exclusive=true"); ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE6?consumer.exclusive=true");
MessageConsumer exclusiveConsumer1 = exclusiveSession1.createConsumer(exclusiveQueue); MessageConsumer exclusiveConsumer1 = exclusiveSession1.createConsumer(exclusiveQueue);
@ -230,8 +233,7 @@ public class ExclusiveConsumerTest extends TestCase {
assertNull(exclusiveConsumer2.receive(100)); assertNull(exclusiveConsumer2.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
// Close the exclusive consumer to verify the non-exclusive consumer // Close the exclusive consumer to verify the non-exclusive consumer takes over
// takes over
exclusiveConsumer1.close(); exclusiveConsumer1.close();
producer.send(msg); producer.send(msg);
@ -239,15 +241,14 @@ public class ExclusiveConsumerTest extends TestCase {
assertNotNull(exclusiveConsumer2.receive(1000)); assertNotNull(exclusiveConsumer2.receive(1000));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
} finally { } finally {
fallbackSession.close(); fallbackSession.close();
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
@Test(timeout = 60000)
public void testFailoverToNonExclusiveConsumer() throws JMSException, InterruptedException { public void testFailoverToNonExclusiveConsumer() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
@ -261,8 +262,7 @@ public class ExclusiveConsumerTest extends TestCase {
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// This creates the exclusive consumer first which avoids AMQ-1024 // This creates the exclusive consumer first which avoids AMQ-1024 bug.
// bug.
ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE3?consumer.exclusive=true"); ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE3?consumer.exclusive=true");
MessageConsumer exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue); MessageConsumer exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue);
@ -281,22 +281,20 @@ public class ExclusiveConsumerTest extends TestCase {
assertNotNull(exclusiveConsumer.receive(100)); assertNotNull(exclusiveConsumer.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
// Close the exclusive consumer to verify the non-exclusive consumer // Close the exclusive consumer to verify the non-exclusive consumer takes over
// takes over
exclusiveConsumer.close(); exclusiveConsumer.close();
producer.send(msg); producer.send(msg);
assertNotNull(fallbackConsumer.receive(100)); assertNotNull(fallbackConsumer.receive(100));
} finally { } finally {
fallbackSession.close(); fallbackSession.close();
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
@Test(timeout = 60000)
public void testFallbackToExclusiveConsumer() throws JMSException, InterruptedException { public void testFallbackToExclusiveConsumer() throws JMSException, InterruptedException {
Connection conn = createConnection(true); Connection conn = createConnection(true);
@ -310,8 +308,7 @@ public class ExclusiveConsumerTest extends TestCase {
fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); fallbackSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); senderSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// This creates the exclusive consumer first which avoids AMQ-1024 // This creates the exclusive consumer first which avoids AMQ-1024 bug.
// bug.
ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE4?consumer.exclusive=true"); ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.QUEUE4?consumer.exclusive=true");
MessageConsumer exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue); MessageConsumer exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue);
@ -330,8 +327,7 @@ public class ExclusiveConsumerTest extends TestCase {
assertNotNull(exclusiveConsumer.receive(100)); assertNotNull(exclusiveConsumer.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
// Close the exclusive consumer to verify the non-exclusive consumer // Close the exclusive consumer to verify the non-exclusive consumer takes over
// takes over
exclusiveConsumer.close(); exclusiveConsumer.close();
producer.send(msg); producer.send(msg);
@ -339,19 +335,16 @@ public class ExclusiveConsumerTest extends TestCase {
// Verify other non-exclusive consumer receices the message. // Verify other non-exclusive consumer receices the message.
assertNotNull(fallbackConsumer.receive(100)); assertNotNull(fallbackConsumer.receive(100));
// Create exclusive consumer to determine if it will start receiving // Create exclusive consumer to determine if it will start receiving the messages.
// the messages.
exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue); exclusiveConsumer = exclusiveSession.createConsumer(exclusiveQueue);
producer.send(msg); producer.send(msg);
assertNotNull(exclusiveConsumer.receive(100)); assertNotNull(exclusiveConsumer.receive(100));
assertNull(fallbackConsumer.receive(100)); assertNull(fallbackConsumer.receive(100));
} finally { } finally {
fallbackSession.close(); fallbackSession.close();
senderSession.close(); senderSession.close();
conn.close(); conn.close();
} }
} }
} }

View File

@ -16,6 +16,9 @@
*/ */
package org.apache.activemq; package org.apache.activemq;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Vector; import java.util.Vector;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -32,41 +35,51 @@ import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import junit.framework.TestCase; import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
public class JmsConsumerResetActiveListenerTest {
public class JmsConsumerResetActiveListenerTest extends TestCase {
private Connection connection; private Connection connection;
private ActiveMQConnectionFactory factory; private ActiveMQConnectionFactory factory;
protected void setUp() throws Exception { @Rule
factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); public final TestName name = new TestName();
@Before
public void setUp() throws Exception {
factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
connection = factory.createConnection(); connection = factory.createConnection();
} }
protected void tearDown() throws Exception { @After
public void tearDown() throws Exception {
if (connection != null) { if (connection != null) {
connection.close(); connection.close();
connection = null; connection = null;
} }
} }
/** /**
* verify the (undefined by spec) behaviour of setting a listener while receiving a message. * verify the (undefined by spec) behaviour of setting a listener while receiving a message.
* *
* @throws Exception * @throws Exception
*/ */
@Test(timeout = 60000)
public void testSetListenerFromListener() throws Exception { public void testSetListenerFromListener() throws Exception {
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination dest = session.createQueue("Queue-" + getName()); Destination dest = session.createQueue("Queue-" + name.getMethodName());
final MessageConsumer consumer = session.createConsumer(dest); final MessageConsumer consumer = session.createConsumer(dest);
final CountDownLatch latch = new CountDownLatch(2); final CountDownLatch latch = new CountDownLatch(2);
final AtomicBoolean first = new AtomicBoolean(true); final AtomicBoolean first = new AtomicBoolean(true);
final Vector<Object> results = new Vector<Object>(); final Vector<Object> results = new Vector<Object>();
consumer.setMessageListener(new MessageListener() { consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) { public void onMessage(Message message) {
if (first.compareAndSet(true, false)) { if (first.compareAndSet(true, false)) {
try { try {
@ -83,14 +96,14 @@ public class JmsConsumerResetActiveListenerTest extends TestCase {
}); });
connection.start(); connection.start();
MessageProducer producer = session.createProducer(dest); MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First")); producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second")); producer.send(session.createTextMessage("Second"));
assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS)); assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS));
assertEquals("we have a result", 2, results.size()); assertEquals("we have a result", 2, results.size());
Object result = results.get(0); Object result = results.get(0);
assertTrue(result instanceof TextMessage); assertTrue(result instanceof TextMessage);
@ -99,22 +112,24 @@ public class JmsConsumerResetActiveListenerTest extends TestCase {
assertTrue(result instanceof TextMessage); assertTrue(result instanceof TextMessage);
assertEquals("result is first", "Second", ((TextMessage)result).getText()); assertEquals("result is first", "Second", ((TextMessage)result).getText());
} }
/** /**
* and a listener on a new consumer, just in case. * and a listener on a new consumer, just in case.
* *
* @throws Exception * @throws Exception
*/ */
@Test(timeout = 60000)
public void testNewConsumerSetListenerFromListener() throws Exception { public void testNewConsumerSetListenerFromListener() throws Exception {
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final Destination dest = session.createQueue("Queue-" + getName()); final Destination dest = session.createQueue("Queue-" + name.getMethodName());
final MessageConsumer consumer = session.createConsumer(dest); final MessageConsumer consumer = session.createConsumer(dest);
final CountDownLatch latch = new CountDownLatch(2); final CountDownLatch latch = new CountDownLatch(2);
final AtomicBoolean first = new AtomicBoolean(true); final AtomicBoolean first = new AtomicBoolean(true);
final Vector<Object> results = new Vector<Object>(); final Vector<Object> results = new Vector<Object>();
consumer.setMessageListener(new MessageListener() { consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) { public void onMessage(Message message) {
if (first.compareAndSet(true, false)) { if (first.compareAndSet(true, false)) {
try { try {
@ -132,14 +147,14 @@ public class JmsConsumerResetActiveListenerTest extends TestCase {
}); });
connection.start(); connection.start();
MessageProducer producer = session.createProducer(dest); MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First")); producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second")); producer.send(session.createTextMessage("Second"));
assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS)); assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS));
assertEquals("we have a result", 2, results.size()); assertEquals("we have a result", 2, results.size());
Object result = results.get(0); Object result = results.get(0);
assertTrue(result instanceof TextMessage); assertTrue(result instanceof TextMessage);

View File

@ -16,6 +16,11 @@
*/ */
package org.apache.activemq; package org.apache.activemq;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
@ -24,76 +29,67 @@ import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.Topic; import javax.jms.Topic;
/** import org.junit.After;
* import org.junit.Before;
*/ import org.junit.Rule;
public class JmsCreateConsumerInOnMessageTest extends TestSupport implements MessageListener { import org.junit.Test;
import org.junit.rules.TestName;
public class JmsCreateConsumerInOnMessageTest {
private Connection connection; private Connection connection;
private Session publisherSession; private ActiveMQConnectionFactory factory;
private Session consumerSession;
private MessageConsumer consumer;
private MessageConsumer testConsumer;
private MessageProducer producer;
private Topic topic;
private Object lock = new Object();
/* @Rule
* @see junit.framework.TestCase#setUp() public final TestName name = new TestName();
*/
protected void setUp() throws Exception { @Before
super.setUp(); public void setUp() throws Exception {
super.topic = true; factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
connection = createConnection(); connection = factory.createConnection();
connection.setClientID("connection:" + getSubject());
publisherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic)super.createDestination("Test.Topic");
consumer = consumerSession.createConsumer(topic);
consumer.setMessageListener(this);
producer = publisherSession.createProducer(topic);
connection.start();
} }
/* @After
* @see junit.framework.TestCase#tearDown() public void tearDown() throws Exception {
*/ if (connection != null) {
protected void tearDown() throws Exception { connection.close();
super.tearDown(); connection = null;
connection.close(); }
} }
/** /**
* Tests if a consumer can be created asynchronusly * Tests if a consumer can be created asynchronusly
* *
* @throws Exception * @throws Exception
*/ */
@Test(timeout = 60000)
public void testCreateConsumer() throws Exception { public void testCreateConsumer() throws Exception {
Message msg = super.createMessage(); final CountDownLatch done = new CountDownLatch(1);
producer.send(msg);
if (testConsumer == null) {
synchronized (lock) {
lock.wait(3000);
}
}
assertTrue(testConsumer != null);
}
/** final Session publisherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
* Use the asynchronous subscription mechanism final Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
* final Topic topic = publisherSession.createTopic("Test.Topic");
* @param message
*/ MessageConsumer consumer = consumerSession.createConsumer(topic);
public void onMessage(Message message) { consumer.setMessageListener(new MessageListener() {
try {
testConsumer = consumerSession.createConsumer(topic); @Override
consumerSession.createProducer(topic); public void onMessage(Message message) {
synchronized (lock) { try {
lock.notify(); consumerSession.createConsumer(topic);
consumerSession.createProducer(topic);
done.countDown();
} catch (Exception ex) {
assertTrue(false);
}
} }
} catch (Exception ex) { });
ex.printStackTrace();
assertTrue(false); MessageProducer producer = publisherSession.createProducer(topic);
} connection.start();
producer.send(publisherSession.createTextMessage("test"));
assertTrue("Should have finished onMessage", done.await(5, TimeUnit.SECONDS));
} }
} }