/** drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { for (int i = 0; i < SIZE + 2; ++i) { DelayQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; assertEquals(SIZE - k, q.size()); assertEquals(k, l.size()); } }
/** drainTo(c) empties queue into another collection c */ public void testDrainTo() { DelayQueue q = new DelayQueue(); PDelay[] elems = new PDelay[SIZE]; for (int i = 0; i < SIZE; ++i) { elems[i] = new PDelay(i); q.add(elems[i]); } ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(0, q.size()); for (int i = 0; i < SIZE; ++i) assertEquals(elems[i], l.get(i)); q.add(elems[0]); q.add(elems[1]); assertFalse(q.isEmpty()); assertTrue(q.contains(elems[0])); assertTrue(q.contains(elems[1])); l.clear(); q.drainTo(l); assertEquals(0, q.size()); assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) assertEquals(elems[i], l.get(i)); }
/** drainTo empties queue */ public void testDrainToWithActivePut() throws InterruptedException { final DelayQueue q = populatedQueue(SIZE); Thread t = new Thread( new CheckedRunnable() { public void realRun() { q.put(new PDelay(SIZE + 1)); } }); t.start(); ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); t.join(); assertTrue(q.size() + l.size() >= SIZE); }