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.18 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.63 by jsr166, Sun Nov 23 22:27:06 2014 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.*;
11 < import java.util.concurrent.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.Delayed;
18 > import java.util.concurrent.DelayQueue;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.TimeUnit;
22 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
23  
24   public class DelayQueueTest extends JSR166TestCase {
25 +
26 +    public static class Generic extends BlockingQueueTest {
27 +        protected BlockingQueue emptyCollection() {
28 +            return new DelayQueue();
29 +        }
30 +        protected PDelay makeElement(int i) {
31 +            return new PDelay(i);
32 +        }
33 +    }
34 +
35      public static void main(String[] args) {
36 <        junit.textui.TestRunner.run (suite());
36 >        junit.textui.TestRunner.run(suite());
37      }
38  
39      public static Test suite() {
40 <        return new TestSuite(DelayQueueTest.class);
40 >        return newTestSuite(DelayQueueTest.class,
41 >                            new Generic().testSuite());
42      }
43  
44      private static final int NOCAP = Integer.MAX_VALUE;
45  
46      /**
47       * A delayed implementation for testing.
48 <     * Most  tests use Pseudodelays, where delays are all elapsed
48 >     * Most tests use Pseudodelays, where delays are all elapsed
49       * (so, no blocking solely for delays) but are still ordered
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 = ((PDelay)y).pseudodelay;
57 <            if (i < j) return -1;
36 <            if (i > j) return 1;
37 <            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          }
39
59          public int compareTo(Delayed y) {
60 <            int i = pseudodelay;
42 <            int j = ((PDelay)y).pseudodelay;
43 <            if (i < j) return -1;
44 <            if (i > j) return 1;
45 <            return 0;
60 >            return compareTo((PDelay)y);
61          }
47
62          public boolean equals(Object other) {
63 <            return ((PDelay)other).pseudodelay == pseudodelay;
64 <        }
51 <        public boolean equals(PDelay other) {
52 <            return ((PDelay)other).pseudodelay == pseudodelay;
63 >            return (other instanceof PDelay) &&
64 >                this.pseudodelay == ((PDelay)other).pseudodelay;
65          }
66 <
67 <
66 >        // suppress [overrides] javac warning
67 >        public int hashCode() { return pseudodelay; }
68          public long getDelay(TimeUnit ignore) {
69 <            return pseudodelay;
69 >            return Integer.MIN_VALUE + pseudodelay;
70          }
59        public int intValue() {
60            return pseudodelay;
61        }
62
71          public String toString() {
72              return String.valueOf(pseudodelay);
73          }
74      }
75  
68
76      /**
77       * Delayed implementation that actually delays
78       */
# Line 76 | Line 83 | public class DelayQueueTest extends JSR1
83          }
84          public int compareTo(NanoDelay y) {
85              long i = trigger;
86 <            long j = ((NanoDelay)y).trigger;
86 >            long j = y.trigger;
87              if (i < j) return -1;
88              if (i > j) return 1;
89              return 0;
90          }
91  
92          public int compareTo(Delayed y) {
93 <            long i = trigger;
87 <            long j = ((NanoDelay)y).trigger;
88 <            if (i < j) return -1;
89 <            if (i > j) return 1;
90 <            return 0;
93 >            return compareTo((NanoDelay)y);
94          }
95  
96          public boolean equals(Object other) {
97 <            return ((NanoDelay)other).trigger == trigger;
97 >            return equals((NanoDelay)other);
98          }
99          public boolean equals(NanoDelay other) {
100 <            return ((NanoDelay)other).trigger == trigger;
100 >            return other.trigger == trigger;
101          }
102  
103 +        // suppress [overrides] javac warning
104 +        public int hashCode() { return (int) trigger; }
105 +
106          public long getDelay(TimeUnit unit) {
107              long n = trigger - System.nanoTime();
108              return unit.convert(n, TimeUnit.NANOSECONDS);
# Line 111 | Line 117 | public class DelayQueueTest extends JSR1
117          }
118      }
119  
114
120      /**
121 <     * Create a queue of given size containing consecutive
121 >     * Returns a new queue of given size containing consecutive
122       * PDelays 0 ... n.
123       */
124 <    private DelayQueue populatedQueue(int n) {
125 <        DelayQueue q = new DelayQueue();
124 >    private DelayQueue<PDelay> populatedQueue(int n) {
125 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
126          assertTrue(q.isEmpty());
127          for (int i = n-1; i >= 0; i-=2)
128              assertTrue(q.offer(new PDelay(i)));
# Line 143 | Line 148 | public class DelayQueueTest extends JSR1
148          try {
149              DelayQueue q = new DelayQueue(null);
150              shouldThrow();
151 <        }
147 <        catch (NullPointerException success) {}
151 >        } catch (NullPointerException success) {}
152      }
153  
154      /**
# Line 155 | Line 159 | public class DelayQueueTest extends JSR1
159              PDelay[] ints = new PDelay[SIZE];
160              DelayQueue q = new DelayQueue(Arrays.asList(ints));
161              shouldThrow();
162 <        }
159 <        catch (NullPointerException success) {}
162 >        } catch (NullPointerException success) {}
163      }
164  
165      /**
# Line 169 | Line 172 | public class DelayQueueTest extends JSR1
172                  ints[i] = new PDelay(i);
173              DelayQueue q = new DelayQueue(Arrays.asList(ints));
174              shouldThrow();
175 <        }
173 <        catch (NullPointerException success) {}
175 >        } catch (NullPointerException success) {}
176      }
177  
178      /**
179       * Queue contains all elements of collection used to initialize
180       */
181      public void testConstructor6() {
182 <        try {
183 <            PDelay[] ints = new PDelay[SIZE];
184 <            for (int i = 0; i < SIZE; ++i)
185 <                ints[i] = new PDelay(i);
186 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
187 <            for (int i = 0; i < SIZE; ++i)
186 <                assertEquals(ints[i], q.poll());
187 <        }
188 <        finally {}
182 >        PDelay[] ints = new PDelay[SIZE];
183 >        for (int i = 0; i < SIZE; ++i)
184 >            ints[i] = new PDelay(i);
185 >        DelayQueue q = new DelayQueue(Arrays.asList(ints));
186 >        for (int i = 0; i < SIZE; ++i)
187 >            assertEquals(ints[i], q.poll());
188      }
189  
190      /**
# Line 204 | Line 203 | public class DelayQueueTest extends JSR1
203      }
204  
205      /**
206 <     * remainingCapacity does not change when elementa added or removed,
206 >     * remainingCapacity does not change when elements added or removed,
207       * but size does
208       */
209      public void testRemainingCapacity() {
# Line 222 | Line 221 | public class DelayQueueTest extends JSR1
221      }
222  
223      /**
225     * offer(null) throws NPE
226     */
227    public void testOfferNull() {
228        try {
229            DelayQueue q = new DelayQueue();
230            q.offer(null);
231            shouldThrow();
232        } catch (NullPointerException success) { }
233    }
234
235    /**
236     * add(null) throws NPE
237     */
238    public void testAddNull() {
239        try {
240            DelayQueue q = new DelayQueue();
241            q.add(null);
242            shouldThrow();
243        } catch (NullPointerException success) { }
244    }
245
246    /**
224       * offer non-null succeeds
225       */
226      public void testOffer() {
# Line 264 | Line 241 | public class DelayQueueTest extends JSR1
241      }
242  
243      /**
267     * addAll(null) throws NPE
268     */
269    public void testAddAll1() {
270        try {
271            DelayQueue q = new DelayQueue();
272            q.addAll(null);
273            shouldThrow();
274        }
275        catch (NullPointerException success) {}
276    }
277
278
279    /**
244       * addAll(this) throws IAE
245       */
246      public void testAddAllSelf() {
# Line 284 | Line 248 | public class DelayQueueTest extends JSR1
248              DelayQueue q = populatedQueue(SIZE);
249              q.addAll(q);
250              shouldThrow();
251 <        }
288 <        catch (IllegalArgumentException success) {}
251 >        } catch (IllegalArgumentException success) {}
252      }
253  
254      /**
292     * addAll of a collection with null elements throws NPE
293     */
294    public void testAddAll2() {
295        try {
296            DelayQueue q = new DelayQueue();
297            PDelay[] ints = new PDelay[SIZE];
298            q.addAll(Arrays.asList(ints));
299            shouldThrow();
300        }
301        catch (NullPointerException success) {}
302    }
303    /**
255       * addAll of a collection with any null elements throws NPE after
256       * possibly adding some elements
257       */
# Line 312 | Line 263 | public class DelayQueueTest extends JSR1
263                  ints[i] = new PDelay(i);
264              q.addAll(Arrays.asList(ints));
265              shouldThrow();
266 <        }
316 <        catch (NullPointerException success) {}
266 >        } catch (NullPointerException success) {}
267      }
268  
269      /**
270       * Queue contains all elements of successful addAll
271       */
272      public void testAddAll5() {
273 <        try {
274 <            PDelay[] empty = new PDelay[0];
275 <            PDelay[] ints = new PDelay[SIZE];
276 <            for (int i = SIZE-1; i >= 0; --i)
277 <                ints[i] = new PDelay(i);
278 <            DelayQueue q = new DelayQueue();
279 <            assertFalse(q.addAll(Arrays.asList(empty)));
280 <            assertTrue(q.addAll(Arrays.asList(ints)));
281 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
273 >        PDelay[] empty = new PDelay[0];
274 >        PDelay[] ints = new PDelay[SIZE];
275 >        for (int i = SIZE-1; i >= 0; --i)
276 >            ints[i] = new PDelay(i);
277 >        DelayQueue q = new DelayQueue();
278 >        assertFalse(q.addAll(Arrays.asList(empty)));
279 >        assertTrue(q.addAll(Arrays.asList(ints)));
280 >        for (int i = 0; i < SIZE; ++i)
281 >            assertEquals(ints[i], q.poll());
282      }
283  
284      /**
338     * put(null) throws NPE
339     */
340     public void testPutNull() {
341        try {
342            DelayQueue q = new DelayQueue();
343            q.put(null);
344            shouldThrow();
345        }
346        catch (NullPointerException success) {
347        }
348     }
349
350    /**
285       * all elements successfully put are contained
286       */
287 <     public void testPut() {
288 <         try {
289 <             DelayQueue q = new DelayQueue();
290 <             for (int i = 0; i < SIZE; ++i) {
291 <                 PDelay I = new PDelay(i);
292 <                 q.put(I);
359 <                 assertTrue(q.contains(I));
360 <             }
361 <             assertEquals(SIZE, q.size());
362 <         }
363 <         finally {
287 >    public void testPut() {
288 >        DelayQueue q = new DelayQueue();
289 >        for (int i = 0; i < SIZE; ++i) {
290 >            PDelay I = new PDelay(i);
291 >            q.put(I);
292 >            assertTrue(q.contains(I));
293          }
294 +        assertEquals(SIZE, q.size());
295      }
296  
297      /**
298       * put doesn't block waiting for take
299       */
300 <    public void testPutWithTake() {
300 >    public void testPutWithTake() throws InterruptedException {
301          final DelayQueue q = new DelayQueue();
302 <        Thread t = new Thread(new Runnable() {
303 <                public void run() {
304 <                    int added = 0;
305 <                    try {
306 <                        q.put(new PDelay(0));
307 <                        ++added;
308 <                        q.put(new PDelay(0));
309 <                        ++added;
310 <                        q.put(new PDelay(0));
311 <                        ++added;
382 <                        q.put(new PDelay(0));
383 <                        ++added;
384 <                        threadAssertTrue(added == 4);
385 <                    } finally {
386 <                    }
387 <                }
388 <            });
389 <        try {
390 <            t.start();
391 <            Thread.sleep(SHORT_DELAY_MS);
392 <            q.take();
393 <            t.interrupt();
394 <            t.join();
395 <        } catch (Exception e) {
396 <            unexpectedException();
397 <        }
302 >        Thread t = newStartedThread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                q.put(new PDelay(0));
305 >                q.put(new PDelay(0));
306 >                q.put(new PDelay(0));
307 >                q.put(new PDelay(0));
308 >            }});
309 >
310 >        awaitTermination(t);
311 >        assertEquals(4, q.size());
312      }
313  
314      /**
315       * timed offer does not time out
316       */
317 <    public void testTimedOffer() {
317 >    public void testTimedOffer() throws InterruptedException {
318          final DelayQueue q = new DelayQueue();
319 <        Thread t = new Thread(new Runnable() {
320 <                public void run() {
321 <                    try {
322 <                        q.put(new PDelay(0));
323 <                        q.put(new PDelay(0));
324 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
325 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
412 <                    } finally { }
413 <                }
414 <            });
319 >        Thread t = newStartedThread(new CheckedRunnable() {
320 >            public void realRun() throws InterruptedException {
321 >                q.put(new PDelay(0));
322 >                q.put(new PDelay(0));
323 >                assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
324 >                assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
325 >            }});
326  
327 <        try {
417 <            t.start();
418 <            Thread.sleep(SMALL_DELAY_MS);
419 <            t.interrupt();
420 <            t.join();
421 <        } catch (Exception e) {
422 <            unexpectedException();
423 <        }
327 >        awaitTermination(t);
328      }
329  
330      /**
331       * take retrieves elements in priority order
332       */
333 <    public void testTake() {
334 <        try {
335 <            DelayQueue q = populatedQueue(SIZE);
336 <            for (int i = 0; i < SIZE; ++i) {
433 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
434 <            }
435 <        } catch (InterruptedException e) {
436 <            unexpectedException();
333 >    public void testTake() throws InterruptedException {
334 >        DelayQueue q = populatedQueue(SIZE);
335 >        for (int i = 0; i < SIZE; ++i) {
336 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
337          }
338      }
339  
340      /**
341 <     * take blocks interruptibly when empty
341 >     * Take removes existing elements until empty, then blocks interruptibly
342       */
343 <    public void testTakeFromEmpty() {
344 <        final DelayQueue q = new DelayQueue();
345 <        Thread t = new Thread(new Runnable() {
346 <                public void run() {
347 <                    try {
348 <                        q.take();
349 <                        threadShouldThrow();
450 <                    } catch (InterruptedException success) { }
343 >    public void testBlockingTake() throws InterruptedException {
344 >        final DelayQueue q = populatedQueue(SIZE);
345 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
346 >        Thread t = newStartedThread(new CheckedRunnable() {
347 >            public void realRun() throws InterruptedException {
348 >                for (int i = 0; i < SIZE; ++i) {
349 >                    assertEquals(new PDelay(i), ((PDelay)q.take()));
350                  }
452            });
453        try {
454            t.start();
455            Thread.sleep(SHORT_DELAY_MS);
456            t.interrupt();
457            t.join();
458        } catch (Exception e) {
459            unexpectedException();
460        }
461    }
351  
352 <    /**
353 <     * Take removes existing elements until empty, then blocks interruptibly
354 <     */
355 <    public void testBlockingTake() {
356 <        Thread t = new Thread(new Runnable() {
357 <                public void run() {
469 <                    try {
470 <                        DelayQueue q = populatedQueue(SIZE);
471 <                        for (int i = 0; i < SIZE; ++i) {
472 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
473 <                        }
474 <                        q.take();
475 <                        threadShouldThrow();
476 <                    } catch (InterruptedException success) {
477 <                    }
478 <                }});
479 <        t.start();
480 <        try {
481 <           Thread.sleep(SHORT_DELAY_MS);
482 <           t.interrupt();
483 <           t.join();
484 <        }
485 <        catch (InterruptedException ie) {
486 <            unexpectedException();
487 <        }
488 <    }
352 >                Thread.currentThread().interrupt();
353 >                try {
354 >                    q.take();
355 >                    shouldThrow();
356 >                } catch (InterruptedException success) {}
357 >                assertFalse(Thread.interrupted());
358  
359 +                pleaseInterrupt.countDown();
360 +                try {
361 +                    q.take();
362 +                    shouldThrow();
363 +                } catch (InterruptedException success) {}
364 +                assertFalse(Thread.interrupted());
365 +            }});
366 +
367 +        await(pleaseInterrupt);
368 +        assertThreadStaysAlive(t);
369 +        t.interrupt();
370 +        awaitTermination(t);
371 +    }
372  
373      /**
374       * poll succeeds unless empty
# Line 500 | Line 382 | public class DelayQueueTest extends JSR1
382      }
383  
384      /**
385 <     * timed pool with zero timeout succeeds when non-empty, else times out
385 >     * timed poll with zero timeout succeeds when non-empty, else times out
386       */
387 <    public void testTimedPoll0() {
388 <        try {
389 <            DelayQueue q = populatedQueue(SIZE);
390 <            for (int i = 0; i < SIZE; ++i) {
509 <                assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
510 <            }
511 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
512 <        } catch (InterruptedException e) {
513 <            unexpectedException();
387 >    public void testTimedPoll0() throws InterruptedException {
388 >        DelayQueue q = populatedQueue(SIZE);
389 >        for (int i = 0; i < SIZE; ++i) {
390 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
391          }
392 +        assertNull(q.poll(0, MILLISECONDS));
393      }
394  
395      /**
396 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
396 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
397       */
398 <    public void testTimedPoll() {
399 <        try {
400 <            DelayQueue q = populatedQueue(SIZE);
401 <            for (int i = 0; i < SIZE; ++i) {
402 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
403 <            }
526 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
527 <        } catch (InterruptedException e) {
528 <            unexpectedException();
398 >    public void testTimedPoll() throws InterruptedException {
399 >        DelayQueue q = populatedQueue(SIZE);
400 >        for (int i = 0; i < SIZE; ++i) {
401 >            long startTime = System.nanoTime();
402 >            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
403 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
404          }
405 +        long startTime = System.nanoTime();
406 +        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
407 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
408 +        checkEmpty(q);
409      }
410  
411      /**
412       * Interrupted timed poll throws InterruptedException instead of
413       * returning timeout status
414       */
415 <    public void testInterruptedTimedPoll() {
416 <        Thread t = new Thread(new Runnable() {
417 <                public void run() {
418 <                    try {
419 <                        DelayQueue q = populatedQueue(SIZE);
420 <                        for (int i = 0; i < SIZE; ++i) {
421 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
543 <                        }
544 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
545 <                    } catch (InterruptedException success) {
546 <                    }
547 <                }});
548 <        t.start();
549 <        try {
550 <           Thread.sleep(SHORT_DELAY_MS);
551 <           t.interrupt();
552 <           t.join();
553 <        }
554 <        catch (InterruptedException ie) {
555 <            unexpectedException();
556 <        }
557 <    }
558 <
559 <    /**
560 <     *  timed poll before a delayed offer fails; after offer succeeds;
561 <     *  on interruption throws
562 <     */
563 <    public void testTimedPollWithOffer() {
564 <        final DelayQueue q = new DelayQueue();
565 <        Thread t = new Thread(new Runnable() {
566 <                public void run() {
567 <                    try {
568 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
569 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
570 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
571 <                        threadFail("Should block");
572 <                    } catch (InterruptedException success) { }
415 >    public void testInterruptedTimedPoll() throws InterruptedException {
416 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
417 >        Thread t = newStartedThread(new CheckedRunnable() {
418 >            public void realRun() throws InterruptedException {
419 >                DelayQueue q = populatedQueue(SIZE);
420 >                for (int i = 0; i < SIZE; ++i) {
421 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
422                  }
574            });
575        try {
576            t.start();
577            Thread.sleep(SMALL_DELAY_MS);
578            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
579            t.interrupt();
580            t.join();
581        } catch (Exception e) {
582            unexpectedException();
583        }
584    }
423  
424 +                Thread.currentThread().interrupt();
425 +                try {
426 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
427 +                    shouldThrow();
428 +                } catch (InterruptedException success) {}
429 +                assertFalse(Thread.interrupted());
430 +
431 +                pleaseInterrupt.countDown();
432 +                try {
433 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
434 +                    shouldThrow();
435 +                } catch (InterruptedException success) {}
436 +                assertFalse(Thread.interrupted());
437 +            }});
438 +
439 +        await(pleaseInterrupt);
440 +        assertThreadStaysAlive(t);
441 +        t.interrupt();
442 +        awaitTermination(t);
443 +    }
444  
445      /**
446       * peek returns next element, or null if empty
# Line 591 | Line 449 | public class DelayQueueTest extends JSR1
449          DelayQueue q = populatedQueue(SIZE);
450          for (int i = 0; i < SIZE; ++i) {
451              assertEquals(new PDelay(i), ((PDelay)q.peek()));
452 <            q.poll();
452 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
453              if (q.isEmpty())
454                  assertNull(q.peek());
455              else
456 <                assertTrue(i != ((PDelay)q.peek()).intValue());
456 >                assertFalse(new PDelay(i).equals(q.peek()));
457          }
458          assertNull(q.peek());
459      }
# Line 612 | Line 470 | public class DelayQueueTest extends JSR1
470          try {
471              q.element();
472              shouldThrow();
473 <        }
616 <        catch (NoSuchElementException success) {}
473 >        } catch (NoSuchElementException success) {}
474      }
475  
476      /**
# Line 627 | Line 484 | public class DelayQueueTest extends JSR1
484          try {
485              q.remove();
486              shouldThrow();
487 <        } catch (NoSuchElementException success) {
631 <        }
632 <    }
633 <
634 <    /**
635 <     * remove(x) removes x and returns true if present
636 <     */
637 <    public void testRemoveElement() {
638 <        DelayQueue q = populatedQueue(SIZE);
639 <        for (int i = 1; i < SIZE; i+=2) {
640 <            assertTrue(q.remove(new PDelay(i)));
641 <        }
642 <        for (int i = 0; i < SIZE; i+=2) {
643 <            assertTrue(q.remove(new PDelay(i)));
644 <            assertFalse(q.remove(new PDelay(i+1)));
645 <        }
646 <        assertTrue(q.isEmpty());
487 >        } catch (NoSuchElementException success) {}
488      }
489  
490      /**
# Line 727 | Line 568 | public class DelayQueueTest extends JSR1
568      /**
569       * toArray contains all elements
570       */
571 <    public void testToArray() {
571 >    public void testToArray() throws InterruptedException {
572          DelayQueue q = populatedQueue(SIZE);
573          Object[] o = q.toArray();
574          Arrays.sort(o);
734        try {
575          for (int i = 0; i < o.length; i++)
576 <            assertEquals(o[i], q.take());
737 <        } catch (InterruptedException e) {
738 <            unexpectedException();
739 <        }
576 >            assertSame(o[i], q.take());
577      }
578  
579      /**
580       * toArray(a) contains all elements
581       */
582      public void testToArray2() {
583 <        DelayQueue q = populatedQueue(SIZE);
583 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
584          PDelay[] ints = new PDelay[SIZE];
585 <        ints = (PDelay[])q.toArray(ints);
585 >        PDelay[] array = q.toArray(ints);
586 >        assertSame(ints, array);
587          Arrays.sort(ints);
588 <        try {
589 <            for (int i = 0; i < ints.length; i++)
752 <                assertEquals(ints[i], q.take());
753 <        } catch (InterruptedException e) {
754 <            unexpectedException();
755 <        }
588 >        for (int i = 0; i < ints.length; i++)
589 >            assertSame(ints[i], q.remove());
590      }
591  
758
592      /**
593 <     * toArray(null) throws NPE
761 <     */
762 <    public void testToArray_BadArg() {
763 <        try {
764 <            DelayQueue q = populatedQueue(SIZE);
765 <            Object o[] = q.toArray(null);
766 <            shouldThrow();
767 <        } catch (NullPointerException success) {}
768 <    }
769 <
770 <    /**
771 <     * toArray with incompatible array type throws CCE
593 >     * toArray(incompatible array type) throws ArrayStoreException
594       */
595      public void testToArray1_BadArg() {
596 +        DelayQueue q = populatedQueue(SIZE);
597          try {
598 <            DelayQueue q = populatedQueue(SIZE);
776 <            Object o[] = q.toArray(new String[10] );
598 >            q.toArray(new String[10]);
599              shouldThrow();
600 <        } catch (ArrayStoreException  success) {}
600 >        } catch (ArrayStoreException success) {}
601      }
602  
603      /**
# Line 795 | Line 617 | public class DelayQueueTest extends JSR1
617      /**
618       * iterator.remove removes current element
619       */
620 <    public void testIteratorRemove () {
620 >    public void testIteratorRemove() {
621          final DelayQueue q = new DelayQueue();
622          q.add(new PDelay(2));
623          q.add(new PDelay(1));
# Line 804 | Line 626 | public class DelayQueueTest extends JSR1
626          it.next();
627          it.remove();
628          it = q.iterator();
629 <        assertEquals(it.next(), new PDelay(2));
630 <        assertEquals(it.next(), new PDelay(3));
629 >        assertEquals(new PDelay(2), it.next());
630 >        assertEquals(new PDelay(3), it.next());
631          assertFalse(it.hasNext());
632      }
633  
812
634      /**
635       * toString contains toStrings of elements
636       */
637      public void testToString() {
638          DelayQueue q = populatedQueue(SIZE);
639          String s = q.toString();
640 <        for (int i = 0; i < SIZE; ++i) {
641 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
821 <        }
640 >        for (Object e : q)
641 >            assertTrue(s.contains(e.toString()));
642      }
643  
644      /**
645 <     * offer transfers elements across Executor tasks
645 >     * timed poll transfers elements across Executor tasks
646       */
647      public void testPollInExecutor() {
648          final DelayQueue q = new DelayQueue();
649 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
650          ExecutorService executor = Executors.newFixedThreadPool(2);
651 <        executor.execute(new Runnable() {
652 <            public void run() {
653 <                threadAssertNull(q.poll());
654 <                try {
655 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
656 <                    threadAssertTrue(q.isEmpty());
657 <                }
658 <                catch (InterruptedException e) {
659 <                    threadUnexpectedException();
660 <                }
661 <            }
662 <        });
651 >        executor.execute(new CheckedRunnable() {
652 >            public void realRun() throws InterruptedException {
653 >                assertNull(q.poll());
654 >                threadsStarted.await();
655 >                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
656 >                checkEmpty(q);
657 >            }});
658 >
659 >        executor.execute(new CheckedRunnable() {
660 >            public void realRun() throws InterruptedException {
661 >                threadsStarted.await();
662 >                q.put(new PDelay(1));
663 >            }});
664  
843        executor.execute(new Runnable() {
844            public void run() {
845                try {
846                    Thread.sleep(SHORT_DELAY_MS);
847                    q.put(new PDelay(1));
848                }
849                catch (InterruptedException e) {
850                    threadUnexpectedException();
851                }
852            }
853        });
665          joinPool(executor);
855
666      }
667  
858
668      /**
669       * Delayed actions do not occur until their delay elapses
670       */
671 <    public void testDelay() {
672 <        DelayQueue q = new DelayQueue();
673 <        NanoDelay[] elements = new NanoDelay[SIZE];
674 <        for (int i = 0; i < SIZE; ++i) {
866 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
867 <        }
868 <        for (int i = 0; i < SIZE; ++i) {
869 <            q.add(elements[i]);
870 <        }
671 >    public void testDelay() throws InterruptedException {
672 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
673 >        for (int i = 0; i < SIZE; ++i)
674 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
675  
676 <        try {
677 <            long last = 0;
678 <            for (int i = 0; i < SIZE; ++i) {
679 <                NanoDelay e = (NanoDelay)(q.take());
680 <                long tt = e.getTriggerTime();
681 <                assertTrue(tt <= System.nanoTime());
682 <                if (i != 0)
683 <                    assertTrue(tt >= last);
880 <                last = tt;
881 <            }
882 <        }
883 <        catch (InterruptedException ie) {
884 <            unexpectedException();
676 >        long last = 0;
677 >        for (int i = 0; i < SIZE; ++i) {
678 >            NanoDelay e = q.take();
679 >            long tt = e.getTriggerTime();
680 >            assertTrue(System.nanoTime() - tt >= 0);
681 >            if (i != 0)
682 >                assertTrue(tt >= last);
683 >            last = tt;
684          }
685 +        assertTrue(q.isEmpty());
686      }
687  
688      /**
# Line 891 | Line 691 | public class DelayQueueTest extends JSR1
691      public void testPeekDelayed() {
692          DelayQueue q = new DelayQueue();
693          q.add(new NanoDelay(Long.MAX_VALUE));
694 <        assert(q.peek() != null);
694 >        assertNotNull(q.peek());
695      }
696  
897
697      /**
698       * poll of a non-empty queue returns null if no expired elements.
699       */
# Line 907 | Line 706 | public class DelayQueueTest extends JSR1
706      /**
707       * timed poll of a non-empty queue returns null if no expired elements.
708       */
709 <    public void testTimedPollDelayed() {
709 >    public void testTimedPollDelayed() throws InterruptedException {
710          DelayQueue q = new DelayQueue();
711          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
712 <        try {
914 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
915 <        } catch (Exception ex) {
916 <            unexpectedException();
917 <        }
918 <    }
919 <
920 <    /**
921 <     * drainTo(null) throws NPE
922 <     */
923 <    public void testDrainToNull() {
924 <        DelayQueue q = populatedQueue(SIZE);
925 <        try {
926 <            q.drainTo(null);
927 <            shouldThrow();
928 <        } catch (NullPointerException success) {
929 <        }
930 <    }
931 <
932 <    /**
933 <     * drainTo(this) throws IAE
934 <     */
935 <    public void testDrainToSelf() {
936 <        DelayQueue q = populatedQueue(SIZE);
937 <        try {
938 <            q.drainTo(q);
939 <            shouldThrow();
940 <        } catch (IllegalArgumentException success) {
941 <        }
712 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
713      }
714  
715      /**
# Line 953 | Line 724 | public class DelayQueueTest extends JSR1
724          }
725          ArrayList l = new ArrayList();
726          q.drainTo(l);
727 <        assertEquals(q.size(), 0);
727 >        assertEquals(0, q.size());
728          for (int i = 0; i < SIZE; ++i)
729 <            assertEquals(l.get(i), elems[i]);
729 >            assertEquals(elems[i], l.get(i));
730          q.add(elems[0]);
731          q.add(elems[1]);
732          assertFalse(q.isEmpty());
# Line 963 | Line 734 | public class DelayQueueTest extends JSR1
734          assertTrue(q.contains(elems[1]));
735          l.clear();
736          q.drainTo(l);
737 <        assertEquals(q.size(), 0);
738 <        assertEquals(l.size(), 2);
737 >        assertEquals(0, q.size());
738 >        assertEquals(2, l.size());
739          for (int i = 0; i < 2; ++i)
740 <            assertEquals(l.get(i), elems[i]);
740 >            assertEquals(elems[i], l.get(i));
741      }
742  
743      /**
744       * drainTo empties queue
745       */
746 <    public void testDrainToWithActivePut() {
746 >    public void testDrainToWithActivePut() throws InterruptedException {
747          final DelayQueue q = populatedQueue(SIZE);
748 <        Thread t = new Thread(new Runnable() {
749 <                public void run() {
750 <                    q.put(new PDelay(SIZE+1));
751 <                }
981 <            });
982 <        try {
983 <            t.start();
984 <            ArrayList l = new ArrayList();
985 <            q.drainTo(l);
986 <            assertTrue(l.size() >= SIZE);
987 <            t.join();
988 <            assertTrue(q.size() + l.size() >= SIZE);
989 <        } catch (Exception e) {
990 <            unexpectedException();
991 <        }
992 <    }
993 <
994 <    /**
995 <     * drainTo(null, n) throws NPE
996 <     */
997 <    public void testDrainToNullN() {
998 <        DelayQueue q = populatedQueue(SIZE);
999 <        try {
1000 <            q.drainTo(null, 0);
1001 <            shouldThrow();
1002 <        } catch (NullPointerException success) {
1003 <        }
1004 <    }
748 >        Thread t = new Thread(new CheckedRunnable() {
749 >            public void realRun() {
750 >                q.put(new PDelay(SIZE+1));
751 >            }});
752  
753 <    /**
754 <     * drainTo(this, n) throws IAE
755 <     */
756 <    public void testDrainToSelfN() {
757 <        DelayQueue q = populatedQueue(SIZE);
758 <        try {
1012 <            q.drainTo(q, 0);
1013 <            shouldThrow();
1014 <        } catch (IllegalArgumentException success) {
1015 <        }
753 >        t.start();
754 >        ArrayList l = new ArrayList();
755 >        q.drainTo(l);
756 >        assertTrue(l.size() >= SIZE);
757 >        t.join();
758 >        assertTrue(q.size() + l.size() >= SIZE);
759      }
760  
761      /**
762 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
762 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
763       */
764      public void testDrainToN() {
765          for (int i = 0; i < SIZE + 2; ++i) {
766              DelayQueue q = populatedQueue(SIZE);
767              ArrayList l = new ArrayList();
768              q.drainTo(l, i);
769 <            int k = (i < SIZE)? i : SIZE;
770 <            assertEquals(q.size(), SIZE-k);
771 <            assertEquals(l.size(), k);
769 >            int k = (i < SIZE) ? i : SIZE;
770 >            assertEquals(SIZE-k, q.size());
771 >            assertEquals(k, l.size());
772          }
773      }
774  
775 <
775 >    /**
776 >     * remove(null), contains(null) always return false
777 >     */
778 >    public void testNeverContainsNull() {
779 >        Collection<?> q = populatedQueue(SIZE);
780 >        assertFalse(q.contains(null));
781 >        assertFalse(q.remove(null));
782 >    }
783   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines