Search in sources :

Example 1 with TestConsumerTarget

use of org.apache.qpid.server.consumer.TestConsumerTarget in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testExclusiveConsumer.

public void testExclusiveConsumer() throws Exception {
    ServerMessage messageA = createMessage(new Long(24));
    // Check adding an exclusive consumer adds it to the queue
    _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(_consumerTarget, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.EXCLUSIVE, ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    assertEquals("Queue does not have consumer", 1, _queue.getConsumerCount());
    assertEquals("Queue does not have active consumer", 1, _queue.getConsumerCountWithCredit());
    // Check sending a message ends up with the subscriber
    _queue.enqueue(messageA, null, null);
    while (_consumerTarget.processPending()) ;
    assertEquals("Queue context did not see expected message", messageA, _consumer.getQueueContext().getLastSeenEntry().getMessage());
    // Check we cannot add a second subscriber to the queue
    TestConsumerTarget subB = new TestConsumerTarget();
    Exception ex = null;
    try {
        _queue.addConsumer(subB, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    } catch (MessageSource.ExistingExclusiveConsumer e) {
        ex = e;
    }
    assertNotNull(ex);
    // Check we cannot add an exclusive subscriber to a queue with an
    // existing consumer
    _consumer.close();
    _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(_consumerTarget, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    try {
        _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(subB, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.EXCLUSIVE), 0);
    } catch (MessageSource.ExistingConsumerPreventsExclusive e) {
        ex = e;
    }
    assertNotNull(ex);
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) MessageSource(org.apache.qpid.server.message.MessageSource) MessageDestinationIsAlternateException(org.apache.qpid.server.virtualhost.MessageDestinationIsAlternateException) UnknownAlternateBindingException(org.apache.qpid.server.virtualhost.UnknownAlternateBindingException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget)

Example 2 with TestConsumerTarget

use of org.apache.qpid.server.consumer.TestConsumerTarget in project qpid-broker-j by apache.

the class StandardQueueTest method testEnqueueDequeuedEntry.

/**
 * Tests that entry in dequeued state are not enqueued and not delivered to consumer
 */
public void testEnqueueDequeuedEntry() throws Exception {
    // create a queue where each even entry is considered a dequeued
    AbstractQueue queue = new DequeuedQueue(getVirtualHost());
    queue.create();
    // create a consumer
    TestConsumerTarget consumer = new TestConsumerTarget();
    // register consumer
    queue.addConsumer(consumer, null, createMessage(-1l).getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    // put test messages into a queue
    putGivenNumberOfMessages(queue, 4);
    while (consumer.processPending()) ;
    // assert received messages
    List<MessageInstance> messages = consumer.getMessages();
    assertEquals("Only 2 messages should be returned", 2, messages.size());
    assertEquals("ID of first message should be 1", 1l, (messages.get(0).getMessage()).getMessageNumber());
    assertEquals("ID of second message should be 3", 3l, (messages.get(1).getMessage()).getMessageNumber());
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget)

Example 3 with TestConsumerTarget

use of org.apache.qpid.server.consumer.TestConsumerTarget in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testReleaseMessageThatBecomesExpiredIsNotRedelivered.

/**
 * Tests that a released message that becomes expired is not resent to the subscriber.
 * This tests ensures that SimpleAMQQueue<?>Entry.getNextAvailableEntry avoids expired entries.
 * Verifies also that the QueueContext._releasedEntry is reset to null after the entry has been reset.
 */
public void testReleaseMessageThatBecomesExpiredIsNotRedelivered() throws Exception {
    ServerMessage messageA = createMessage(new Long(24));
    final CountDownLatch sendIndicator = new CountDownLatch(1);
    _consumerTarget = new TestConsumerTarget() {

        @Override
        public void notifyWork() {
            while (processPending()) ;
        }

        @Override
        public void send(MessageInstanceConsumer consumer, MessageInstance entry, boolean batch) {
            try {
                super.send(consumer, entry, batch);
            } finally {
                sendIndicator.countDown();
            }
        }
    };
    _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(_consumerTarget, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.SEES_REQUEUES, ConsumerOption.ACQUIRES), 0);
    final ArrayList<QueueEntry> queueEntries = new ArrayList<QueueEntry>();
    EntryListAddingAction postEnqueueAction = new EntryListAddingAction(queueEntries);
    /* Enqueue one message with expiration set for a short time in the future */
    final long expiration = System.currentTimeMillis() + 100L;
    when(messageA.getExpiration()).thenReturn(expiration);
    _queue.enqueue(messageA, postEnqueueAction, null);
    assertTrue("Message was not sent during expected time interval", sendIndicator.await(5000, TimeUnit.MILLISECONDS));
    assertEquals("Unexpected total number of messages sent to consumer", 1, _consumerTarget.getMessages().size());
    QueueEntry queueEntry = queueEntries.get(0);
    final CountDownLatch dequeueIndicator = new CountDownLatch(1);
    queueEntry.addStateChangeListener(new StateChangeListener<MessageInstance, MessageInstance.EntryState>() {

        @Override
        public void stateChanged(MessageInstance object, MessageInstance.EntryState oldState, MessageInstance.EntryState newState) {
            if (newState.equals(MessageInstance.DEQUEUED_STATE)) {
                dequeueIndicator.countDown();
            }
        }
    });
    assertFalse("Redelivery flag should not be set", queueEntry.isRedelivered());
    /* Wait a little more to be sure that message will have expired, then release the first message only, causing it to be requeued */
    while (!queueEntry.expired() && System.currentTimeMillis() <= expiration) {
        Thread.sleep(10);
    }
    assertTrue("Expecting the queue entry to be now expired", queueEntry.expired());
    queueEntry.release();
    assertTrue("Message was not de-queued due to expiration", dequeueIndicator.await(5000, TimeUnit.MILLISECONDS));
    assertEquals("Total number of messages sent should not have changed", 1, _consumerTarget.getMessages().size());
    assertFalse("Redelivery flag should not be set", queueEntry.isRedelivered());
    // QueueContext#_releasedEntry is updated after notification, thus, we need to make sure that it is updated
    long waitLoopLimit = 10;
    while (_consumer.getQueueContext().getReleasedEntry() != null && waitLoopLimit-- > 0) {
        Thread.sleep(10);
    }
    assertNull("releasedEntry should be cleared after requeue processed:" + _consumer.getQueueContext().getReleasedEntry(), _consumer.getQueueContext().getReleasedEntry());
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) ArrayList(java.util.ArrayList) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget) MessageInstance(org.apache.qpid.server.message.MessageInstance)

Example 4 with TestConsumerTarget

use of org.apache.qpid.server.consumer.TestConsumerTarget in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testReleaseForQueueWithMultipleConsumers.

/**
 * Tests that a release requeues an entry for a queue with multiple consumers.  Verifies that a
 * requeue resends a message to a <i>single</i> subscriber.
 */
public void testReleaseForQueueWithMultipleConsumers() throws Exception {
    ServerMessage messageA = createMessage(new Long(24));
    ServerMessage messageB = createMessage(new Long(25));
    TestConsumerTarget target1 = new TestConsumerTarget();
    TestConsumerTarget target2 = new TestConsumerTarget();
    QueueConsumer consumer1 = (QueueConsumer) _queue.addConsumer(target1, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    QueueConsumer consumer2 = (QueueConsumer) _queue.addConsumer(target2, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    final ArrayList<QueueEntry> queueEntries = new ArrayList<QueueEntry>();
    EntryListAddingAction postEnqueueAction = new EntryListAddingAction(queueEntries);
    /* Enqueue two messages */
    _queue.enqueue(messageA, postEnqueueAction, null);
    _queue.enqueue(messageB, postEnqueueAction, null);
    while (target1.processPending()) ;
    while (target2.processPending()) ;
    assertEquals("Unexpected total number of messages sent to both after enqueue", 2, target1.getMessages().size() + target2.getMessages().size());
    /* Now release the first message only, causing it to be requeued */
    queueEntries.get(0).release();
    while (target1.processPending()) ;
    while (target2.processPending()) ;
    assertEquals("Unexpected total number of messages sent to both consumers after release", 3, target1.getMessages().size() + target2.getMessages().size());
    assertNull("releasedEntry should be cleared after requeue processed", consumer1.getQueueContext().getReleasedEntry());
    assertNull("releasedEntry should be cleared after requeue processed", consumer2.getQueueContext().getReleasedEntry());
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) ArrayList(java.util.ArrayList) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget)

Example 5 with TestConsumerTarget

use of org.apache.qpid.server.consumer.TestConsumerTarget in project qpid-broker-j by apache.

the class StandardQueueTest method testProcessQueueWithDequeuedEntry.

/**
 * Tests whether dequeued entry is sent to subscriber
 */
public void testProcessQueueWithDequeuedEntry() throws Exception {
    // total number of messages to send
    int messageNumber = 4;
    int dequeueMessageIndex = 1;
    Map<String, Object> queueAttributes = new HashMap<>();
    queueAttributes.put(Queue.NAME, "test");
    // create queue with overridden method deliverAsync
    StandardQueueImpl testQueue = new StandardQueueImpl(queueAttributes, getVirtualHost());
    testQueue.create();
    // put messages
    List<StandardQueueEntry> entries = (List<StandardQueueEntry>) enqueueGivenNumberOfMessages(testQueue, messageNumber);
    // dequeue message
    dequeueMessage(testQueue, dequeueMessageIndex);
    // latch to wait for message receipt
    final CountDownLatch latch = new CountDownLatch(messageNumber - 1);
    // create a consumer
    TestConsumerTarget consumer = new TestConsumerTarget() {

        @Override
        public void notifyWork() {
            while (processPending()) ;
        }

        /**
         * Send a message and decrement latch
         * @param consumer
         * @param entry
         * @param batch
         */
        @Override
        public void send(final MessageInstanceConsumer consumer, MessageInstance entry, boolean batch) {
            super.send(consumer, entry, batch);
            latch.countDown();
        }
    };
    // subscribe
    testQueue.addConsumer(consumer, null, entries.get(0).getMessage().getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    // wait up to 1 minute for message receipt
    try {
        latch.await(1, TimeUnit.MINUTES);
    } catch (InterruptedException e1) {
        Thread.currentThread().interrupt();
    }
    List<MessageInstance> expected = Arrays.asList((MessageInstance) entries.get(0), entries.get(2), entries.get(3));
    verifyReceivedMessages(expected, consumer.getMessages());
}
Also used : HashMap(java.util.HashMap) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget) MessageInstance(org.apache.qpid.server.message.MessageInstance) List(java.util.List)

Aggregations

TestConsumerTarget (org.apache.qpid.server.consumer.TestConsumerTarget)5 MessageInstance (org.apache.qpid.server.message.MessageInstance)3 ServerMessage (org.apache.qpid.server.message.ServerMessage)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 MessageInstanceConsumer (org.apache.qpid.server.message.MessageInstanceConsumer)2 HashMap (java.util.HashMap)1 List (java.util.List)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1 MessageSource (org.apache.qpid.server.message.MessageSource)1 MessageDestinationIsAlternateException (org.apache.qpid.server.virtualhost.MessageDestinationIsAlternateException)1 UnknownAlternateBindingException (org.apache.qpid.server.virtualhost.UnknownAlternateBindingException)1