Example #1
0
 /** Returns a new queue of given size containing consecutive PDelays 0 ... n. */
 private DelayQueue<PDelay> populatedQueue(int n) {
   DelayQueue<PDelay> q = new DelayQueue<PDelay>();
   assertTrue(q.isEmpty());
   for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new PDelay(i)));
   for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new PDelay(i)));
   assertFalse(q.isEmpty());
   assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
   assertEquals(n, q.size());
   return q;
 }
Example #2
0
 /** isEmpty is true before add, false after */
 public void testEmpty() {
   DelayQueue q = new DelayQueue();
   assertTrue(q.isEmpty());
   assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
   q.add(new PDelay(1));
   assertFalse(q.isEmpty());
   q.add(new PDelay(2));
   q.remove();
   q.remove();
   assertTrue(q.isEmpty());
 }
Example #3
0
 /** clear removes all elements */
 public void testClear() {
   DelayQueue q = populatedQueue(SIZE);
   q.clear();
   assertTrue(q.isEmpty());
   assertEquals(0, q.size());
   assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
   PDelay x = new PDelay(1);
   q.add(x);
   assertFalse(q.isEmpty());
   assertTrue(q.contains(x));
   q.clear();
   assertTrue(q.isEmpty());
 }
Example #4
0
 /** peek returns next element, or null if empty */
 public void testPeek() {
   DelayQueue q = populatedQueue(SIZE);
   for (int i = 0; i < SIZE; ++i) {
     assertEquals(new PDelay(i), q.peek());
     assertEquals(new PDelay(i), q.poll());
     if (q.isEmpty()) assertNull(q.peek());
     else assertFalse(new PDelay(i).equals(q.peek()));
   }
   assertNull(q.peek());
 }
Example #5
0
  /** Delayed actions do not occur until their delay elapses */
  public void testDelay() throws InterruptedException {
    DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
    for (int i = 0; i < SIZE; ++i) q.add(new NanoDelay(1000000L * (SIZE - i)));

    long last = 0;
    for (int i = 0; i < SIZE; ++i) {
      NanoDelay e = q.take();
      long tt = e.getTriggerTime();
      assertTrue(System.nanoTime() - tt >= 0);
      if (i != 0) assertTrue(tt >= last);
      last = tt;
    }
    assertTrue(q.isEmpty());
  }
Example #6
0
 /** 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));
 }