Queue queue = new Queue(1, ms, pm, true, true, 100, 20, 10, new QueuedExecutor(), null);
Queue queue = new Queue(1, ms, pm, true, true, 100, 20, 10, new QueuedExecutor());
   public void test1() throws Throwable   {      Queue queue = new Queue(1, ms, pm, true, true, 100, 20, 10, new QueuedExecutor(), null);                          Message[] msgs = new Message[241];            MessageReference[] refs = new MessageReference[241];        //Send 99            Transaction tx = tr.createTransaction();            for (int i = 0; i < 99; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);                         queue.handle(null, refs[i], tx);          refs[i].releaseMemoryReference();      }      tx.commit();            //verify no refs in storage                  List refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertTrue(refIds.isEmpty());            //Verify no msgs in storage      List msgIds = getMessageIds();      assertTrue(msgIds.isEmpty());            //Verify 99 msgs in store      assertEquals(99, ms.size());            //Verify 99 refs in queue      assertEquals(99, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify not paging      assertFalse(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                  //Send one more ref            tx = tr.createTransaction();            msgs[99] = CoreMessageFactory.createCoreMessage(99, false, null);      refs[99] = ms.reference(msgs[99]);      queue.handle(null, refs[99], tx);      refs[99].releaseMemoryReference();            tx.commit();            //verify no refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertTrue(refIds.isEmpty());            //Verify no msgs in storage      msgIds = getMessageIds();      assertTrue(msgIds.isEmpty());            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());                  //Verify paging      assertTrue(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                        //Send 9 more            tx = tr.createTransaction();      for (int i = 100; i < 109; i++)      {                  msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);          refs[i].releaseMemoryReference();      }      tx.commit();            //verify no refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertTrue(refIds.isEmpty());            //Verify no msgs in storage      msgIds = getMessageIds();      assertTrue(msgIds.isEmpty());            //Verify 109 msgs in store      assertEquals(109, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 9 refs in downcache      assertEquals(9, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Send one more ref - should clear the down cache            tx = tr.createTransaction();      msgs[109] = CoreMessageFactory.createCoreMessage(109, false, null);      refs[109] = ms.reference(msgs[109]);      queue.handle(null, refs[109], tx);      refs[109].releaseMemoryReference();      tx.commit();            //verify 10 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(10, refIds.size());      assertSameIds(refIds, refs, 100, 109);            //Verify 10 msgs in storage      msgIds = getMessageIds();      assertEquals(10, msgIds.size());       assertSameIds(msgIds, refs, 100, 109);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Send one more ref            tx = tr.createTransaction();      msgs[110] = CoreMessageFactory.createCoreMessage(110, false, null);      refs[110] = ms.reference(msgs[110]);      queue.handle(null, refs[110], tx);      refs[110].releaseMemoryReference();      tx.commit();            //verify 10 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(10, refIds.size());      assertSameIds(refIds, refs, 100, 109);            //Verify 10 msgs in storage      msgIds = getMessageIds();      assertEquals(10, msgIds.size());       assertSameIds(msgIds, refs, 100, 109);            //Verify 101 msgs in store      assertEquals(101, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 1 refs in downcache      assertEquals(1, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Send 9 more refs            tx = tr.createTransaction();      for (int i = 111; i < 120; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);                  refs[i].releaseMemoryReference();      }            tx.commit();            //verify 20 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(20, refIds.size());      assertSameIds(refIds, refs, 100, 119);            //Verify 20 msgs in storage      msgIds = getMessageIds();      assertEquals(20, msgIds.size());       assertSameIds(msgIds, refs, 100, 119);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                  //Send 100 more refs then roll back      tx = tr.createTransaction();            for (int i = 200; i < 300; i++)      {         Message m = CoreMessageFactory.createCoreMessage(i, true, null);         MessageReference ref = ms.reference(m);         queue.handle(null, ref, tx);              ref.releaseMemoryReference();      }        tx.rollback();               //Send 10 more refs            tx = tr.createTransaction();      for (int i = 120; i < 130; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);                  refs[i].releaseMemoryReference();      }        tx.commit();            //verify 30 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(30, refIds.size());      assertSameIds(refIds, refs, 100, 129);            //Verify 30 msgs in storage      msgIds = getMessageIds();      assertEquals(30, msgIds.size());       assertSameIds(msgIds, refs, 100, 129);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Send 10 more refs            tx = tr.createTransaction();      for (int i = 130; i < 140; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);               refs[i].releaseMemoryReference();      }        tx.commit();            //verify 40 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(40, refIds.size());      assertSameIds(refIds, refs, 100, 139);            //Verify 40 msgs in storage      msgIds = getMessageIds();      assertEquals(40, msgIds.size());       assertSameIds(msgIds, refs, 100, 139);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());        //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Send one more ref            tx = tr.createTransaction();      msgs[140] = CoreMessageFactory.createCoreMessage(140, false, null);      refs[140] = ms.reference(msgs[140]);      queue.handle(null, refs[140], tx);      refs[140].releaseMemoryReference();      tx.commit();            //verify 40 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(40, refIds.size());      assertSameIds(refIds, refs, 100, 139);            //Verify 40 msgs in storage      msgIds = getMessageIds();      assertEquals(40, msgIds.size());       assertSameIds(msgIds, refs, 100, 139);            //Verify 101 msgs in store      assertEquals(101, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 1 refs in downcache      assertEquals(1, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());              //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                              //Consume 1      int consumeCount = 0;      consumeInTx(queue, consumeCount, refs, 1);      consumeCount++;            //verify 40 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(40, refIds.size());      assertSameIds(refIds, refs, 100, 139);            //Verify 40 msgs in storage      msgIds = getMessageIds();      assertEquals(40, msgIds.size());       assertSameIds(msgIds, refs, 100, 139);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 99 refs in queue      assertEquals(99, queue.memoryRefCount());            //Verify 1 refs in downcache      assertEquals(1, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Now we should have 99 refs in memory, 40 refs in storage, and 1 in down cache, 100 msgs in memory            //Consume 18 more      consumeInTx(queue, consumeCount, refs, 18);      consumeCount += 18;            //We should have 81 refs in memory, 40 refs in storage, and 1 in down cache, 100 msgs in memory            //verify 40 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(40, refIds.size());      assertSameIds(refIds, refs, 100, 139);            //Verify 40 msgs in storage      msgIds = getMessageIds();      assertEquals(40, msgIds.size());       assertSameIds(msgIds, refs, 100, 139);            //Verify 82 msgs in store      assertEquals(82, ms.size());            //Verify 81 refs in queue      assertEquals(81, queue.memoryRefCount());            //Verify 1 refs in downcache      assertEquals(1, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Consume one more            consumeInTx(queue, consumeCount, refs, 1);      consumeCount++;            //This should force a load of 20 and flush the downcache            //verify 21 refs in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(21, refIds.size());      assertSameIds(refIds, refs, 120, 140);            //Verify 21 msgs in storage      msgIds = getMessageIds();      assertEquals(21, msgIds.size());       assertSameIds(msgIds, refs, 120, 140);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Consume 20 more            consumeInTx(queue, consumeCount, refs, 20);      consumeCount += 20;            //verify 1 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(1, refIds.size());      assertSameIds(refIds, refs, 140, 140);            //Verify 1 msgs in storage      msgIds = getMessageIds();      assertEquals(1, msgIds.size());       assertSameIds(msgIds, refs, 140, 140);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                                    //Consume 1 more            consumeInTx(queue, consumeCount, refs, 1);      consumeCount ++;            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());             //Verify 81 msgs in store      assertEquals(100, ms.size());            //Verify 81 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Consume 20 more            consumeInTx(queue, consumeCount, refs, 20);      consumeCount += 20;            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());             //Verify 80 msgs in store      assertEquals(80, ms.size());            //Verify 80 refs in queue      assertEquals(80, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify not paging      assertFalse(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                        //Consumer 60 more                        consumeInTx(queue, consumeCount, refs, 60);      consumeCount += 60;            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());             //Verify 20 msgs in store      assertEquals(20, ms.size());            //Verify 20 refs in queue      assertEquals(20, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify not paging      assertFalse(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Add 20 more messages      tx = tr.createTransaction();      for (int i = 141; i < 161; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);         refs[i].releaseMemoryReference();      }      tx.commit();            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());             //Verify 40 msgs in store      assertEquals(40, ms.size());            //Verify 40 refs in queue      assertEquals(40, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify not paging      assertFalse(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Add 20 more messages      tx = tr.createTransaction();      for (int i = 161; i < 181; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);         refs[i].releaseMemoryReference();      }      tx.commit();            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());             //Verify 60 msgs in store      assertEquals(60, ms.size());            //Verify 60 refs in queue      assertEquals(60, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify not paging      assertFalse(queue.isPaging());            //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());                        //Add 60 more messages      tx = tr.createTransaction();      for (int i = 181; i < 241; i++)      {         msgs[i] = CoreMessageFactory.createCoreMessage(i, false, null);         refs[i] = ms.reference(msgs[i]);         queue.handle(null, refs[i], tx);         refs[i].releaseMemoryReference();      }      tx.commit();            //verify 20 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(20, refIds.size());      assertSameIds(refIds, refs, 221, 240);            //Verify 20 msgs in storage      msgIds = getMessageIds();      assertEquals(20, msgIds.size());       assertSameIds(msgIds, refs, 221, 240);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify 0 refs in downcache      assertEquals(0, queue.downCacheCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify no deliveries      assertEquals(0, queue.memoryDeliveryCount());;                               //test cancellation            //remove 20 but don't ack them yet      //this should cause a load to be triggered            SimpleDelivery[] dels = getDeliveries(queue, 20);                  //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());         //Verify 120 msgs in store      assertEquals(120, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify 20 deliveries      assertEquals(20, queue.memoryDeliveryCount());;                               //Cancel last 7      for (int i = 19; i > 12; i--)      {         dels[i].cancel();        }            //This should cause the refs corresponding to the deliveries to go the front of the in memory quuee      //and the oldest refs in memory evicted off the end into the down cache            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());            //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());         //Verify 120 msgs in store      assertEquals(120, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify 13 deliveries      assertEquals(13, queue.memoryDeliveryCount());;                           //Cancel 3 more            for (int i = 12; i > 9; i--)      {         dels[i].cancel();      }            //This should cause the down cache to be flushed            //verify 10 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(10, refIds.size());      assertSameIds(refIds, refs, 231, 240);            //Verify 10 msgs in storage      msgIds = getMessageIds();      assertEquals(10, msgIds.size());       assertSameIds(msgIds, refs, 231, 240);            //Verify 110 msgs in store      assertEquals(110, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify 10 deliveries      assertEquals(10, queue.memoryDeliveryCount());;                                    //Cancel the last 10            for (int i = 9; i >= 0; i--)      {         dels[i].cancel();      }                  //This should cause the down cache to be flushed            //verify 20 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(20, refIds.size());      assertSameIds(refIds, refs, 221, 240);            //Verify 20 msgs in storage      msgIds = getMessageIds();      assertEquals(20, msgIds.size());       assertSameIds(msgIds, refs, 221, 240);            //Verify 100 msgs in store      assertEquals(100, ms.size());            //Verify 100 refs in queue      assertEquals(100, queue.memoryRefCount());            //Verify paging      assertTrue(queue.isPaging());                  //Verify 0 deliveries      assertEquals(0, queue.memoryDeliveryCount());;                        //Now there should be 120 message left to consume            //Consume 50            consumeInTx(queue, consumeCount, refs, 50);      consumeCount += 50;            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());                 //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());       //Verify 70 msgs in store      assertEquals(70, ms.size());            //Verify 70 refs in queue      assertEquals(70, queue.memoryRefCount());              //Verify not paging      assertFalse(queue.isPaging());                  //Verify 0 deliveries      assertEquals(0, queue.memoryDeliveryCount());                              //Consume the rest            consumeInTx(queue, consumeCount, refs, 70);      consumeCount += 70;            //verify 0 ref in storage            refIds = getReferenceIdsOrderedByPageOrd(queue.getChannelID());      assertEquals(0, refIds.size());                 //Verify 0 msgs in storage      msgIds = getMessageIds();      assertEquals(0, msgIds.size());       //Verify 0 msgs in store      assertEquals(0, ms.size());            //Verify 0 refs in queue      assertEquals(0, queue.memoryRefCount());            //Make sure there are no more refs in queue            assertEquals(0, queue.messageCount());            assertEquals(0, LockMap.instance.getSize());   }