Updating testcase.. this new version fails very fast now.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@679849 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2008-07-25 16:13:20 +00:00
parent ec35afa9ea
commit 08e01e7fb4
1 changed files with 59 additions and 51 deletions

View File

@ -30,6 +30,8 @@ import javax.jms.TextMessage;
import junit.framework.TestCase; 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.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.policy.PolicyMap;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -58,6 +60,15 @@ public class AMQ1866 extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
// Start an embedded broker up. // Start an embedded broker up.
brokerService = new BrokerService(); brokerService = new BrokerService();
brokerService.deleteAllMessages();
// A small max page size makes this issue occur faster.
PolicyMap policyMap = new PolicyMap();
PolicyEntry pe = new PolicyEntry();
pe.setMaxPageSize(1);
policyMap.put(new ActiveMQQueue(">"), pe);
brokerService.setDestinationPolicy(policyMap);
brokerService.addConnector(ACTIVEMQ_BROKER_BIND); brokerService.addConnector(ACTIVEMQ_BROKER_BIND);
brokerService.start(); brokerService.start();
destination = new ActiveMQQueue(getName()); destination = new ActiveMQQueue(getName());
@ -68,52 +79,65 @@ public class AMQ1866 extends TestCase {
// Stop any running threads. // Stop any running threads.
shutdown.set(true); shutdown.set(true);
for (Thread t : threads) { for (Thread t : threads) {
t.interrupt();
t.join(); t.join();
} }
brokerService.stop(); brokerService.stop();
} }
// Failing // Failing
public void testConsumerSlowDownPrefetch0() throws InterruptedException { public void testConsumerSlowDownPrefetch0() throws Exception {
ACTIVEMQ_BROKER_URI = "tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=0"; ACTIVEMQ_BROKER_URI = "tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=0";
doTestConsumerSlowDown(); doTestConsumerSlowDown();
} }
// Failing // Failing
public void testConsumerSlowDownPrefetch10() throws InterruptedException { public void testConsumerSlowDownPrefetch10() throws Exception {
ACTIVEMQ_BROKER_URI = "tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=10"; ACTIVEMQ_BROKER_URI = "tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=10";
doTestConsumerSlowDown(); doTestConsumerSlowDown();
} }
// Passing // Passing
public void testConsumerSlowDownDefaultPrefetch() throws InterruptedException { public void testConsumerSlowDownDefaultPrefetch() throws Exception {
ACTIVEMQ_BROKER_URI = "tcp://localhost:61616"; ACTIVEMQ_BROKER_URI = "tcp://localhost:61616";
doTestConsumerSlowDown(); doTestConsumerSlowDown();
} }
public void doTestConsumerSlowDown() throws InterruptedException { public void doTestConsumerSlowDown() throws Exception {
ProducerThread p1 = new ProducerThread("Producer-1");
threads.add(p1);
p1.start();
// Wait a bit before starting the consumers to load up the queues a bit.. // Preload the queue.
// If the queue is loaded up it seems that the even the Default Prefetch size case fails. produce(20000);
Thread.sleep(10000);
Thread producer = new Thread() {
@Override
public void run() {
try {
while(!shutdown.get()) {
produce(1000);
}
} catch (Exception e) {
}
}
};
threads.add(producer);
producer.start();
// This is the slow consumer.
ConsumerThread c1 = new ConsumerThread("Consumer-1"); ConsumerThread c1 = new ConsumerThread("Consumer-1");
threads.add(c1); threads.add(c1);
c1.start(); c1.start();
// Wait a bit so that the slow consumer gets assigned most of the messages.
Thread.sleep(500);
ConsumerThread c2 = new ConsumerThread("Consumer-2"); ConsumerThread c2 = new ConsumerThread("Consumer-2");
threads.add(c2); threads.add(c2);
c2.start(); c2.start();
for ( int i=0; i < 30; i++) { for ( int i=0; i < 30; i++) {
Thread.sleep(1000); Thread.sleep(1000);
long p1Counter = p1.counter.getAndSet(0);
long c1Counter = c1.counter.getAndSet(0); long c1Counter = c1.counter.getAndSet(0);
long c2Counter = c2.counter.getAndSet(0); long c2Counter = c2.counter.getAndSet(0);
System.out.println("p1: "+p1Counter+", c1: "+c1Counter+", c2: "+c2Counter); System.out.println("c1: "+c1Counter+", c2: "+c2Counter);
// Once message have been flowing for a few seconds, start asserting that c2 always gets messages. It should be receiving about 100 / sec // Once message have been flowing for a few seconds, start asserting that c2 always gets messages. It should be receiving about 100 / sec
if( i > 3 ) { if( i > 3 ) {
@ -122,46 +146,30 @@ public class AMQ1866 extends TestCase {
} }
} }
public class ProducerThread extends Thread { public void produce(int count) throws Exception {
final AtomicLong counter = new AtomicLong(); Connection connection=null;
try {
public ProducerThread(String threadId) { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_BROKER_URI);
super(threadId); factory.setDispatchAsync(true);
}
connection = factory.createConnection();
public void run() {
Connection connection=null; Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
connection.start();
for( int i=0 ; i< count; i++ ) {
producer.send(session.createTextMessage(getName()+" Message "+(++i)));
}
} finally {
try { try {
log.debug(getName() + ": is running"); connection.close();
} catch (Throwable e) {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_BROKER_URI);
factory.setDispatchAsync(true);
connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
connection.start();
int i = 0;
while (!shutdown.get()) {
producer.send(session.createTextMessage(getName()+" Message "+(++i)));
counter.incrementAndGet();
Thread.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Throwable e) {
}
} }
} }
} }
public class ConsumerThread extends Thread { public class ConsumerThread extends Thread {
final AtomicLong counter = new AtomicLong(); final AtomicLong counter = new AtomicLong();
@ -188,7 +196,7 @@ public class AMQ1866 extends TestCase {
if ( msg!=null ) { if ( msg!=null ) {
int sleepingTime; int sleepingTime;
if (getName().equals("Consumer-1")) { if (getName().equals("Consumer-1")) {
sleepingTime = 10 * 1000; sleepingTime = 1000 * 1000;
} else { } else {
sleepingTime = 1; sleepingTime = 1;
} }
@ -198,8 +206,8 @@ public class AMQ1866 extends TestCase {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} finally { } finally {
log.debug(getName() + ": is stopping");
try { try {
connection.close(); connection.close();
} catch (Throwable e) { } catch (Throwable e) {