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.44 by jsr166, Thu Nov 4 01:04:54 2010 UTC vs.
Revision 1.79 by jsr166, Mon Oct 17 01:54:51 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
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 >        class Implementation implements CollectionImplementation {
43 >            public Class<?> klazz() { return DelayQueue.class; }
44 >            public Collection emptyCollection() { return new DelayQueue(); }
45 >            public Object makeElement(int i) { return new PDelay(i); }
46 >            public boolean isConcurrent() { return true; }
47 >            public boolean permitsNulls() { return false; }
48 >        }
49 >        return newTestSuite(DelayQueueTest.class,
50 >                            new Generic().testSuite(),
51 >                            CollectionTest.testSuite(new Implementation()));
52      }
53  
23    private static final int NOCAP = Integer.MAX_VALUE;
24
54      /**
55       * A delayed implementation for testing.
56       * Most tests use Pseudodelays, where delays are all elapsed
# Line 29 | Line 58 | public class DelayQueueTest extends JSR1
58       */
59      static class PDelay implements Delayed {
60          int pseudodelay;
61 <        PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
62 <        public int compareTo(PDelay y) {
63 <            int i = pseudodelay;
64 <            int j = y.pseudodelay;
65 <            if (i < j) return -1;
37 <            if (i > j) return 1;
38 <            return 0;
61 >        PDelay(int i) { pseudodelay = i; }
62 >        public int compareTo(PDelay other) {
63 >            int a = this.pseudodelay;
64 >            int b = other.pseudodelay;
65 >            return (a < b) ? -1 : (a > b) ? 1 : 0;
66          }
40
67          public int compareTo(Delayed y) {
68              return compareTo((PDelay)y);
69          }
44
70          public boolean equals(Object other) {
71 <            return equals((PDelay)other);
71 >            return (other instanceof PDelay) &&
72 >                this.pseudodelay == ((PDelay)other).pseudodelay;
73          }
74 <        public boolean equals(PDelay other) {
75 <            return other.pseudodelay == pseudodelay;
50 <        }
51 <
52 <
74 >        // suppress [overrides] javac warning
75 >        public int hashCode() { return pseudodelay; }
76          public long getDelay(TimeUnit ignore) {
77 <            return pseudodelay;
77 >            return Integer.MIN_VALUE + pseudodelay;
78          }
56        public int intValue() {
57            return pseudodelay;
58        }
59
79          public String toString() {
80              return String.valueOf(pseudodelay);
81          }
82      }
83  
65
84      /**
85       * Delayed implementation that actually delays
86       */
# Line 84 | Line 102 | public class DelayQueueTest extends JSR1
102          }
103  
104          public boolean equals(Object other) {
105 <            return equals((NanoDelay)other);
106 <        }
89 <        public boolean equals(NanoDelay other) {
90 <            return other.trigger == trigger;
105 >            return (other instanceof NanoDelay) &&
106 >                this.trigger == ((NanoDelay)other).trigger;
107          }
108  
109 +        // suppress [overrides] javac warning
110 +        public int hashCode() { return (int) trigger; }
111 +
112          public long getDelay(TimeUnit unit) {
113              long n = trigger - System.nanoTime();
114              return unit.convert(n, TimeUnit.NANOSECONDS);
# Line 104 | Line 123 | public class DelayQueueTest extends JSR1
123          }
124      }
125  
107
126      /**
127 <     * Create a queue of given size containing consecutive
128 <     * PDelays 0 ... n.
127 >     * Returns a new queue of given size containing consecutive
128 >     * PDelays 0 ... n - 1.
129       */
130 <    private DelayQueue populatedQueue(int n) {
131 <        DelayQueue q = new DelayQueue();
130 >    private DelayQueue<PDelay> populatedQueue(int n) {
131 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
132          assertTrue(q.isEmpty());
133 <        for (int i = n-1; i >= 0; i-=2)
133 >        for (int i = n - 1; i >= 0; i -= 2)
134              assertTrue(q.offer(new PDelay(i)));
135 <        for (int i = (n & 1); i < n; i+=2)
135 >        for (int i = (n & 1); i < n; i += 2)
136              assertTrue(q.offer(new PDelay(i)));
137          assertFalse(q.isEmpty());
138 <        assertEquals(NOCAP, q.remainingCapacity());
138 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
139          assertEquals(n, q.size());
140 +        assertEquals(new PDelay(0), q.peek());
141          return q;
142      }
143  
# Line 126 | Line 145 | public class DelayQueueTest extends JSR1
145       * A new queue has unbounded capacity
146       */
147      public void testConstructor1() {
148 <        assertEquals(NOCAP, new DelayQueue().remainingCapacity());
148 >        assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity());
149      }
150  
151      /**
# Line 134 | Line 153 | public class DelayQueueTest extends JSR1
153       */
154      public void testConstructor3() {
155          try {
156 <            DelayQueue q = new DelayQueue(null);
156 >            new DelayQueue(null);
157              shouldThrow();
158          } catch (NullPointerException success) {}
159      }
# Line 144 | Line 163 | public class DelayQueueTest extends JSR1
163       */
164      public void testConstructor4() {
165          try {
166 <            PDelay[] ints = new PDelay[SIZE];
148 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
166 >            new DelayQueue(Arrays.asList(new PDelay[SIZE]));
167              shouldThrow();
168          } catch (NullPointerException success) {}
169      }
# Line 154 | Line 172 | public class DelayQueueTest extends JSR1
172       * Initializing from Collection with some null elements throws NPE
173       */
174      public void testConstructor5() {
175 +        PDelay[] a = new PDelay[SIZE];
176 +        for (int i = 0; i < SIZE - 1; ++i)
177 +            a[i] = new PDelay(i);
178          try {
179 <            PDelay[] ints = new PDelay[SIZE];
159 <            for (int i = 0; i < SIZE-1; ++i)
160 <                ints[i] = new PDelay(i);
161 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
179 >            new DelayQueue(Arrays.asList(a));
180              shouldThrow();
181          } catch (NullPointerException success) {}
182      }
# Line 181 | Line 199 | public class DelayQueueTest extends JSR1
199      public void testEmpty() {
200          DelayQueue q = new DelayQueue();
201          assertTrue(q.isEmpty());
202 <        assertEquals(NOCAP, q.remainingCapacity());
202 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
203          q.add(new PDelay(1));
204          assertFalse(q.isEmpty());
205          q.add(new PDelay(2));
# Line 191 | Line 209 | public class DelayQueueTest extends JSR1
209      }
210  
211      /**
212 <     * remainingCapacity does not change when elements added or removed,
195 <     * but size does
212 >     * remainingCapacity() always returns Integer.MAX_VALUE
213       */
214      public void testRemainingCapacity() {
215 <        DelayQueue q = populatedQueue(SIZE);
215 >        BlockingQueue q = populatedQueue(SIZE);
216          for (int i = 0; i < SIZE; ++i) {
217 <            assertEquals(NOCAP, q.remainingCapacity());
218 <            assertEquals(SIZE-i, q.size());
219 <            q.remove();
217 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
218 >            assertEquals(SIZE - i, q.size());
219 >            assertTrue(q.remove() instanceof PDelay);
220          }
221          for (int i = 0; i < SIZE; ++i) {
222 <            assertEquals(NOCAP, q.remainingCapacity());
222 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
223              assertEquals(i, q.size());
224 <            q.add(new PDelay(i));
224 >            assertTrue(q.add(new PDelay(i)));
225          }
226      }
227  
228      /**
212     * offer(null) throws NPE
213     */
214    public void testOfferNull() {
215        try {
216            DelayQueue q = new DelayQueue();
217            q.offer(null);
218            shouldThrow();
219        } catch (NullPointerException success) {}
220    }
221
222    /**
223     * add(null) throws NPE
224     */
225    public void testAddNull() {
226        try {
227            DelayQueue q = new DelayQueue();
228            q.add(null);
229            shouldThrow();
230        } catch (NullPointerException success) {}
231    }
232
233    /**
229       * offer non-null succeeds
230       */
231      public void testOffer() {
# Line 251 | Line 246 | public class DelayQueueTest extends JSR1
246      }
247  
248      /**
254     * addAll(null) throws NPE
255     */
256    public void testAddAll1() {
257        try {
258            DelayQueue q = new DelayQueue();
259            q.addAll(null);
260            shouldThrow();
261        } catch (NullPointerException success) {}
262    }
263
264
265    /**
249       * addAll(this) throws IAE
250       */
251      public void testAddAllSelf() {
252 +        DelayQueue q = populatedQueue(SIZE);
253          try {
270            DelayQueue q = populatedQueue(SIZE);
254              q.addAll(q);
255              shouldThrow();
256          } catch (IllegalArgumentException success) {}
257      }
258  
259      /**
277     * addAll of a collection with null elements throws NPE
278     */
279    public void testAddAll2() {
280        try {
281            DelayQueue q = new DelayQueue();
282            PDelay[] ints = new PDelay[SIZE];
283            q.addAll(Arrays.asList(ints));
284            shouldThrow();
285        } catch (NullPointerException success) {}
286    }
287
288    /**
260       * addAll of a collection with any null elements throws NPE after
261       * possibly adding some elements
262       */
263      public void testAddAll3() {
264 +        DelayQueue q = new DelayQueue();
265 +        PDelay[] a = new PDelay[SIZE];
266 +        for (int i = 0; i < SIZE - 1; ++i)
267 +            a[i] = new PDelay(i);
268          try {
269 <            DelayQueue q = new DelayQueue();
295 <            PDelay[] ints = new PDelay[SIZE];
296 <            for (int i = 0; i < SIZE-1; ++i)
297 <                ints[i] = new PDelay(i);
298 <            q.addAll(Arrays.asList(ints));
269 >            q.addAll(Arrays.asList(a));
270              shouldThrow();
271          } catch (NullPointerException success) {}
272      }
# Line 306 | Line 277 | public class DelayQueueTest extends JSR1
277      public void testAddAll5() {
278          PDelay[] empty = new PDelay[0];
279          PDelay[] ints = new PDelay[SIZE];
280 <        for (int i = SIZE-1; i >= 0; --i)
280 >        for (int i = SIZE - 1; i >= 0; --i)
281              ints[i] = new PDelay(i);
282          DelayQueue q = new DelayQueue();
283          assertFalse(q.addAll(Arrays.asList(empty)));
# Line 316 | Line 287 | public class DelayQueueTest extends JSR1
287      }
288  
289      /**
319     * put(null) throws NPE
320     */
321     public void testPutNull() {
322        try {
323            DelayQueue q = new DelayQueue();
324            q.put(null);
325            shouldThrow();
326        } catch (NullPointerException success) {}
327     }
328
329    /**
290       * all elements successfully put are contained
291       */
292 <     public void testPut() {
293 <         DelayQueue q = new DelayQueue();
294 <         for (int i = 0; i < SIZE; ++i) {
295 <             PDelay I = new PDelay(i);
296 <             q.put(I);
297 <             assertTrue(q.contains(I));
298 <         }
299 <         assertEquals(SIZE, q.size());
292 >    public void testPut() {
293 >        DelayQueue q = new DelayQueue();
294 >        for (int i = 0; i < SIZE; ++i) {
295 >            PDelay x = new PDelay(i);
296 >            q.put(x);
297 >            assertTrue(q.contains(x));
298 >        }
299 >        assertEquals(SIZE, q.size());
300      }
301  
302      /**
# Line 344 | Line 304 | public class DelayQueueTest extends JSR1
304       */
305      public void testPutWithTake() throws InterruptedException {
306          final DelayQueue q = new DelayQueue();
307 <        Thread t = new Thread(new CheckedRunnable() {
307 >        Thread t = newStartedThread(new CheckedRunnable() {
308              public void realRun() {
309                  q.put(new PDelay(0));
310                  q.put(new PDelay(0));
# Line 352 | Line 312 | public class DelayQueueTest extends JSR1
312                  q.put(new PDelay(0));
313              }});
314  
315 <        t.start();
316 <        Thread.sleep(SHORT_DELAY_MS);
357 <        q.take();
358 <        t.interrupt();
359 <        t.join();
315 >        awaitTermination(t);
316 >        assertEquals(4, q.size());
317      }
318  
319      /**
# Line 364 | Line 321 | public class DelayQueueTest extends JSR1
321       */
322      public void testTimedOffer() throws InterruptedException {
323          final DelayQueue q = new DelayQueue();
324 <        Thread t = new Thread(new CheckedRunnable() {
324 >        Thread t = newStartedThread(new CheckedRunnable() {
325              public void realRun() throws InterruptedException {
326                  q.put(new PDelay(0));
327                  q.put(new PDelay(0));
# Line 372 | Line 329 | public class DelayQueueTest extends JSR1
329                  assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
330              }});
331  
332 <        t.start();
376 <        Thread.sleep(SMALL_DELAY_MS);
377 <        t.interrupt();
378 <        t.join();
332 >        awaitTermination(t);
333      }
334  
335      /**
# Line 384 | Line 338 | public class DelayQueueTest extends JSR1
338      public void testTake() throws InterruptedException {
339          DelayQueue q = populatedQueue(SIZE);
340          for (int i = 0; i < SIZE; ++i) {
341 <            assertEquals(new PDelay(i), ((PDelay)q.take()));
341 >            assertEquals(new PDelay(i), q.take());
342          }
343      }
344  
345      /**
392     * take blocks interruptibly when empty
393     */
394    public void testTakeFromEmptyBlocksInterruptibly()
395            throws InterruptedException {
396        final BlockingQueue q = new DelayQueue();
397        final CountDownLatch threadStarted = new CountDownLatch(1);
398        Thread t = newStartedThread(new CheckedRunnable() {
399            public void realRun() {
400                long t0 = System.nanoTime();
401                threadStarted.countDown();
402                try {
403                    q.take();
404                    shouldThrow();
405                } catch (InterruptedException expected) {}
406                assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS);
407            }});
408        threadStarted.await();
409        Thread.sleep(SHORT_DELAY_MS);
410        assertTrue(t.isAlive());
411        t.interrupt();
412        awaitTermination(t, MEDIUM_DELAY_MS);
413        assertFalse(t.isAlive());
414    }
415
416    /**
346       * Take removes existing elements until empty, then blocks interruptibly
347       */
348      public void testBlockingTake() throws InterruptedException {
349          final DelayQueue q = populatedQueue(SIZE);
350 <        Thread t = new Thread(new CheckedRunnable() {
350 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
351 >        Thread t = newStartedThread(new CheckedRunnable() {
352              public void realRun() throws InterruptedException {
353                  for (int i = 0; i < SIZE; ++i) {
354                      assertEquals(new PDelay(i), ((PDelay)q.take()));
355                  }
356 +
357 +                Thread.currentThread().interrupt();
358                  try {
359                      q.take();
360                      shouldThrow();
361                  } catch (InterruptedException success) {}
362 +                assertFalse(Thread.interrupted());
363 +
364 +                pleaseInterrupt.countDown();
365 +                try {
366 +                    q.take();
367 +                    shouldThrow();
368 +                } catch (InterruptedException success) {}
369 +                assertFalse(Thread.interrupted());
370              }});
371  
372 <        t.start();
373 <        Thread.sleep(SHORT_DELAY_MS);
372 >        await(pleaseInterrupt);
373 >        assertThreadStaysAlive(t);
374          t.interrupt();
375 <        t.join();
375 >        awaitTermination(t);
376      }
377  
438
378      /**
379       * poll succeeds unless empty
380       */
381      public void testPoll() {
382          DelayQueue q = populatedQueue(SIZE);
383          for (int i = 0; i < SIZE; ++i) {
384 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
384 >            assertEquals(new PDelay(i), q.poll());
385          }
386          assertNull(q.poll());
387      }
# Line 453 | Line 392 | public class DelayQueueTest extends JSR1
392      public void testTimedPoll0() throws InterruptedException {
393          DelayQueue q = populatedQueue(SIZE);
394          for (int i = 0; i < SIZE; ++i) {
395 <            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
395 >            assertEquals(new PDelay(i), q.poll(0, MILLISECONDS));
396          }
397          assertNull(q.poll(0, MILLISECONDS));
398      }
# Line 464 | Line 403 | public class DelayQueueTest extends JSR1
403      public void testTimedPoll() throws InterruptedException {
404          DelayQueue q = populatedQueue(SIZE);
405          for (int i = 0; i < SIZE; ++i) {
406 <            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
407 <        }
408 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
406 >            long startTime = System.nanoTime();
407 >            assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS));
408 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
409 >        }
410 >        long startTime = System.nanoTime();
411 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
412 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
413 >        checkEmpty(q);
414      }
415  
416      /**
# Line 474 | Line 418 | public class DelayQueueTest extends JSR1
418       * returning timeout status
419       */
420      public void testInterruptedTimedPoll() throws InterruptedException {
421 <        Thread t = new Thread(new CheckedRunnable() {
421 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
422 >        final DelayQueue q = populatedQueue(SIZE);
423 >        Thread t = newStartedThread(new CheckedRunnable() {
424              public void realRun() throws InterruptedException {
425 <                DelayQueue q = populatedQueue(SIZE);
425 >                long startTime = System.nanoTime();
426                  for (int i = 0; i < SIZE; ++i) {
427 <                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
427 >                    assertEquals(new PDelay(i),
428 >                                 ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
429                  }
483                try {
484                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
485                    shouldThrow();
486                } catch (InterruptedException success) {}
487            }});
488
489        t.start();
490        Thread.sleep(SHORT_DELAY_MS);
491        t.interrupt();
492        t.join();
493    }
494
495    /**
496     * timed poll before a delayed offer fails; after offer succeeds;
497     * on interruption throws
498     */
499    public void testTimedPollWithOffer() throws InterruptedException {
500        final DelayQueue q = new DelayQueue();
501        final PDelay pdelay = new PDelay(0);
502        final CheckedBarrier barrier = new CheckedBarrier(2);
503        Thread t = new Thread(new CheckedRunnable() {
504            public void realRun() throws InterruptedException {
505                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
506
507                barrier.await();
508                assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
430  
431                  Thread.currentThread().interrupt();
432                  try {
433 <                    q.poll(SHORT_DELAY_MS, MILLISECONDS);
433 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
434                      shouldThrow();
435                  } catch (InterruptedException success) {}
436 +                assertFalse(Thread.interrupted());
437  
438 <                barrier.await();
438 >                pleaseInterrupt.countDown();
439                  try {
440 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
440 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
441                      shouldThrow();
442                  } catch (InterruptedException success) {}
443 +                assertFalse(Thread.interrupted());
444 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
445              }});
446  
447 <        t.start();
448 <        barrier.await();
525 <        assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS));
526 <        barrier.await();
527 <        sleep(SHORT_DELAY_MS);
447 >        await(pleaseInterrupt);
448 >        assertThreadStaysAlive(t);
449          t.interrupt();
450 <        t.join();
450 >        awaitTermination(t);
451 >        checkEmpty(q);
452      }
453  
532
454      /**
455       * peek returns next element, or null if empty
456       */
457      public void testPeek() {
458          DelayQueue q = populatedQueue(SIZE);
459          for (int i = 0; i < SIZE; ++i) {
460 <            assertEquals(new PDelay(i), ((PDelay)q.peek()));
461 <            assertEquals(new PDelay(i), ((PDelay)q.poll()));
460 >            assertEquals(new PDelay(i), q.peek());
461 >            assertEquals(new PDelay(i), q.poll());
462              if (q.isEmpty())
463                  assertNull(q.peek());
464              else
# Line 552 | Line 473 | public class DelayQueueTest extends JSR1
473      public void testElement() {
474          DelayQueue q = populatedQueue(SIZE);
475          for (int i = 0; i < SIZE; ++i) {
476 <            assertEquals(new PDelay(i), ((PDelay)q.element()));
476 >            assertEquals(new PDelay(i), q.element());
477              q.poll();
478          }
479          try {
# Line 567 | Line 488 | public class DelayQueueTest extends JSR1
488      public void testRemove() {
489          DelayQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(new PDelay(i), ((PDelay)q.remove()));
491 >            assertEquals(new PDelay(i), q.remove());
492          }
493          try {
494              q.remove();
# Line 576 | Line 497 | public class DelayQueueTest extends JSR1
497      }
498  
499      /**
579     * remove(x) removes x and returns true if present
580     */
581    public void testRemoveElement() {
582        DelayQueue q = populatedQueue(SIZE);
583        for (int i = 1; i < SIZE; i+=2) {
584            assertTrue(q.remove(new PDelay(i)));
585        }
586        for (int i = 0; i < SIZE; i+=2) {
587            assertTrue(q.remove(new PDelay(i)));
588            assertFalse(q.remove(new PDelay(i+1)));
589        }
590        assertTrue(q.isEmpty());
591    }
592
593    /**
500       * contains(x) reports true when elements added but not yet removed
501       */
502      public void testContains() {
# Line 610 | Line 516 | public class DelayQueueTest extends JSR1
516          q.clear();
517          assertTrue(q.isEmpty());
518          assertEquals(0, q.size());
519 <        assertEquals(NOCAP, q.remainingCapacity());
519 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
520          PDelay x = new PDelay(1);
521          q.add(x);
522          assertFalse(q.isEmpty());
# Line 647 | Line 553 | public class DelayQueueTest extends JSR1
553                  assertTrue(changed);
554  
555              assertTrue(q.containsAll(p));
556 <            assertEquals(SIZE-i, q.size());
556 >            assertEquals(SIZE - i, q.size());
557              p.remove();
558          }
559      }
# Line 660 | Line 566 | public class DelayQueueTest extends JSR1
566              DelayQueue q = populatedQueue(SIZE);
567              DelayQueue p = populatedQueue(i);
568              assertTrue(q.removeAll(p));
569 <            assertEquals(SIZE-i, q.size());
569 >            assertEquals(SIZE - i, q.size());
570              for (int j = 0; j < i; ++j) {
571 <                PDelay I = (PDelay)(p.remove());
572 <                assertFalse(q.contains(I));
571 >                PDelay x = (PDelay)(p.remove());
572 >                assertFalse(q.contains(x));
573              }
574          }
575      }
# Line 683 | Line 589 | public class DelayQueueTest extends JSR1
589       * toArray(a) contains all elements
590       */
591      public void testToArray2() {
592 <        DelayQueue q = populatedQueue(SIZE);
592 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
593          PDelay[] ints = new PDelay[SIZE];
594 <        assertSame(ints, q.toArray(ints));
594 >        PDelay[] array = q.toArray(ints);
595 >        assertSame(ints, array);
596          Arrays.sort(ints);
597          for (int i = 0; i < ints.length; i++)
598              assertSame(ints[i], q.remove());
599      }
600  
694
695    /**
696     * toArray(null) throws NullPointerException
697     */
698    public void testToArray_NullArg() {
699        DelayQueue q = populatedQueue(SIZE);
700        try {
701            q.toArray(null);
702            shouldThrow();
703        } catch (NullPointerException success) {}
704    }
705
601      /**
602       * toArray(incompatible array type) throws ArrayStoreException
603       */
# Line 726 | Line 621 | public class DelayQueueTest extends JSR1
621              ++i;
622          }
623          assertEquals(i, SIZE);
624 +        assertIteratorExhausted(it);
625 +    }
626 +
627 +    /**
628 +     * iterator of empty collection has no elements
629 +     */
630 +    public void testEmptyIterator() {
631 +        assertIteratorExhausted(new DelayQueue().iterator());
632      }
633  
634      /**
# Line 740 | Line 643 | public class DelayQueueTest extends JSR1
643          it.next();
644          it.remove();
645          it = q.iterator();
646 <        assertEquals(it.next(), new PDelay(2));
647 <        assertEquals(it.next(), new PDelay(3));
646 >        assertEquals(new PDelay(2), it.next());
647 >        assertEquals(new PDelay(3), it.next());
648          assertFalse(it.hasNext());
649      }
650  
748
651      /**
652       * toString contains toStrings of elements
653       */
654      public void testToString() {
655          DelayQueue q = populatedQueue(SIZE);
656          String s = q.toString();
657 <        for (int i = 0; i < SIZE; ++i) {
658 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
757 <        }
657 >        for (Object e : q)
658 >            assertTrue(s.contains(e.toString()));
659      }
660  
661      /**
662 <     * offer transfers elements across Executor tasks
662 >     * timed poll transfers elements across Executor tasks
663       */
664      public void testPollInExecutor() {
665          final DelayQueue q = new DelayQueue();
666 <        ExecutorService executor = Executors.newFixedThreadPool(2);
667 <        executor.execute(new CheckedRunnable() {
668 <            public void realRun() throws InterruptedException {
669 <                assertNull(q.poll());
670 <                assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
671 <                assertTrue(q.isEmpty());
672 <            }});
673 <
674 <        executor.execute(new CheckedRunnable() {
675 <            public void realRun() throws InterruptedException {
676 <                Thread.sleep(SHORT_DELAY_MS);
677 <                q.put(new PDelay(1));
678 <            }});
679 <
680 <        joinPool(executor);
666 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
667 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
668 >        try (PoolCleaner cleaner = cleaner(executor)) {
669 >            executor.execute(new CheckedRunnable() {
670 >                public void realRun() throws InterruptedException {
671 >                    assertNull(q.poll());
672 >                    threadsStarted.await();
673 >                    assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
674 >                    checkEmpty(q);
675 >                }});
676 >
677 >            executor.execute(new CheckedRunnable() {
678 >                public void realRun() throws InterruptedException {
679 >                    threadsStarted.await();
680 >                    q.put(new PDelay(1));
681 >                }});
682 >        }
683      }
684  
782
685      /**
686       * Delayed actions do not occur until their delay elapses
687       */
# Line 809 | Line 711 | public class DelayQueueTest extends JSR1
711          assertNotNull(q.peek());
712      }
713  
812
714      /**
715       * poll of a non-empty queue returns null if no expired elements.
716       */
# Line 825 | Line 726 | public class DelayQueueTest extends JSR1
726      public void testTimedPollDelayed() throws InterruptedException {
727          DelayQueue q = new DelayQueue();
728          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
729 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
829 <    }
830 <
831 <    /**
832 <     * drainTo(null) throws NPE
833 <     */
834 <    public void testDrainToNull() {
835 <        DelayQueue q = populatedQueue(SIZE);
836 <        try {
837 <            q.drainTo(null);
838 <            shouldThrow();
839 <        } catch (NullPointerException success) {}
840 <    }
841 <
842 <    /**
843 <     * drainTo(this) throws IAE
844 <     */
845 <    public void testDrainToSelf() {
846 <        DelayQueue q = populatedQueue(SIZE);
847 <        try {
848 <            q.drainTo(q);
849 <            shouldThrow();
850 <        } catch (IllegalArgumentException success) {}
729 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
730      }
731  
732      /**
# Line 862 | Line 741 | public class DelayQueueTest extends JSR1
741          }
742          ArrayList l = new ArrayList();
743          q.drainTo(l);
744 <        assertEquals(q.size(), 0);
744 >        assertEquals(0, q.size());
745          for (int i = 0; i < SIZE; ++i)
746 <            assertEquals(l.get(i), elems[i]);
746 >            assertEquals(elems[i], l.get(i));
747          q.add(elems[0]);
748          q.add(elems[1]);
749          assertFalse(q.isEmpty());
# Line 872 | Line 751 | public class DelayQueueTest extends JSR1
751          assertTrue(q.contains(elems[1]));
752          l.clear();
753          q.drainTo(l);
754 <        assertEquals(q.size(), 0);
755 <        assertEquals(l.size(), 2);
754 >        assertEquals(0, q.size());
755 >        assertEquals(2, l.size());
756          for (int i = 0; i < 2; ++i)
757 <            assertEquals(l.get(i), elems[i]);
757 >            assertEquals(elems[i], l.get(i));
758      }
759  
760      /**
# Line 885 | Line 764 | public class DelayQueueTest extends JSR1
764          final DelayQueue q = populatedQueue(SIZE);
765          Thread t = new Thread(new CheckedRunnable() {
766              public void realRun() {
767 <                q.put(new PDelay(SIZE+1));
767 >                q.put(new PDelay(SIZE + 1));
768              }});
769  
770          t.start();
# Line 897 | Line 776 | public class DelayQueueTest extends JSR1
776      }
777  
778      /**
900     * drainTo(null, n) throws NPE
901     */
902    public void testDrainToNullN() {
903        DelayQueue q = populatedQueue(SIZE);
904        try {
905            q.drainTo(null, 0);
906            shouldThrow();
907        } catch (NullPointerException success) {}
908    }
909
910    /**
911     * drainTo(this, n) throws IAE
912     */
913    public void testDrainToSelfN() {
914        DelayQueue q = populatedQueue(SIZE);
915        try {
916            q.drainTo(q, 0);
917            shouldThrow();
918        } catch (IllegalArgumentException success) {}
919    }
920
921    /**
779       * drainTo(c, n) empties first min(n, size) elements of queue into c
780       */
781      public void testDrainToN() {
# Line 927 | Line 784 | public class DelayQueueTest extends JSR1
784              ArrayList l = new ArrayList();
785              q.drainTo(l, i);
786              int k = (i < SIZE) ? i : SIZE;
787 <            assertEquals(q.size(), SIZE-k);
788 <            assertEquals(l.size(), k);
787 >            assertEquals(SIZE - k, q.size());
788 >            assertEquals(k, l.size());
789          }
790      }
791  
792 <
792 >    /**
793 >     * remove(null), contains(null) always return false
794 >     */
795 >    public void testNeverContainsNull() {
796 >        Collection<?> q = populatedQueue(SIZE);
797 >        assertFalse(q.contains(null));
798 >        assertFalse(q.remove(null));
799 >    }
800   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines