ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.52 by jsr166, Sat May 28 22:40:00 2011 UTC vs.
Revision 1.78 by jsr166, Sun Oct 16 20:44:18 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.util.concurrent.*;
10 >
11 > import java.util.ArrayList;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Delayed;
19 > import java.util.concurrent.DelayQueue;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.TimeUnit;
23 >
24 > import junit.framework.Test;
25  
26   public class DelayQueueTest extends JSR166TestCase {
27 +
28 +    public static class Generic extends BlockingQueueTest {
29 +        protected BlockingQueue emptyCollection() {
30 +            return new DelayQueue();
31 +        }
32 +        protected PDelay makeElement(int i) {
33 +            return new PDelay(i);
34 +        }
35 +    }
36 +
37      public static void main(String[] args) {
38 <        junit.textui.TestRunner.run(suite());
38 >        main(suite(), args);
39      }
40  
41      public static Test suite() {
42 <        return new TestSuite(DelayQueueTest.class);
42 >        return newTestSuite(DelayQueueTest.class,
43 >                            new Generic().testSuite());
44      }
45  
23    private static final int NOCAP = Integer.MAX_VALUE;
24
46      /**
47       * A delayed implementation for testing.
48       * Most tests use Pseudodelays, where delays are all elapsed
# Line 29 | Line 50 | public class DelayQueueTest extends JSR1
50       */
51      static class PDelay implements Delayed {
52          int pseudodelay;
53 <        PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
54 <        public int compareTo(PDelay y) {
55 <            int i = pseudodelay;
56 <            int j = y.pseudodelay;
57 <            if (i < j) return -1;
37 <            if (i > j) return 1;
38 <            return 0;
53 >        PDelay(int i) { pseudodelay = i; }
54 >        public int compareTo(PDelay other) {
55 >            int a = this.pseudodelay;
56 >            int b = other.pseudodelay;
57 >            return (a < b) ? -1 : (a > b) ? 1 : 0;
58          }
40
59          public int compareTo(Delayed y) {
60              return compareTo((PDelay)y);
61          }
44
62          public boolean equals(Object other) {
63 <            return equals((PDelay)other);
64 <        }
48 <        public boolean equals(PDelay other) {
49 <            return other.pseudodelay == pseudodelay;
63 >            return (other instanceof PDelay) &&
64 >                this.pseudodelay == ((PDelay)other).pseudodelay;
65          }
66 <
66 >        // suppress [overrides] javac warning
67 >        public int hashCode() { return pseudodelay; }
68          public long getDelay(TimeUnit ignore) {
69 <            return pseudodelay;
54 <        }
55 <        public int intValue() {
56 <            return pseudodelay;
69 >            return Integer.MIN_VALUE + pseudodelay;
70          }
58
71          public String toString() {
72              return String.valueOf(pseudodelay);
73          }
# Line 82 | Line 94 | public class DelayQueueTest extends JSR1
94          }
95  
96          public boolean equals(Object other) {
97 <            return equals((NanoDelay)other);
98 <        }
87 <        public boolean equals(NanoDelay other) {
88 <            return other.trigger == trigger;
97 >            return (other instanceof NanoDelay) &&
98 >                this.trigger == ((NanoDelay)other).trigger;
99          }
100  
101 +        // suppress [overrides] javac warning
102 +        public int hashCode() { return (int) trigger; }
103 +
104          public long getDelay(TimeUnit unit) {
105              long n = trigger - System.nanoTime();
106              return unit.convert(n, TimeUnit.NANOSECONDS);
# Line 103 | Line 116 | public class DelayQueueTest extends JSR1
116      }
117  
118      /**
119 <     * Create a queue of given size containing consecutive
120 <     * PDelays 0 ... n.
119 >     * Returns a new queue of given size containing consecutive
120 >     * PDelays 0 ... n - 1.
121       */
122      private DelayQueue<PDelay> populatedQueue(int n) {
123          DelayQueue<PDelay> q = new DelayQueue<PDelay>();
124          assertTrue(q.isEmpty());
125 <        for (int i = n-1; i >= 0; i-=2)
125 >        for (int i = n - 1; i >= 0; i -= 2)
126              assertTrue(q.offer(new PDelay(i)));
127 <        for (int i = (n & 1); i < n; i+=2)
127 >        for (int i = (n & 1); i < n; i += 2)
128              assertTrue(q.offer(new PDelay(i)));
129          assertFalse(q.isEmpty());
130 <        assertEquals(NOCAP, q.remainingCapacity());
130 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
131          assertEquals(n, q.size());
132 +        assertEquals(new PDelay(0), q.peek());
133          return q;
134      }
135  
# Line 123 | Line 137 | public class DelayQueueTest extends JSR1
137       * A new queue has unbounded capacity
138       */
139      public void testConstructor1() {
140 <        assertEquals(NOCAP, new DelayQueue().remainingCapacity());
140 >        assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity());
141      }
142  
143      /**
# Line 131 | Line 145 | public class DelayQueueTest extends JSR1
145       */
146      public void testConstructor3() {
147          try {
148 <            DelayQueue q = new DelayQueue(null);
148 >            new DelayQueue(null);
149              shouldThrow();
150          } catch (NullPointerException success) {}
151      }
# Line 141 | Line 155 | public class DelayQueueTest extends JSR1
155       */
156      public void testConstructor4() {
157          try {
158 <            PDelay[] ints = new PDelay[SIZE];
145 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
158 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
159              shouldThrow();
160          } catch (NullPointerException success) {}
161      }
# Line 151 | Line 164 | public class DelayQueueTest extends JSR1
164       * Initializing from Collection with some null elements throws NPE
165       */
166      public void testConstructor5() {
167 +        PDelay[] a = new PDelay[SIZE];
168 +        for (int i = 0; i < SIZE - 1; ++i)
169 +            a[i] = new PDelay(i);
170          try {
171 <            PDelay[] ints = new PDelay[SIZE];
156 <            for (int i = 0; i < SIZE-1; ++i)
157 <                ints[i] = new PDelay(i);
158 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
171 >            new DelayQueue(Arrays.asList(a));
172              shouldThrow();
173          } catch (NullPointerException success) {}
174      }
# Line 178 | Line 191 | public class DelayQueueTest extends JSR1
191      public void testEmpty() {
192          DelayQueue q = new DelayQueue();
193          assertTrue(q.isEmpty());
194 <        assertEquals(NOCAP, q.remainingCapacity());
194 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
195          q.add(new PDelay(1));
196          assertFalse(q.isEmpty());
197          q.add(new PDelay(2));
# Line 188 | Line 201 | public class DelayQueueTest extends JSR1
201      }
202  
203      /**
204 <     * remainingCapacity does not change when elements added or removed,
192 <     * but size does
204 >     * remainingCapacity() always returns Integer.MAX_VALUE
205       */
206      public void testRemainingCapacity() {
207 <        DelayQueue q = populatedQueue(SIZE);
207 >        BlockingQueue q = populatedQueue(SIZE);
208          for (int i = 0; i < SIZE; ++i) {
209 <            assertEquals(NOCAP, q.remainingCapacity());
210 <            assertEquals(SIZE-i, q.size());
211 <            q.remove();
209 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
210 >            assertEquals(SIZE - i, q.size());
211 >            assertTrue(q.remove() instanceof PDelay);
212          }
213          for (int i = 0; i < SIZE; ++i) {
214 <            assertEquals(NOCAP, q.remainingCapacity());
214 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
215              assertEquals(i, q.size());
216 <            q.add(new PDelay(i));
216 >            assertTrue(q.add(new PDelay(i)));
217          }
218      }
219  
220      /**
209     * offer(null) throws NPE
210     */
211    public void testOfferNull() {
212        try {
213            DelayQueue q = new DelayQueue();
214            q.offer(null);
215            shouldThrow();
216        } catch (NullPointerException success) {}
217    }
218
219    /**
220     * add(null) throws NPE
221     */
222    public void testAddNull() {
223        try {
224            DelayQueue q = new DelayQueue();
225            q.add(null);
226            shouldThrow();
227        } catch (NullPointerException success) {}
228    }
229
230    /**
221       * offer non-null succeeds
222       */
223      public void testOffer() {
# Line 248 | Line 238 | public class DelayQueueTest extends JSR1
238      }
239  
240      /**
251     * addAll(null) throws NPE
252     */
253    public void testAddAll1() {
254        try {
255            DelayQueue q = new DelayQueue();
256            q.addAll(null);
257            shouldThrow();
258        } catch (NullPointerException success) {}
259    }
260
261    /**
241       * addAll(this) throws IAE
242       */
243      public void testAddAllSelf() {
244 +        DelayQueue q = populatedQueue(SIZE);
245          try {
266            DelayQueue q = populatedQueue(SIZE);
246              q.addAll(q);
247              shouldThrow();
248          } catch (IllegalArgumentException success) {}
249      }
250  
251      /**
273     * addAll of a collection with null elements throws NPE
274     */
275    public void testAddAll2() {
276        try {
277            DelayQueue q = new DelayQueue();
278            PDelay[] ints = new PDelay[SIZE];
279            q.addAll(Arrays.asList(ints));
280            shouldThrow();
281        } catch (NullPointerException success) {}
282    }
283
284    /**
252       * addAll of a collection with any null elements throws NPE after
253       * possibly adding some elements
254       */
255      public void testAddAll3() {
256 +        DelayQueue q = new DelayQueue();
257 +        PDelay[] a = new PDelay[SIZE];
258 +        for (int i = 0; i < SIZE - 1; ++i)
259 +            a[i] = new PDelay(i);
260          try {
261 <            DelayQueue q = new DelayQueue();
291 <            PDelay[] ints = new PDelay[SIZE];
292 <            for (int i = 0; i < SIZE-1; ++i)
293 <                ints[i] = new PDelay(i);
294 <            q.addAll(Arrays.asList(ints));
261 >            q.addAll(Arrays.asList(a));
262              shouldThrow();
263          } catch (NullPointerException success) {}
264      }
# Line 302 | Line 269 | public class DelayQueueTest extends JSR1
269      public void testAddAll5() {
270          PDelay[] empty = new PDelay[0];
271          PDelay[] ints = new PDelay[SIZE];
272 <        for (int i = SIZE-1; i >= 0; --i)
272 >        for (int i = SIZE - 1; i >= 0; --i)
273              ints[i] = new PDelay(i);
274          DelayQueue q = new DelayQueue();
275          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 312 | Line 279 | public class DelayQueueTest extends JSR1
279      }
280  
281      /**
315     * put(null) throws NPE
316     */
317    public void testPutNull() {
318        try {
319            DelayQueue q = new DelayQueue();
320            q.put(null);
321            shouldThrow();
322        } catch (NullPointerException success) {}
323    }
324
325    /**
282       * all elements successfully put are contained
283       */
284      public void testPut() {
285          DelayQueue q = new DelayQueue();
286          for (int i = 0; i < SIZE; ++i) {
287 <            PDelay I = new PDelay(i);
288 <            q.put(I);
289 <            assertTrue(q.contains(I));
287 >            PDelay x = new PDelay(i);
288 >            q.put(x);
289 >            assertTrue(q.contains(x));
290          }
291          assertEquals(SIZE, q.size());
292      }
# Line 374 | Line 330 | public class DelayQueueTest extends JSR1
330      public void testTake() throws InterruptedException {
331          DelayQueue q = populatedQueue(SIZE);
332          for (int i = 0; i < SIZE; ++i) {
333 <            assertEquals(new PDelay(i), ((PDelay)q.take()));
333 >            assertEquals(new PDelay(i), q.take());
334          }
335      }
336  
337      /**
382     * take blocks interruptibly when empty
383     */
384    public void testTakeFromEmptyBlocksInterruptibly()
385            throws InterruptedException {
386        final BlockingQueue q = new DelayQueue();
387        final CountDownLatch threadStarted = new CountDownLatch(1);
388        Thread t = newStartedThread(new CheckedRunnable() {
389            public void realRun() {
390                threadStarted.countDown();
391                try {
392                    q.take();
393                    shouldThrow();
394                } catch (InterruptedException success) {}
395                assertFalse(Thread.interrupted());
396            }});
397
398        await(threadStarted);
399        assertThreadStaysAlive(t);
400        t.interrupt();
401        awaitTermination(t);
402    }
403
404    /**
338       * Take removes existing elements until empty, then blocks interruptibly
339       */
340      public void testBlockingTake() throws InterruptedException {
# Line 440 | Line 373 | public class DelayQueueTest extends JSR1
373      public void testPoll() {
374          DelayQueue q = populatedQueue(SIZE);
375          for (int i = 0; i < SIZE; ++i) {
376 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
376 >            assertEquals(new PDelay(i), q.poll());
377          }
378          assertNull(q.poll());
379      }
# Line 451 | Line 384 | public class DelayQueueTest extends JSR1
384      public void testTimedPoll0() throws InterruptedException {
385          DelayQueue q = populatedQueue(SIZE);
386          for (int i = 0; i < SIZE; ++i) {
387 <            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
387 >            assertEquals(new PDelay(i), q.poll(0, MILLISECONDS));
388          }
389          assertNull(q.poll(0, MILLISECONDS));
390      }
# Line 463 | Line 396 | public class DelayQueueTest extends JSR1
396          DelayQueue q = populatedQueue(SIZE);
397          for (int i = 0; i < SIZE; ++i) {
398              long startTime = System.nanoTime();
399 <            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
399 >            assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS));
400              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
401          }
402          long startTime = System.nanoTime();
# Line 478 | Line 411 | public class DelayQueueTest extends JSR1
411       */
412      public void testInterruptedTimedPoll() throws InterruptedException {
413          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
414 +        final DelayQueue q = populatedQueue(SIZE);
415          Thread t = newStartedThread(new CheckedRunnable() {
416              public void realRun() throws InterruptedException {
417 <                DelayQueue q = populatedQueue(SIZE);
417 >                long startTime = System.nanoTime();
418                  for (int i = 0; i < SIZE; ++i) {
419 <                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
419 >                    assertEquals(new PDelay(i),
420 >                                 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
421                  }
422  
423                  Thread.currentThread().interrupt();
# Line 498 | Line 433 | public class DelayQueueTest extends JSR1
433                      shouldThrow();
434                  } catch (InterruptedException success) {}
435                  assertFalse(Thread.interrupted());
436 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
437              }});
438  
439          await(pleaseInterrupt);
440          assertThreadStaysAlive(t);
441          t.interrupt();
442          awaitTermination(t);
443 <    }
508 <
509 <    /**
510 <     * timed poll before a delayed offer fails; after offer succeeds;
511 <     * on interruption throws
512 <     */
513 <    public void testTimedPollWithOffer() throws InterruptedException {
514 <        final DelayQueue q = new DelayQueue();
515 <        final PDelay pdelay = new PDelay(0);
516 <        final CheckedBarrier barrier = new CheckedBarrier(2);
517 <        Thread t = newStartedThread(new CheckedRunnable() {
518 <            public void realRun() throws InterruptedException {
519 <                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
520 <
521 <                barrier.await();
522 <                assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS));
523 <
524 <                Thread.currentThread().interrupt();
525 <                try {
526 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
527 <                    shouldThrow();
528 <                } catch (InterruptedException success) {}
529 <                assertFalse(Thread.interrupted());
530 <
531 <                barrier.await();
532 <                try {
533 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
534 <                    shouldThrow();
535 <                } catch (InterruptedException success) {}
536 <                assertFalse(Thread.interrupted());
537 <            }});
538 <
539 <        barrier.await();
540 <        long startTime = System.nanoTime();
541 <        assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS));
542 <        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
543 <
544 <        barrier.await();
545 <        assertThreadStaysAlive(t);
546 <        t.interrupt();
547 <        awaitTermination(t);
443 >        checkEmpty(q);
444      }
445  
446      /**
# Line 553 | Line 449 | public class DelayQueueTest extends JSR1
449      public void testPeek() {
450          DelayQueue q = populatedQueue(SIZE);
451          for (int i = 0; i < SIZE; ++i) {
452 <            assertEquals(new PDelay(i), ((PDelay)q.peek()));
453 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
452 >            assertEquals(new PDelay(i), q.peek());
453 >            assertEquals(new PDelay(i), q.poll());
454              if (q.isEmpty())
455                  assertNull(q.peek());
456              else
# Line 569 | Line 465 | public class DelayQueueTest extends JSR1
465      public void testElement() {
466          DelayQueue q = populatedQueue(SIZE);
467          for (int i = 0; i < SIZE; ++i) {
468 <            assertEquals(new PDelay(i), ((PDelay)q.element()));
468 >            assertEquals(new PDelay(i), q.element());
469              q.poll();
470          }
471          try {
# Line 584 | Line 480 | public class DelayQueueTest extends JSR1
480      public void testRemove() {
481          DelayQueue q = populatedQueue(SIZE);
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertEquals(new PDelay(i), ((PDelay)q.remove()));
483 >            assertEquals(new PDelay(i), q.remove());
484          }
485          try {
486              q.remove();
# Line 593 | Line 489 | public class DelayQueueTest extends JSR1
489      }
490  
491      /**
596     * remove(x) removes x and returns true if present
597     */
598    public void testRemoveElement() {
599        DelayQueue q = populatedQueue(SIZE);
600        for (int i = 1; i < SIZE; i+=2) {
601            assertTrue(q.remove(new PDelay(i)));
602        }
603        for (int i = 0; i < SIZE; i+=2) {
604            assertTrue(q.remove(new PDelay(i)));
605            assertFalse(q.remove(new PDelay(i+1)));
606        }
607        assertTrue(q.isEmpty());
608    }
609
610    /**
492       * contains(x) reports true when elements added but not yet removed
493       */
494      public void testContains() {
# Line 627 | Line 508 | public class DelayQueueTest extends JSR1
508          q.clear();
509          assertTrue(q.isEmpty());
510          assertEquals(0, q.size());
511 <        assertEquals(NOCAP, q.remainingCapacity());
511 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
512          PDelay x = new PDelay(1);
513          q.add(x);
514          assertFalse(q.isEmpty());
# Line 664 | Line 545 | public class DelayQueueTest extends JSR1
545                  assertTrue(changed);
546  
547              assertTrue(q.containsAll(p));
548 <            assertEquals(SIZE-i, q.size());
548 >            assertEquals(SIZE - i, q.size());
549              p.remove();
550          }
551      }
# Line 677 | Line 558 | public class DelayQueueTest extends JSR1
558              DelayQueue q = populatedQueue(SIZE);
559              DelayQueue p = populatedQueue(i);
560              assertTrue(q.removeAll(p));
561 <            assertEquals(SIZE-i, q.size());
561 >            assertEquals(SIZE - i, q.size());
562              for (int j = 0; j < i; ++j) {
563 <                PDelay I = (PDelay)(p.remove());
564 <                assertFalse(q.contains(I));
563 >                PDelay x = (PDelay)(p.remove());
564 >                assertFalse(q.contains(x));
565              }
566          }
567      }
# Line 710 | Line 591 | public class DelayQueueTest extends JSR1
591      }
592  
593      /**
713     * toArray(null) throws NullPointerException
714     */
715    public void testToArray_NullArg() {
716        DelayQueue q = populatedQueue(SIZE);
717        try {
718            q.toArray(null);
719            shouldThrow();
720        } catch (NullPointerException success) {}
721    }
722
723    /**
594       * toArray(incompatible array type) throws ArrayStoreException
595       */
596      public void testToArray1_BadArg() {
# Line 743 | Line 613 | public class DelayQueueTest extends JSR1
613              ++i;
614          }
615          assertEquals(i, SIZE);
616 +        assertIteratorExhausted(it);
617 +    }
618 +
619 +    /**
620 +     * iterator of empty collection has no elements
621 +     */
622 +    public void testEmptyIterator() {
623 +        assertIteratorExhausted(new DelayQueue().iterator());
624      }
625  
626      /**
# Line 757 | Line 635 | public class DelayQueueTest extends JSR1
635          it.next();
636          it.remove();
637          it = q.iterator();
638 <        assertEquals(it.next(), new PDelay(2));
639 <        assertEquals(it.next(), new PDelay(3));
638 >        assertEquals(new PDelay(2), it.next());
639 >        assertEquals(new PDelay(3), it.next());
640          assertFalse(it.hasNext());
641      }
642  
# Line 768 | Line 646 | public class DelayQueueTest extends JSR1
646      public void testToString() {
647          DelayQueue q = populatedQueue(SIZE);
648          String s = q.toString();
649 <        for (int i = 0; i < SIZE; ++i) {
650 <            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
773 <        }
649 >        for (Object e : q)
650 >            assertTrue(s.contains(e.toString()));
651      }
652  
653      /**
# Line 779 | Line 656 | public class DelayQueueTest extends JSR1
656      public void testPollInExecutor() {
657          final DelayQueue q = new DelayQueue();
658          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
659 <        ExecutorService executor = Executors.newFixedThreadPool(2);
660 <        executor.execute(new CheckedRunnable() {
661 <            public void realRun() throws InterruptedException {
662 <                assertNull(q.poll());
663 <                threadsStarted.await();
664 <                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
665 <                checkEmpty(q);
666 <            }});
667 <
668 <        executor.execute(new CheckedRunnable() {
669 <            public void realRun() throws InterruptedException {
670 <                threadsStarted.await();
671 <                q.put(new PDelay(1));
672 <            }});
673 <
674 <        joinPool(executor);
659 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
660 >        try (PoolCleaner cleaner = cleaner(executor)) {
661 >            executor.execute(new CheckedRunnable() {
662 >                public void realRun() throws InterruptedException {
663 >                    assertNull(q.poll());
664 >                    threadsStarted.await();
665 >                    assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
666 >                    checkEmpty(q);
667 >                }});
668 >
669 >            executor.execute(new CheckedRunnable() {
670 >                public void realRun() throws InterruptedException {
671 >                    threadsStarted.await();
672 >                    q.put(new PDelay(1));
673 >                }});
674 >        }
675      }
676  
677      /**
# Line 845 | Line 722 | public class DelayQueueTest extends JSR1
722      }
723  
724      /**
848     * drainTo(null) throws NPE
849     */
850    public void testDrainToNull() {
851        DelayQueue q = populatedQueue(SIZE);
852        try {
853            q.drainTo(null);
854            shouldThrow();
855        } catch (NullPointerException success) {}
856    }
857
858    /**
859     * drainTo(this) throws IAE
860     */
861    public void testDrainToSelf() {
862        DelayQueue q = populatedQueue(SIZE);
863        try {
864            q.drainTo(q);
865            shouldThrow();
866        } catch (IllegalArgumentException success) {}
867    }
868
869    /**
725       * drainTo(c) empties queue into another collection c
726       */
727      public void testDrainTo() {
# Line 878 | Line 733 | public class DelayQueueTest extends JSR1
733          }
734          ArrayList l = new ArrayList();
735          q.drainTo(l);
736 <        assertEquals(q.size(), 0);
736 >        assertEquals(0, q.size());
737          for (int i = 0; i < SIZE; ++i)
738 <            assertEquals(l.get(i), elems[i]);
738 >            assertEquals(elems[i], l.get(i));
739          q.add(elems[0]);
740          q.add(elems[1]);
741          assertFalse(q.isEmpty());
# Line 888 | Line 743 | public class DelayQueueTest extends JSR1
743          assertTrue(q.contains(elems[1]));
744          l.clear();
745          q.drainTo(l);
746 <        assertEquals(q.size(), 0);
747 <        assertEquals(l.size(), 2);
746 >        assertEquals(0, q.size());
747 >        assertEquals(2, l.size());
748          for (int i = 0; i < 2; ++i)
749 <            assertEquals(l.get(i), elems[i]);
749 >            assertEquals(elems[i], l.get(i));
750      }
751  
752      /**
# Line 901 | Line 756 | public class DelayQueueTest extends JSR1
756          final DelayQueue q = populatedQueue(SIZE);
757          Thread t = new Thread(new CheckedRunnable() {
758              public void realRun() {
759 <                q.put(new PDelay(SIZE+1));
759 >                q.put(new PDelay(SIZE + 1));
760              }});
761  
762          t.start();
# Line 913 | Line 768 | public class DelayQueueTest extends JSR1
768      }
769  
770      /**
916     * drainTo(null, n) throws NPE
917     */
918    public void testDrainToNullN() {
919        DelayQueue q = populatedQueue(SIZE);
920        try {
921            q.drainTo(null, 0);
922            shouldThrow();
923        } catch (NullPointerException success) {}
924    }
925
926    /**
927     * drainTo(this, n) throws IAE
928     */
929    public void testDrainToSelfN() {
930        DelayQueue q = populatedQueue(SIZE);
931        try {
932            q.drainTo(q, 0);
933            shouldThrow();
934        } catch (IllegalArgumentException success) {}
935    }
936
937    /**
771       * drainTo(c, n) empties first min(n, size) elements of queue into c
772       */
773      public void testDrainToN() {
# Line 943 | Line 776 | public class DelayQueueTest extends JSR1
776              ArrayList l = new ArrayList();
777              q.drainTo(l, i);
778              int k = (i < SIZE) ? i : SIZE;
779 <            assertEquals(q.size(), SIZE-k);
780 <            assertEquals(l.size(), k);
779 >            assertEquals(SIZE - k, q.size());
780 >            assertEquals(k, l.size());
781          }
782      }
783  
784 +    /**
785 +     * remove(null), contains(null) always return false
786 +     */
787 +    public void testNeverContainsNull() {
788 +        Collection<?> q = populatedQueue(SIZE);
789 +        assertFalse(q.contains(null));
790 +        assertFalse(q.remove(null));
791 +    }
792   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines