Search in sources :

Example 6 with FakeConsumer

use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.

the class QueueImplTest method testConsumerWithFilters.

// Private ------------------------------------------------------------------------------
private void testConsumerWithFilters(final boolean direct) throws Exception {
    QueueImpl queue = getTemporaryQueue();
    Filter filter = new FakeFilter("fruit", "orange");
    FakeConsumer consumer = new FakeConsumer(filter);
    if (direct) {
        queue.addConsumer(consumer);
    }
    List<MessageReference> refs = new ArrayList<>();
    MessageReference ref1 = generateReference(queue, 1);
    ref1.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("banana"));
    queue.addTail(ref1);
    MessageReference ref2 = generateReference(queue, 2);
    ref2.getMessage().putStringProperty(new SimpleString("cheese"), new SimpleString("stilton"));
    queue.addTail(ref2);
    MessageReference ref3 = generateReference(queue, 3);
    ref3.getMessage().putStringProperty(new SimpleString("cake"), new SimpleString("sponge"));
    queue.addTail(ref3);
    MessageReference ref4 = generateReference(queue, 4);
    ref4.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("orange"));
    refs.add(ref4);
    queue.addTail(ref4);
    MessageReference ref5 = generateReference(queue, 5);
    ref5.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("apple"));
    queue.addTail(ref5);
    MessageReference ref6 = generateReference(queue, 6);
    ref6.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("orange"));
    refs.add(ref6);
    queue.addTail(ref6);
    if (!direct) {
        queue.addConsumer(consumer);
        queue.deliverNow();
    }
    Assert.assertEquals(6, getMessageCount(queue));
    awaitExecution();
    Assert.assertEquals(2, consumer.getReferences().size());
    Assert.assertEquals(2, queue.getDeliveringCount());
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
    queue.acknowledge(ref5);
    queue.acknowledge(ref6);
    queue.removeConsumer(consumer);
    consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    queue.deliverNow();
    Assert.assertEquals(4, getMessageCount(queue));
    Assert.assertEquals(4, consumer.getReferences().size());
    Assert.assertEquals(4, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) Filter(org.apache.activemq.artemis.core.filter.Filter) FakeFilter(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) FakeFilter(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter)

Example 7 with FakeConsumer

use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.

the class QueueImplTest method testPauseAndResumeWithAsync.

/**
 * Test the paused and resumed states with async deliveries.
 *
 * @throws Exception
 */
@Test
public void testPauseAndResumeWithAsync() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    // pauses the queue
    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);
    }
    // even as this queue is paused, it will receive the messages anyway
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    // Now add a consumer
    FakeConsumer consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    Assert.assertTrue(consumer.getReferences().isEmpty());
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    // explicit order of delivery
    queue.deliverNow();
    // As the queue is paused, even an explicit order of delivery will not work.
    Assert.assertEquals(0, consumer.getReferences().size());
    Assert.assertEquals(numMessages, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    // resuming work
    queue.resume();
    awaitExecution();
    // after resuming the delivery begins.
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
    Assert.assertEquals(numMessages, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(numMessages, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 8 with FakeConsumer

use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.

the class QueueImplTest method testConsumerWithFilterThenAddMoreMessages.

@Test
public void testConsumerWithFilterThenAddMoreMessages() 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);
        ref.getMessage().putStringProperty("color", "red");
        refs.add(ref);
        queue.addTail(ref);
    }
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    queue.deliverNow();
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    for (int i = numMessages; i < numMessages * 2; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        ref.getMessage().putStringProperty("color", "green");
        queue.addTail(ref);
    }
    FakeConsumer consumer = new FakeConsumer(FilterImpl.createFilter("color = 'green'"));
    queue.addConsumer(consumer);
    queue.deliverNow();
    Assert.assertEquals(20, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(10, queue.getDeliveringCount());
    for (int i = numMessages * 2; i < numMessages * 3; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        ref.getMessage().putStringProperty("color", "green");
        queue.addTail(ref);
    }
    queue.deliverNow();
    Assert.assertEquals(20, consumer.getReferences().size());
    Assert.assertEquals(30, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(20, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 9 with FakeConsumer

use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.

the class QueueImplTest method testAddRemoveConsumer.

@Test
public void testAddRemoveConsumer() throws Exception {
    Consumer cons1 = new FakeConsumer();
    Consumer cons2 = new FakeConsumer();
    Consumer cons3 = new FakeConsumer();
    QueueImpl queue = getTemporaryQueue();
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.addConsumer(cons1);
    Assert.assertEquals(1, queue.getConsumerCount());
    queue.removeConsumer(cons1);
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.addConsumer(cons1);
    queue.addConsumer(cons2);
    queue.addConsumer(cons3);
    Assert.assertEquals(3, queue.getConsumerCount());
    queue.removeConsumer(new FakeConsumer());
    Assert.assertEquals(3, queue.getConsumerCount());
    queue.removeConsumer(cons1);
    Assert.assertEquals(2, queue.getConsumerCount());
    queue.removeConsumer(cons2);
    Assert.assertEquals(1, queue.getConsumerCount());
    queue.removeConsumer(cons3);
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.removeConsumer(cons3);
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) Consumer(org.apache.activemq.artemis.core.server.Consumer) FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 10 with FakeConsumer

use of org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer in project activemq-artemis by apache.

the class QueueImplTest method testRoundRobinWithQueueing.

@Test
public void testRoundRobinWithQueueing() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    final int numMessages = 10;
    List<MessageReference> refs = new ArrayList<>();
    queue.pause();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        queue.addTail(ref);
    }
    FakeConsumer cons1 = new FakeConsumer();
    FakeConsumer cons2 = new FakeConsumer();
    queue.addConsumer(cons1);
    queue.addConsumer(cons2);
    queue.resume();
    // Need to make sure the consumers will receive the messages before we do these assertions
    long timeout = System.currentTimeMillis() + 5000;
    while (cons1.getReferences().size() != numMessages / 2 && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    while (cons2.getReferences().size() != numMessages / 2 && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    Assert.assertEquals(numMessages / 2, cons1.getReferences().size());
    Assert.assertEquals(numMessages / 2, cons2.getReferences().size());
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref;
        ref = i % 2 == 0 ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
        Assert.assertEquals(refs.get(i), ref);
    }
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Aggregations

QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)21 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)21 Test (org.junit.Test)20 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)18 ArrayList (java.util.ArrayList)16 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)6 Filter (org.apache.activemq.artemis.core.filter.Filter)3 FakeFilter (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter)3 Consumer (org.apache.activemq.artemis.core.server.Consumer)2 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1