Source:
public void testMsgDeliveryPool() throws Exception { MsgDeliveryPool pool = new MsgDeliveryPool(); pool.setSize(-2); assertEquals(0, pool.getSize()); pool.setSize(0); assertEquals(0, pool.getSize()); pool.setSize(5); assertEquals(5, pool.getSize()); pool.setSize(2); assertEquals(5, pool.getSize()); pool.setSize(7); assertEquals(7, pool.getSize()); final AtomicInteger received = new AtomicInteger(0); Options opts = Nats.defaultOptions(); ConnectionImpl nc = new ConnectionImpl(opts); AsyncSubscriptionImpl sub1 = new AsyncSubscriptionImpl(nc, "foo", null, new MessageHandler() { public void onMessage(Message msg) { received.incrementAndGet(); } }); pool.assignDeliveryWorker(sub1); MsgDeliveryWorker worker1 = sub1.getDeliveryWorker(); assertNotNull(worker1); sub1.lock(); sub1.max = 2; sub1.unlock(); for (int i=0; i<3; i++) { final Message msg = new Message("hello".getBytes(), "foo", null, sub1); worker1.lock(); worker1.postMsg(msg); worker1.unlock(); } Thread.sleep(100); assertEquals(2, received.get()); received.set(0); AsyncSubscriptionImpl sub2 = new AsyncSubscriptionImpl(nc, "foo", null, new MessageHandler(){ public void onMessage(Message msg) { received.incrementAndGet(); SubscriptionImpl sub = (SubscriptionImpl) msg.getSubscription(); sub.lock(); sub.closed = true; sub.unlock(); } }); pool.assignDeliveryWorker(sub2); MsgDeliveryWorker worker2 = sub2.getDeliveryWorker(); assertNotNull(worker2); assertNotEquals(worker1, worker2); for (int i=0; i<2; i++) { final Message msg = new Message("hello".getBytes(), "foo", null, sub2); worker2.lock(); worker2.postMsg(msg); worker2.unlock(); } Thread.sleep(100); assertEquals(1, received.get()); pool.shutdown(); assertEquals(0, pool.getSize()); pool.shutdown(); assertEquals(0, pool.getSize()); pool.setSize(1); pool.assignDeliveryWorker(sub1); pool.assignDeliveryWorker(sub2); assertEquals(sub1.getDeliveryWorker(), sub2.getDeliveryWorker()); }

Target:
public void testMsgDeliveryPool() throws Exception { MsgDeliveryPool pool = new MsgDeliveryPool(5); assertEquals(5, pool.getSize()); final AtomicInteger received = new AtomicInteger(0); Options opts = Nats.defaultOptions(); ConnectionImpl nc = new ConnectionImpl(opts); AsyncSubscriptionImpl sub1 = new AsyncSubscriptionImpl(nc, "foo", null, new MessageHandler() { public void onMessage(Message msg) { received.incrementAndGet(); } }); pool.assignDeliveryWorker(sub1); MsgDeliveryWorker worker1 = sub1.getDeliveryWorker(); assertNotNull(worker1); sub1.lock(); sub1.max = 2; sub1.unlock(); for (int i=0; i<3; i++) { final Message msg = new Message("hello".getBytes(), "foo", null, sub1); worker1.lock(); worker1.postMsg(msg); worker1.unlock(); } Thread.sleep(100); assertEquals(2, received.get()); received.set(0); AsyncSubscriptionImpl sub2 = new AsyncSubscriptionImpl(nc, "foo", null, new MessageHandler(){ public void onMessage(Message msg) { received.incrementAndGet(); SubscriptionImpl sub = (SubscriptionImpl) msg.getSubscription(); sub.lock(); sub.closed = true; sub.unlock(); } }); pool.assignDeliveryWorker(sub2); MsgDeliveryWorker worker2 = sub2.getDeliveryWorker(); assertNotNull(worker2); assertNotEquals(worker1, worker2); for (int i=0; i<2; i++) { final Message msg = new Message("hello".getBytes(), "foo", null, sub2); worker2.lock(); worker2.postMsg(msg); worker2.unlock(); } Thread.sleep(100); assertEquals(1, received.get()); pool.shutdown(); assertEquals(0, pool.getSize()); pool.shutdown(); assertEquals(0, pool.getSize()); pool = new MsgDeliveryPool(1); pool.assignDeliveryWorker(sub1); pool.assignDeliveryWorker(sub2); assertEquals(sub1.getDeliveryWorker(), sub2.getDeliveryWorker()); pool.shutdown(); }