queueAttributes.put(Queue.NAME, "testActiveConsumerCount");
queueAttributes.put(Queue.OWNER, "testOwner");
final StandardQueueImpl queue = new StandardQueueImpl(queueAttributes, getVirtualHost());
queue.open();
//verify adding an active consumer increases the count
final MockConsumer consumer1 = new MockConsumer();
consumer1.setActive(true);
consumer1.setState(ConsumerTarget.State.ACTIVE);
assertEquals("Unexpected active consumer count", 0, queue.getConsumerCountWithCredit());
queue.addConsumer(consumer1,
null,
createMessage(-1l).getClass(),
"test",
EnumSet.of(ConsumerImpl.Option.ACQUIRES,
ConsumerImpl.Option.SEES_REQUEUES));
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());
//verify adding an inactive consumer doesn't increase the count
final MockConsumer consumer2 = new MockConsumer();
consumer2.setActive(false);
consumer2.setState(ConsumerTarget.State.SUSPENDED);
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());
queue.addConsumer(consumer2,
null,
createMessage(-1l).getClass(),
"test",
EnumSet.of(ConsumerImpl.Option.ACQUIRES,
ConsumerImpl.Option.SEES_REQUEUES));
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());
//verify behaviour in face of expected state changes:
//verify a consumer going suspended->active increases the count
consumer2.setState(ConsumerTarget.State.ACTIVE);
assertEquals("Unexpected active consumer count", 2, queue.getConsumerCountWithCredit());
//verify a consumer going active->suspended decreases the count
consumer2.setState(ConsumerTarget.State.SUSPENDED);
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());
//verify a consumer going suspended->closed doesn't change the count
consumer2.setState(ConsumerTarget.State.CLOSED);
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());
//verify a consumer going active->active doesn't change the count
consumer1.setState(ConsumerTarget.State.ACTIVE);
assertEquals("Unexpected active consumer count", 1, queue.getConsumerCountWithCredit());