Clean up test, don't start JMX connector as it is not needed, add
timeout.
This commit is contained in:
Timothy Bish 2015-03-03 19:16:28 -05:00
parent 4d5bb4ab7b
commit 257a4fa416
1 changed files with 27 additions and 86 deletions

View File

@ -14,12 +14,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.activemq.bugs; package org.apache.activemq.bugs;
import java.io.IOException; import static org.junit.Assert.fail;
import java.lang.management.ManagementFactory;
import java.net.ServerSocket;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -31,59 +29,34 @@ import javax.jms.Message;
import javax.jms.MessageListener; import javax.jms.MessageListener;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQTopicSubscriber; import org.apache.activemq.ActiveMQTopicSubscriber;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.jmx.ManagementContext; import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.command.ActiveMQTopic; import org.apache.activemq.command.ActiveMQTopic;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.fail; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQ3678Test implements MessageListener { public class AMQ3678Test implements MessageListener {
public int deliveryMode = DeliveryMode.NON_PERSISTENT; private static Logger LOG = LoggerFactory.getLogger(AMQ3678Test.class);
private BrokerService broker; private BrokerService broker;
private String connectionURI;
AtomicInteger messagesSent = new AtomicInteger(0); private final AtomicInteger messagesSent = new AtomicInteger(0);
AtomicInteger messagesReceived = new AtomicInteger(0); private final AtomicInteger messagesReceived = new AtomicInteger(0);
private final ActiveMQTopic destination = new ActiveMQTopic("XYZ");
private final CountDownLatch latch = new CountDownLatch(2);
private final int deliveryMode = DeliveryMode.NON_PERSISTENT;
ActiveMQTopic destination = new ActiveMQTopic("XYZ"); @Test(timeout = 60000)
int port;
int jmxport;
final CountDownLatch latch = new CountDownLatch(2);
public static void main(String[] args) throws Exception {
}
public static int findFreePort() throws IOException {
ServerSocket socket = null;
try {
// 0 is open a socket on any free port
socket = new ServerSocket(0);
return socket.getLocalPort();
} finally {
if (socket != null) {
socket.close();
}
}
}
@Test
public void countConsumers() throws JMSException { public void countConsumers() throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:" + port); ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionURI);
factory.setAlwaysSyncSend(true); factory.setAlwaysSyncSend(true);
factory.setDispatchAsync(false); factory.setDispatchAsync(false);
@ -95,13 +68,13 @@ public class AMQ3678Test implements MessageListener {
consumerConnection.setClientID("subscriber1"); consumerConnection.setClientID("subscriber1");
Session consumerMQSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Session consumerMQSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
ActiveMQTopicSubscriber activeConsumer = (ActiveMQTopicSubscriber) consumerMQSession.createDurableSubscriber(destination, "myTopic?consumer.prefetchSize=1"); ActiveMQTopicSubscriber activeConsumer = (ActiveMQTopicSubscriber) consumerMQSession.createDurableSubscriber(destination,
"myTopic?consumer.prefetchSize=1");
activeConsumer.setMessageListener(this); activeConsumer.setMessageListener(this);
consumerConnection.start(); consumerConnection.start();
final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final MessageProducer producer = producerSession.createProducer(destination); final MessageProducer producer = producerSession.createProducer(destination);
producer.setDeliveryMode(deliveryMode); producer.setDeliveryMode(deliveryMode);
@ -110,17 +83,14 @@ public class AMQ3678Test implements MessageListener {
private boolean done = false; private boolean done = false;
@Override
public void run() { public void run() {
while (!done) { while (!done) {
if (messagesSent.get() == 50) { if (messagesSent.get() == 50) {
try { try {
broker.getAdminView().removeTopic(destination.getTopicName()); broker.getAdminView().removeTopic(destination.getTopicName());
} catch (Exception e1) { } catch (Exception e1) {
// TODO Auto-generated catch block fail("Unable to remove destination:" + destination.getPhysicalName());
e1.printStackTrace();
System.err.flush();
fail("Unable to remove destination:"
+ destination.getPhysicalName());
} }
} }
@ -128,18 +98,15 @@ public class AMQ3678Test implements MessageListener {
producer.send(producerSession.createTextMessage()); producer.send(producerSession.createTextMessage());
int val = messagesSent.incrementAndGet(); int val = messagesSent.incrementAndGet();
System.out.println("sent message (" + val + ")"); LOG.trace("sent message (" + val + ")");
System.out.flush();
if (val == 100) { if (val == 100) {
done = true; done = true;
latch.countDown(); latch.countDown();
producer.close(); producer.close();
producerSession.close(); producerSession.close();
} }
} catch (JMSException e) { } catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -153,68 +120,42 @@ public class AMQ3678Test implements MessageListener {
fail("did not receive all the messages"); fail("did not receive all the messages");
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block
fail("did not receive all the messages, exception waiting for latch"); fail("did not receive all the messages, exception waiting for latch");
e.printStackTrace();
} }
//
} }
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
try {
port = findFreePort();
jmxport = findFreePort();
} catch (Exception e) {
fail("Unable to obtain a free port on which to start the broker");
}
System.out.println("Starting broker");
System.out.flush();
broker = new BrokerService(); broker = new BrokerService();
broker.setPersistent(false); broker.setPersistent(false);
ManagementContext ctx = new ManagementContext(ManagementFactory.getPlatformMBeanServer());
ctx.setConnectorPort(jmxport);
broker.setManagementContext(ctx);
broker.setUseJmx(true); broker.setUseJmx(true);
// broker.setAdvisorySupport(false); broker.getManagementContext().setCreateConnector(false);
// broker.setDeleteAllMessagesOnStartup(true); broker.setAdvisorySupport(false);
broker.addConnector("tcp://localhost:" + port).setName("Default"); TransportConnector connector = broker.addConnector("tcp://localhost:0");
broker.start(); broker.start();
broker.waitUntilStarted();
connectionURI = connector.getPublishableConnectString();
System.out.println("End of Broker Setup");
System.out.flush();
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
broker.stop(); broker.stop();
broker.waitUntilStopped();
} }
@Override @Override
public void onMessage(Message message) { public void onMessage(Message message) {
try { try {
message.acknowledge(); message.acknowledge();
int val = messagesReceived.incrementAndGet(); int val = messagesReceived.incrementAndGet();
System.out.println("received message (" + val + ")"); LOG.trace("received message (" + val + ")");
System.out.flush();
if (messagesReceived.get() == 100) { if (messagesReceived.get() == 100) {
latch.countDown(); latch.countDown();
} }
} catch (JMSException e) { } catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
} }