use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.
the class QueueImplTest method testPauseAndResumeWithDirect.
/**
* Test the paused and resumed states with direct deliveries.
*
* @throws Exception
*/
@Test
public void testPauseAndResumeWithDirect() throws Exception {
QueueImpl queue = getTemporaryQueue();
// Now add a consumer
FakeConsumer consumer = new FakeConsumer();
queue.addConsumer(consumer);
// brings to queue to paused state
queue.pause();
final int numMessages = 10;
List<MessageReference> refs = new ArrayList<>();
for (int i = 0; i < numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
// the queue even if it's paused will receive the message but won't forward
// directly to the consumer until resumed.
Assert.assertEquals(numMessages, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(0, queue.getDeliveringCount());
Assert.assertTrue(consumer.getReferences().isEmpty());
// brings the queue to resumed state.
queue.resume();
awaitExecution();
// resuming delivery of messages
assertRefListsIdenticalRefs(refs, consumer.getReferences());
Assert.assertEquals(numMessages, getMessageCount(queue));
Assert.assertEquals(numMessages, queue.getDeliveringCount());
}
use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.
the class QueueImplTest method testChangeConsumersAndDeliver.
@Test
public void testChangeConsumersAndDeliver() throws Exception {
QueueImpl queue = getTemporaryQueue();
final int numMessages = 10;
List<MessageReference> refs = new ArrayList<>();
for (int i = 0; i < numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
Assert.assertEquals(numMessages, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(0, queue.getDeliveringCount());
FakeConsumer cons1 = new FakeConsumer();
queue.addConsumer(cons1);
queue.deliverNow();
Assert.assertEquals(numMessages, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(numMessages, queue.getDeliveringCount());
assertRefListsIdenticalRefs(refs, cons1.getReferences());
FakeConsumer cons2 = new FakeConsumer();
queue.addConsumer(cons2);
Assert.assertEquals(2, queue.getConsumerCount());
cons1.getReferences().clear();
for (MessageReference ref : refs) {
queue.acknowledge(ref);
}
refs.clear();
for (int i = 0; i < 2 * numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
queue.deliverNow();
Assert.assertEquals(numMessages * 2, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(numMessages * 2, queue.getDeliveringCount());
Assert.assertEquals(numMessages, cons1.getReferences().size());
Assert.assertEquals(numMessages, cons2.getReferences().size());
cons1.getReferences().clear();
cons2.getReferences().clear();
for (MessageReference ref : refs) {
queue.acknowledge(ref);
}
refs.clear();
FakeConsumer cons3 = new FakeConsumer();
queue.addConsumer(cons3);
Assert.assertEquals(3, queue.getConsumerCount());
for (int i = 0; i < 3 * numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
queue.deliverNow();
Assert.assertEquals(numMessages * 3, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(numMessages * 3, queue.getDeliveringCount());
Assert.assertEquals(numMessages, cons1.getReferences().size());
Assert.assertEquals(numMessages, cons2.getReferences().size());
Assert.assertEquals(numMessages, cons3.getReferences().size());
queue.removeConsumer(cons1);
cons3.getReferences().clear();
cons2.getReferences().clear();
for (MessageReference ref : refs) {
queue.acknowledge(ref);
}
refs.clear();
for (int i = 0; i < 2 * numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
queue.deliverNow();
Assert.assertEquals(numMessages * 2, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(numMessages * 2, queue.getDeliveringCount());
Assert.assertEquals(numMessages, cons2.getReferences().size());
Assert.assertEquals(numMessages, cons3.getReferences().size());
queue.removeConsumer(cons3);
cons2.getReferences().clear();
for (MessageReference ref : refs) {
queue.acknowledge(ref);
}
refs.clear();
for (int i = 0; i < numMessages; i++) {
MessageReference ref = generateReference(queue, i);
refs.add(ref);
queue.addTail(ref);
}
queue.deliverNow();
Assert.assertEquals(numMessages, getMessageCount(queue));
Assert.assertEquals(0, queue.getScheduledCount());
Assert.assertEquals(numMessages, queue.getDeliveringCount());
Assert.assertEquals(numMessages, cons2.getReferences().size());
}
use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.
the class QueueImplTest method testScheduledNoConsumer.
// The tests ----------------------------------------------------------------
@Test
public void testScheduledNoConsumer() throws Exception {
QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), new SimpleString("queue1"), null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
// Send one scheduled
long now = System.currentTimeMillis();
MessageReference ref1 = generateReference(queue, 1);
ref1.setScheduledDeliveryTime(now + 700);
queue.addTail(ref1);
// Send some non scheduled messages
MessageReference ref2 = generateReference(queue, 2);
queue.addTail(ref2);
MessageReference ref3 = generateReference(queue, 3);
queue.addTail(ref3);
MessageReference ref4 = generateReference(queue, 4);
queue.addTail(ref4);
// Now send some more scheduled messages
MessageReference ref5 = generateReference(queue, 5);
ref5.setScheduledDeliveryTime(now + 500);
queue.addTail(ref5);
MessageReference ref6 = generateReference(queue, 6);
ref6.setScheduledDeliveryTime(now + 400);
queue.addTail(ref6);
MessageReference ref7 = generateReference(queue, 7);
ref7.setScheduledDeliveryTime(now + 300);
queue.addTail(ref7);
MessageReference ref8 = generateReference(queue, 8);
ref8.setScheduledDeliveryTime(now + 600);
queue.addTail(ref8);
List<MessageReference> refs = new ArrayList<>();
// Scheduled refs are added back to *FRONT* of queue - otherwise if there were many messages in the queue
// They may get stranded behind a big backlog
refs.add(ref1);
refs.add(ref8);
refs.add(ref5);
refs.add(ref6);
refs.add(ref7);
refs.add(ref2);
refs.add(ref3);
refs.add(ref4);
Thread.sleep(750);
FakeConsumer consumer = new FakeConsumer();
queue.addConsumer(consumer);
queue.deliverNow();
assertRefListsIdenticalRefs(refs, consumer.getReferences());
}
use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.
the class QueueImplTest method testDeliveryScheduled.
@Test
public void testDeliveryScheduled() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
Consumer consumer = new FakeConsumer() {
@Override
public synchronized HandleStatus handle(final MessageReference reference) {
countDownLatch.countDown();
return HandleStatus.HANDLED;
}
@Override
public void disconnect() {
}
};
QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), QueueImplTest.queue1, null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
MessageReference messageReference = generateReference(queue, 1);
queue.addConsumer(consumer);
messageReference.setScheduledDeliveryTime(System.currentTimeMillis() + 200);
queue.addHead(messageReference, false);
boolean gotLatch = countDownLatch.await(3000, TimeUnit.MILLISECONDS);
Assert.assertTrue(gotLatch);
}
use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.
the class QueueImplTest method testScheduled.
@Test
public void testScheduled() throws Exception {
QueueImpl queue = new QueueImpl(1, new SimpleString("address1"), new SimpleString("queue1"), null, null, false, true, false, scheduledExecutor, null, null, null, ArtemisExecutor.delegate(executor), null, null);
FakeConsumer consumer = null;
// Send one scheduled
long now = System.currentTimeMillis();
MessageReference ref1 = generateReference(queue, 1);
ref1.setScheduledDeliveryTime(now + 700);
queue.addTail(ref1);
// Send some non scheduled messages
MessageReference ref2 = generateReference(queue, 2);
queue.addTail(ref2);
MessageReference ref3 = generateReference(queue, 3);
queue.addTail(ref3);
MessageReference ref4 = generateReference(queue, 4);
queue.addTail(ref4);
// Now send some more scheduled messages
MessageReference ref5 = generateReference(queue, 5);
ref5.setScheduledDeliveryTime(now + 500);
queue.addTail(ref5);
MessageReference ref6 = generateReference(queue, 6);
ref6.setScheduledDeliveryTime(now + 400);
queue.addTail(ref6);
MessageReference ref7 = generateReference(queue, 7);
ref7.setScheduledDeliveryTime(now + 300);
queue.addTail(ref7);
MessageReference ref8 = generateReference(queue, 8);
ref8.setScheduledDeliveryTime(now + 600);
queue.addTail(ref8);
consumer = new FakeConsumer();
queue.addConsumer(consumer);
queue.deliverNow();
List<MessageReference> refs = new ArrayList<>();
refs.add(ref2);
refs.add(ref3);
refs.add(ref4);
assertRefListsIdenticalRefs(refs, consumer.getReferences());
refs.clear();
consumer.getReferences().clear();
MessageReference ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
Assert.assertEquals(ref7, ref);
long now2 = System.currentTimeMillis();
Assert.assertTrue(now2 - now >= 300);
ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
Assert.assertEquals(ref6, ref);
now2 = System.currentTimeMillis();
Assert.assertTrue(now2 - now >= 400);
ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
Assert.assertEquals(ref5, ref);
now2 = System.currentTimeMillis();
Assert.assertTrue(now2 - now >= 500);
ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
Assert.assertEquals(ref8, ref);
now2 = System.currentTimeMillis();
Assert.assertTrue(now2 - now >= 600);
ref = consumer.waitForNextReference(QueueImplTest.TIMEOUT);
Assert.assertEquals(ref1, ref);
now2 = System.currentTimeMillis();
Assert.assertTrue(now2 - now >= 700);
Assert.assertTrue(consumer.getReferences().isEmpty());
}
Aggregations