The QueueDepth should decrease when messages are delivered from the queue (QPID-408)
*/
public void testQueueDepthAlertWithSubscribers() throws Exception
{
protocolSession = new TestMinaProtocolSession();
AMQChannel channel = new AMQChannel(protocolSession, 2, _messageStore, null);
protocolSession.addChannel(channel);
// Create queue
_queue = getNewQueue();
_queue.registerProtocolSession(protocolSession, channel.getChannelId(),
new AMQShortString("consumer_tag"), true, null, false, false);
_queueMBean = (AMQQueueMBean) _queue.getManagedObject();
_queueMBean.setMaximumMessageCount(9999l); // Set a high value, because this is not being tested
_queueMBean.setMaximumQueueDepth(MAX_QUEUE_DEPTH);
// Send messages(no of message to be little more than what can cause a Queue_Depth alert)
int messageCount = Math.round(MAX_QUEUE_DEPTH / MAX_MESSAGE_SIZE) + 10;
long totalSize = (messageCount * MAX_MESSAGE_SIZE) >> 10;
sendMessages(messageCount, MAX_MESSAGE_SIZE);
// Check queueDepth. There should be no messages on the queue and as the subscriber is listening
// so there should be no Queue_Deoth alert raised
assertEquals(new Long(0), new Long(_queueMBean.getQueueDepth()));
Notification lastNotification = _queueMBean.getLastNotification();
assertNull(lastNotification);
// Kill the subscriber and check for the queue depth values.
// Messages are unacknowledged, so those should get requeued. All messages should be on the Queue
_queue.unregisterProtocolSession(protocolSession, channel.getChannelId(), new AMQShortString("consumer_tag"));
channel.requeue();
assertEquals(new Long(totalSize), new Long(_queueMBean.getQueueDepth()));
lastNotification = _queueMBean.getLastNotification();
assertNotNull(lastNotification);
String notificationMsg = lastNotification.getMessage();
assertTrue(notificationMsg.startsWith(NotificationCheck.QUEUE_DEPTH_ALERT.name()));
// Connect a consumer again and check QueueDepth values. The queue should get emptied.
// Messages will get delivered but still are unacknowledged.
_queue.registerProtocolSession(protocolSession, channel.getChannelId(),
new AMQShortString("consumer_tag"), true, null, false, false);
_queue.deliverAsync();
while (_queue.getMessageCount() != 0)
{
Thread.sleep(100);
}
assertEquals(new Long(0), new Long(_queueMBean.getQueueDepth()));
// Kill the subscriber again. Now those messages should get requeued again. Check if the queue depth
// value is correct.
_queue.unregisterProtocolSession(protocolSession, channel.getChannelId(), new AMQShortString("consumer_tag"));
channel.requeue();
assertEquals(new Long(totalSize), new Long(_queueMBean.getQueueDepth()));
protocolSession.closeSession();
// Check the clear queue