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.19 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.53 by jsr166, Mon May 30 22:43:20 2011 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.*;
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;
12 import java.util.concurrent.*;
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 {
# Line 32 | Line 53 | public class DelayQueueTest extends JSR1
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;
56 >            int j = y.pseudodelay;
57              if (i < j) return -1;
58              if (i > j) return 1;
59              return 0;
60          }
61  
62          public int compareTo(Delayed y) {
63 <            int i = pseudodelay;
43 <            int j = ((PDelay)y).pseudodelay;
44 <            if (i < j) return -1;
45 <            if (i > j) return 1;
46 <            return 0;
63 >            return compareTo((PDelay)y);
64          }
65  
66          public boolean equals(Object other) {
67 <            return ((PDelay)other).pseudodelay == pseudodelay;
67 >            return equals((PDelay)other);
68          }
69          public boolean equals(PDelay other) {
70 <            return ((PDelay)other).pseudodelay == pseudodelay;
70 >            return other.pseudodelay == pseudodelay;
71          }
72  
56
73          public long getDelay(TimeUnit ignore) {
74              return pseudodelay;
75          }
# Line 66 | Line 82 | public class DelayQueueTest extends JSR1
82          }
83      }
84  
69
85      /**
86       * Delayed implementation that actually delays
87       */
# Line 77 | Line 92 | public class DelayQueueTest extends JSR1
92          }
93          public int compareTo(NanoDelay y) {
94              long i = trigger;
95 <            long j = ((NanoDelay)y).trigger;
95 >            long j = y.trigger;
96              if (i < j) return -1;
97              if (i > j) return 1;
98              return 0;
99          }
100  
101          public int compareTo(Delayed y) {
102 <            long i = trigger;
88 <            long j = ((NanoDelay)y).trigger;
89 <            if (i < j) return -1;
90 <            if (i > j) return 1;
91 <            return 0;
102 >            return compareTo((NanoDelay)y);
103          }
104  
105          public boolean equals(Object other) {
106 <            return ((NanoDelay)other).trigger == trigger;
106 >            return equals((NanoDelay)other);
107          }
108          public boolean equals(NanoDelay other) {
109 <            return ((NanoDelay)other).trigger == trigger;
109 >            return other.trigger == trigger;
110          }
111  
112          public long getDelay(TimeUnit unit) {
# Line 112 | Line 123 | public class DelayQueueTest extends JSR1
123          }
124      }
125  
115
126      /**
127       * Create a queue of given size containing consecutive
128       * PDelays 0 ... n.
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)
134              assertTrue(q.offer(new PDelay(i)));
# Line 144 | Line 154 | public class DelayQueueTest extends JSR1
154          try {
155              DelayQueue q = new DelayQueue(null);
156              shouldThrow();
157 <        }
148 <        catch (NullPointerException success) {}
157 >        } catch (NullPointerException success) {}
158      }
159  
160      /**
# Line 156 | Line 165 | public class DelayQueueTest extends JSR1
165              PDelay[] ints = new PDelay[SIZE];
166              DelayQueue q = new DelayQueue(Arrays.asList(ints));
167              shouldThrow();
168 <        }
160 <        catch (NullPointerException success) {}
168 >        } catch (NullPointerException success) {}
169      }
170  
171      /**
# Line 170 | Line 178 | public class DelayQueueTest extends JSR1
178                  ints[i] = new PDelay(i);
179              DelayQueue q = new DelayQueue(Arrays.asList(ints));
180              shouldThrow();
181 <        }
174 <        catch (NullPointerException success) {}
181 >        } catch (NullPointerException success) {}
182      }
183  
184      /**
185       * Queue contains all elements of collection used to initialize
186       */
187      public void testConstructor6() {
188 <        try {
189 <            PDelay[] ints = new PDelay[SIZE];
190 <            for (int i = 0; i < SIZE; ++i)
191 <                ints[i] = new PDelay(i);
192 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
193 <            for (int i = 0; i < SIZE; ++i)
187 <                assertEquals(ints[i], q.poll());
188 <        }
189 <        finally {}
188 >        PDelay[] ints = new PDelay[SIZE];
189 >        for (int i = 0; i < SIZE; ++i)
190 >            ints[i] = new PDelay(i);
191 >        DelayQueue q = new DelayQueue(Arrays.asList(ints));
192 >        for (int i = 0; i < SIZE; ++i)
193 >            assertEquals(ints[i], q.poll());
194      }
195  
196      /**
# Line 205 | Line 209 | public class DelayQueueTest extends JSR1
209      }
210  
211      /**
212 <     * remainingCapacity does not change when elementa added or removed,
212 >     * remainingCapacity does not change when elements added or removed,
213       * but size does
214       */
215      public void testRemainingCapacity() {
# Line 223 | Line 227 | public class DelayQueueTest extends JSR1
227      }
228  
229      /**
226     * offer(null) throws NPE
227     */
228    public void testOfferNull() {
229        try {
230            DelayQueue q = new DelayQueue();
231            q.offer(null);
232            shouldThrow();
233        } catch (NullPointerException success) { }
234    }
235
236    /**
237     * add(null) throws NPE
238     */
239    public void testAddNull() {
240        try {
241            DelayQueue q = new DelayQueue();
242            q.add(null);
243            shouldThrow();
244        } catch (NullPointerException success) { }
245    }
246
247    /**
230       * offer non-null succeeds
231       */
232      public void testOffer() {
# Line 265 | Line 247 | public class DelayQueueTest extends JSR1
247      }
248  
249      /**
268     * addAll(null) throws NPE
269     */
270    public void testAddAll1() {
271        try {
272            DelayQueue q = new DelayQueue();
273            q.addAll(null);
274            shouldThrow();
275        }
276        catch (NullPointerException success) {}
277    }
278
279
280    /**
250       * addAll(this) throws IAE
251       */
252      public void testAddAllSelf() {
# Line 285 | Line 254 | public class DelayQueueTest extends JSR1
254              DelayQueue q = populatedQueue(SIZE);
255              q.addAll(q);
256              shouldThrow();
257 <        }
289 <        catch (IllegalArgumentException success) {}
257 >        } catch (IllegalArgumentException success) {}
258      }
259  
260      /**
293     * addAll of a collection with null elements throws NPE
294     */
295    public void testAddAll2() {
296        try {
297            DelayQueue q = new DelayQueue();
298            PDelay[] ints = new PDelay[SIZE];
299            q.addAll(Arrays.asList(ints));
300            shouldThrow();
301        }
302        catch (NullPointerException success) {}
303    }
304    /**
261       * addAll of a collection with any null elements throws NPE after
262       * possibly adding some elements
263       */
# Line 313 | Line 269 | public class DelayQueueTest extends JSR1
269                  ints[i] = new PDelay(i);
270              q.addAll(Arrays.asList(ints));
271              shouldThrow();
272 <        }
317 <        catch (NullPointerException success) {}
272 >        } catch (NullPointerException success) {}
273      }
274  
275      /**
276       * Queue contains all elements of successful addAll
277       */
278      public void testAddAll5() {
279 <        try {
280 <            PDelay[] empty = new PDelay[0];
281 <            PDelay[] ints = new PDelay[SIZE];
282 <            for (int i = SIZE-1; i >= 0; --i)
283 <                ints[i] = new PDelay(i);
284 <            DelayQueue q = new DelayQueue();
285 <            assertFalse(q.addAll(Arrays.asList(empty)));
286 <            assertTrue(q.addAll(Arrays.asList(ints)));
287 <            for (int i = 0; i < SIZE; ++i)
333 <                assertEquals(ints[i], q.poll());
334 <        }
335 <        finally {}
279 >        PDelay[] empty = new PDelay[0];
280 >        PDelay[] ints = new PDelay[SIZE];
281 >        for (int i = SIZE-1; i >= 0; --i)
282 >            ints[i] = new PDelay(i);
283 >        DelayQueue q = new DelayQueue();
284 >        assertFalse(q.addAll(Arrays.asList(empty)));
285 >        assertTrue(q.addAll(Arrays.asList(ints)));
286 >        for (int i = 0; i < SIZE; ++i)
287 >            assertEquals(ints[i], q.poll());
288      }
289  
290      /**
339     * put(null) throws NPE
340     */
341     public void testPutNull() {
342        try {
343            DelayQueue q = new DelayQueue();
344            q.put(null);
345            shouldThrow();
346        }
347        catch (NullPointerException success) {
348        }
349     }
350
351    /**
291       * all elements successfully put are contained
292       */
293 <     public void testPut() {
294 <         try {
295 <             DelayQueue q = new DelayQueue();
296 <             for (int i = 0; i < SIZE; ++i) {
297 <                 PDelay I = new PDelay(i);
298 <                 q.put(I);
360 <                 assertTrue(q.contains(I));
361 <             }
362 <             assertEquals(SIZE, q.size());
363 <         }
364 <         finally {
293 >    public void testPut() {
294 >        DelayQueue q = new DelayQueue();
295 >        for (int i = 0; i < SIZE; ++i) {
296 >            PDelay I = new PDelay(i);
297 >            q.put(I);
298 >            assertTrue(q.contains(I));
299          }
300 +        assertEquals(SIZE, q.size());
301      }
302  
303      /**
304       * put doesn't block waiting for take
305       */
306 <    public void testPutWithTake() {
306 >    public void testPutWithTake() throws InterruptedException {
307          final DelayQueue q = new DelayQueue();
308 <        Thread t = new Thread(new Runnable() {
309 <                public void run() {
310 <                    int added = 0;
311 <                    try {
312 <                        q.put(new PDelay(0));
313 <                        ++added;
314 <                        q.put(new PDelay(0));
315 <                        ++added;
316 <                        q.put(new PDelay(0));
317 <                        ++added;
383 <                        q.put(new PDelay(0));
384 <                        ++added;
385 <                        threadAssertTrue(added == 4);
386 <                    } finally {
387 <                    }
388 <                }
389 <            });
390 <        try {
391 <            t.start();
392 <            Thread.sleep(SHORT_DELAY_MS);
393 <            q.take();
394 <            t.interrupt();
395 <            t.join();
396 <        } catch (Exception e) {
397 <            unexpectedException();
398 <        }
308 >        Thread t = newStartedThread(new CheckedRunnable() {
309 >            public void realRun() {
310 >                q.put(new PDelay(0));
311 >                q.put(new PDelay(0));
312 >                q.put(new PDelay(0));
313 >                q.put(new PDelay(0));
314 >            }});
315 >
316 >        awaitTermination(t);
317 >        assertEquals(4, q.size());
318      }
319  
320      /**
321       * timed offer does not time out
322       */
323 <    public void testTimedOffer() {
323 >    public void testTimedOffer() throws InterruptedException {
324          final DelayQueue q = new DelayQueue();
325 <        Thread t = new Thread(new Runnable() {
326 <                public void run() {
327 <                    try {
328 <                        q.put(new PDelay(0));
329 <                        q.put(new PDelay(0));
330 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
331 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
413 <                    } finally { }
414 <                }
415 <            });
325 >        Thread t = newStartedThread(new CheckedRunnable() {
326 >            public void realRun() throws InterruptedException {
327 >                q.put(new PDelay(0));
328 >                q.put(new PDelay(0));
329 >                assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
330 >                assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
331 >            }});
332  
333 <        try {
418 <            t.start();
419 <            Thread.sleep(SMALL_DELAY_MS);
420 <            t.interrupt();
421 <            t.join();
422 <        } catch (Exception e) {
423 <            unexpectedException();
424 <        }
333 >        awaitTermination(t);
334      }
335  
336      /**
337       * take retrieves elements in priority order
338       */
339 <    public void testTake() {
340 <        try {
341 <            DelayQueue q = populatedQueue(SIZE);
342 <            for (int i = 0; i < SIZE; ++i) {
434 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
435 <            }
436 <        } catch (InterruptedException e) {
437 <            unexpectedException();
339 >    public void testTake() throws InterruptedException {
340 >        DelayQueue q = populatedQueue(SIZE);
341 >        for (int i = 0; i < SIZE; ++i) {
342 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
343          }
344      }
345  
346      /**
347 <     * take blocks interruptibly when empty
347 >     * Take removes existing elements until empty, then blocks interruptibly
348       */
349 <    public void testTakeFromEmpty() {
350 <        final DelayQueue q = new DelayQueue();
351 <        Thread t = new Thread(new Runnable() {
352 <                public void run() {
353 <                    try {
354 <                        q.take();
355 <                        threadShouldThrow();
451 <                    } catch (InterruptedException success) { }
349 >    public void testBlockingTake() throws InterruptedException {
350 >        final DelayQueue q = populatedQueue(SIZE);
351 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
352 >        Thread t = newStartedThread(new CheckedRunnable() {
353 >            public void realRun() throws InterruptedException {
354 >                for (int i = 0; i < SIZE; ++i) {
355 >                    assertEquals(new PDelay(i), ((PDelay)q.take()));
356                  }
453            });
454        try {
455            t.start();
456            Thread.sleep(SHORT_DELAY_MS);
457            t.interrupt();
458            t.join();
459        } catch (Exception e) {
460            unexpectedException();
461        }
462    }
357  
358 <    /**
359 <     * Take removes existing elements until empty, then blocks interruptibly
360 <     */
361 <    public void testBlockingTake() {
362 <        Thread t = new Thread(new Runnable() {
363 <                public void run() {
470 <                    try {
471 <                        DelayQueue q = populatedQueue(SIZE);
472 <                        for (int i = 0; i < SIZE; ++i) {
473 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
474 <                        }
475 <                        q.take();
476 <                        threadShouldThrow();
477 <                    } catch (InterruptedException success) {
478 <                    }
479 <                }});
480 <        t.start();
481 <        try {
482 <           Thread.sleep(SHORT_DELAY_MS);
483 <           t.interrupt();
484 <           t.join();
485 <        }
486 <        catch (InterruptedException ie) {
487 <            unexpectedException();
488 <        }
489 <    }
358 >                Thread.currentThread().interrupt();
359 >                try {
360 >                    q.take();
361 >                    shouldThrow();
362 >                } catch (InterruptedException success) {}
363 >                assertFalse(Thread.interrupted());
364  
365 +                pleaseInterrupt.countDown();
366 +                try {
367 +                    q.take();
368 +                    shouldThrow();
369 +                } catch (InterruptedException success) {}
370 +                assertFalse(Thread.interrupted());
371 +            }});
372 +
373 +        await(pleaseInterrupt);
374 +        assertThreadStaysAlive(t);
375 +        t.interrupt();
376 +        awaitTermination(t);
377 +    }
378  
379      /**
380       * poll succeeds unless empty
# Line 501 | Line 388 | public class DelayQueueTest extends JSR1
388      }
389  
390      /**
391 <     * timed pool with zero timeout succeeds when non-empty, else times out
391 >     * timed poll with zero timeout succeeds when non-empty, else times out
392       */
393 <    public void testTimedPoll0() {
394 <        try {
395 <            DelayQueue q = populatedQueue(SIZE);
396 <            for (int i = 0; i < SIZE; ++i) {
510 <                assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
511 <            }
512 <            assertNull(q.poll(0, MILLISECONDS));
513 <        } catch (InterruptedException e) {
514 <            unexpectedException();
393 >    public void testTimedPoll0() throws InterruptedException {
394 >        DelayQueue q = populatedQueue(SIZE);
395 >        for (int i = 0; i < SIZE; ++i) {
396 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
397          }
398 +        assertNull(q.poll(0, MILLISECONDS));
399      }
400  
401      /**
402 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
402 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
403       */
404 <    public void testTimedPoll() {
405 <        try {
406 <            DelayQueue q = populatedQueue(SIZE);
407 <            for (int i = 0; i < SIZE; ++i) {
408 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
409 <            }
527 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
528 <        } catch (InterruptedException e) {
529 <            unexpectedException();
404 >    public void testTimedPoll() throws InterruptedException {
405 >        DelayQueue q = populatedQueue(SIZE);
406 >        for (int i = 0; i < SIZE; ++i) {
407 >            long startTime = System.nanoTime();
408 >            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
409 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
410          }
411 +        long startTime = System.nanoTime();
412 +        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
413 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
414 +        checkEmpty(q);
415      }
416  
417      /**
418       * Interrupted timed poll throws InterruptedException instead of
419       * returning timeout status
420       */
421 <    public void testInterruptedTimedPoll() {
422 <        Thread t = new Thread(new Runnable() {
423 <                public void run() {
424 <                    try {
425 <                        DelayQueue q = populatedQueue(SIZE);
426 <                        for (int i = 0; i < SIZE; ++i) {
427 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
544 <                        }
545 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
546 <                    } catch (InterruptedException success) {
547 <                    }
548 <                }});
549 <        t.start();
550 <        try {
551 <           Thread.sleep(SHORT_DELAY_MS);
552 <           t.interrupt();
553 <           t.join();
554 <        }
555 <        catch (InterruptedException ie) {
556 <            unexpectedException();
557 <        }
558 <    }
559 <
560 <    /**
561 <     *  timed poll before a delayed offer fails; after offer succeeds;
562 <     *  on interruption throws
563 <     */
564 <    public void testTimedPollWithOffer() {
565 <        final DelayQueue q = new DelayQueue();
566 <        Thread t = new Thread(new Runnable() {
567 <                public void run() {
568 <                    try {
569 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
570 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
571 <                        q.poll(LONG_DELAY_MS, MILLISECONDS);
572 <                        threadFail("Should block");
573 <                    } catch (InterruptedException success) { }
421 >    public void testInterruptedTimedPoll() throws InterruptedException {
422 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
423 >        Thread t = newStartedThread(new CheckedRunnable() {
424 >            public void realRun() throws InterruptedException {
425 >                DelayQueue q = populatedQueue(SIZE);
426 >                for (int i = 0; i < SIZE; ++i) {
427 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
428                  }
575            });
576        try {
577            t.start();
578            Thread.sleep(SMALL_DELAY_MS);
579            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
580            t.interrupt();
581            t.join();
582        } catch (Exception e) {
583            unexpectedException();
584        }
585    }
429  
430 +                Thread.currentThread().interrupt();
431 +                try {
432 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
433 +                    shouldThrow();
434 +                } catch (InterruptedException success) {}
435 +                assertFalse(Thread.interrupted());
436 +
437 +                pleaseInterrupt.countDown();
438 +                try {
439 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
440 +                    shouldThrow();
441 +                } catch (InterruptedException success) {}
442 +                assertFalse(Thread.interrupted());
443 +            }});
444 +
445 +        await(pleaseInterrupt);
446 +        assertThreadStaysAlive(t);
447 +        t.interrupt();
448 +        awaitTermination(t);
449 +    }
450  
451      /**
452       * peek returns next element, or null if empty
# Line 592 | Line 455 | public class DelayQueueTest extends JSR1
455          DelayQueue q = populatedQueue(SIZE);
456          for (int i = 0; i < SIZE; ++i) {
457              assertEquals(new PDelay(i), ((PDelay)q.peek()));
458 <            q.poll();
458 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
459              if (q.isEmpty())
460                  assertNull(q.peek());
461              else
462 <                assertTrue(i != ((PDelay)q.peek()).intValue());
462 >                assertFalse(new PDelay(i).equals(q.peek()));
463          }
464          assertNull(q.peek());
465      }
# Line 613 | Line 476 | public class DelayQueueTest extends JSR1
476          try {
477              q.element();
478              shouldThrow();
479 <        }
617 <        catch (NoSuchElementException success) {}
479 >        } catch (NoSuchElementException success) {}
480      }
481  
482      /**
# Line 628 | Line 490 | public class DelayQueueTest extends JSR1
490          try {
491              q.remove();
492              shouldThrow();
493 <        } catch (NoSuchElementException success) {
632 <        }
493 >        } catch (NoSuchElementException success) {}
494      }
495  
496      /**
# Line 728 | Line 589 | public class DelayQueueTest extends JSR1
589      /**
590       * toArray contains all elements
591       */
592 <    public void testToArray() {
592 >    public void testToArray() throws InterruptedException {
593          DelayQueue q = populatedQueue(SIZE);
594          Object[] o = q.toArray();
595          Arrays.sort(o);
735        try {
596          for (int i = 0; i < o.length; i++)
597 <            assertEquals(o[i], q.take());
738 <        } catch (InterruptedException e) {
739 <            unexpectedException();
740 <        }
597 >            assertSame(o[i], q.take());
598      }
599  
600      /**
601       * toArray(a) contains all elements
602       */
603      public void testToArray2() {
604 <        DelayQueue q = populatedQueue(SIZE);
604 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
605          PDelay[] ints = new PDelay[SIZE];
606 <        ints = (PDelay[])q.toArray(ints);
606 >        PDelay[] array = q.toArray(ints);
607 >        assertSame(ints, array);
608          Arrays.sort(ints);
609 <        try {
610 <            for (int i = 0; i < ints.length; i++)
753 <                assertEquals(ints[i], q.take());
754 <        } catch (InterruptedException e) {
755 <            unexpectedException();
756 <        }
757 <    }
758 <
759 <
760 <    /**
761 <     * toArray(null) throws NPE
762 <     */
763 <    public void testToArray_BadArg() {
764 <        try {
765 <            DelayQueue q = populatedQueue(SIZE);
766 <            Object o[] = q.toArray(null);
767 <            shouldThrow();
768 <        } catch (NullPointerException success) {}
609 >        for (int i = 0; i < ints.length; i++)
610 >            assertSame(ints[i], q.remove());
611      }
612  
613      /**
614 <     * toArray with incompatible array type throws CCE
614 >     * toArray(incompatible array type) throws ArrayStoreException
615       */
616      public void testToArray1_BadArg() {
617 +        DelayQueue q = populatedQueue(SIZE);
618          try {
619 <            DelayQueue q = populatedQueue(SIZE);
777 <            Object o[] = q.toArray(new String[10] );
619 >            q.toArray(new String[10]);
620              shouldThrow();
621 <        } catch (ArrayStoreException  success) {}
621 >        } catch (ArrayStoreException success) {}
622      }
623  
624      /**
# Line 796 | Line 638 | public class DelayQueueTest extends JSR1
638      /**
639       * iterator.remove removes current element
640       */
641 <    public void testIteratorRemove () {
641 >    public void testIteratorRemove() {
642          final DelayQueue q = new DelayQueue();
643          q.add(new PDelay(2));
644          q.add(new PDelay(1));
# Line 810 | Line 652 | public class DelayQueueTest extends JSR1
652          assertFalse(it.hasNext());
653      }
654  
813
655      /**
656       * toString contains toStrings of elements
657       */
# Line 818 | Line 659 | public class DelayQueueTest extends JSR1
659          DelayQueue q = populatedQueue(SIZE);
660          String s = q.toString();
661          for (int i = 0; i < SIZE; ++i) {
662 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
662 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
663          }
664      }
665  
666      /**
667 <     * offer transfers elements across Executor tasks
667 >     * timed poll transfers elements across Executor tasks
668       */
669      public void testPollInExecutor() {
670          final DelayQueue q = new DelayQueue();
671 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
672          ExecutorService executor = Executors.newFixedThreadPool(2);
673 <        executor.execute(new Runnable() {
674 <            public void run() {
675 <                threadAssertNull(q.poll());
676 <                try {
677 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
678 <                    threadAssertTrue(q.isEmpty());
679 <                }
680 <                catch (InterruptedException e) {
681 <                    threadUnexpectedException();
682 <                }
683 <            }
684 <        });
673 >        executor.execute(new CheckedRunnable() {
674 >            public void realRun() throws InterruptedException {
675 >                assertNull(q.poll());
676 >                threadsStarted.await();
677 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
678 >                checkEmpty(q);
679 >            }});
680 >
681 >        executor.execute(new CheckedRunnable() {
682 >            public void realRun() throws InterruptedException {
683 >                threadsStarted.await();
684 >                q.put(new PDelay(1));
685 >            }});
686  
844        executor.execute(new Runnable() {
845            public void run() {
846                try {
847                    Thread.sleep(SHORT_DELAY_MS);
848                    q.put(new PDelay(1));
849                }
850                catch (InterruptedException e) {
851                    threadUnexpectedException();
852                }
853            }
854        });
687          joinPool(executor);
856
688      }
689  
859
690      /**
691       * Delayed actions do not occur until their delay elapses
692       */
693 <    public void testDelay() {
694 <        DelayQueue q = new DelayQueue();
695 <        NanoDelay[] elements = new NanoDelay[SIZE];
696 <        for (int i = 0; i < SIZE; ++i) {
867 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
868 <        }
869 <        for (int i = 0; i < SIZE; ++i) {
870 <            q.add(elements[i]);
871 <        }
693 >    public void testDelay() throws InterruptedException {
694 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
695 >        for (int i = 0; i < SIZE; ++i)
696 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
697  
698 <        try {
699 <            long last = 0;
700 <            for (int i = 0; i < SIZE; ++i) {
701 <                NanoDelay e = (NanoDelay)(q.take());
702 <                long tt = e.getTriggerTime();
703 <                assertTrue(tt <= System.nanoTime());
704 <                if (i != 0)
705 <                    assertTrue(tt >= last);
881 <                last = tt;
882 <            }
883 <        }
884 <        catch (InterruptedException ie) {
885 <            unexpectedException();
698 >        long last = 0;
699 >        for (int i = 0; i < SIZE; ++i) {
700 >            NanoDelay e = q.take();
701 >            long tt = e.getTriggerTime();
702 >            assertTrue(System.nanoTime() - tt >= 0);
703 >            if (i != 0)
704 >                assertTrue(tt >= last);
705 >            last = tt;
706          }
707 +        assertTrue(q.isEmpty());
708      }
709  
710      /**
# Line 892 | Line 713 | public class DelayQueueTest extends JSR1
713      public void testPeekDelayed() {
714          DelayQueue q = new DelayQueue();
715          q.add(new NanoDelay(Long.MAX_VALUE));
716 <        assert(q.peek() != null);
716 >        assertNotNull(q.peek());
717      }
718  
898
719      /**
720       * poll of a non-empty queue returns null if no expired elements.
721       */
# Line 908 | Line 728 | public class DelayQueueTest extends JSR1
728      /**
729       * timed poll of a non-empty queue returns null if no expired elements.
730       */
731 <    public void testTimedPollDelayed() {
731 >    public void testTimedPollDelayed() throws InterruptedException {
732          DelayQueue q = new DelayQueue();
733          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
734 <        try {
915 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
916 <        } catch (Exception ex) {
917 <            unexpectedException();
918 <        }
919 <    }
920 <
921 <    /**
922 <     * drainTo(null) throws NPE
923 <     */
924 <    public void testDrainToNull() {
925 <        DelayQueue q = populatedQueue(SIZE);
926 <        try {
927 <            q.drainTo(null);
928 <            shouldThrow();
929 <        } catch (NullPointerException success) {
930 <        }
931 <    }
932 <
933 <    /**
934 <     * drainTo(this) throws IAE
935 <     */
936 <    public void testDrainToSelf() {
937 <        DelayQueue q = populatedQueue(SIZE);
938 <        try {
939 <            q.drainTo(q);
940 <            shouldThrow();
941 <        } catch (IllegalArgumentException success) {
942 <        }
734 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
735      }
736  
737      /**
# Line 973 | Line 765 | public class DelayQueueTest extends JSR1
765      /**
766       * drainTo empties queue
767       */
768 <    public void testDrainToWithActivePut() {
768 >    public void testDrainToWithActivePut() throws InterruptedException {
769          final DelayQueue q = populatedQueue(SIZE);
770 <        Thread t = new Thread(new Runnable() {
771 <                public void run() {
772 <                    q.put(new PDelay(SIZE+1));
773 <                }
982 <            });
983 <        try {
984 <            t.start();
985 <            ArrayList l = new ArrayList();
986 <            q.drainTo(l);
987 <            assertTrue(l.size() >= SIZE);
988 <            t.join();
989 <            assertTrue(q.size() + l.size() >= SIZE);
990 <        } catch (Exception e) {
991 <            unexpectedException();
992 <        }
993 <    }
770 >        Thread t = new Thread(new CheckedRunnable() {
771 >            public void realRun() {
772 >                q.put(new PDelay(SIZE+1));
773 >            }});
774  
775 <    /**
776 <     * drainTo(null, n) throws NPE
777 <     */
778 <    public void testDrainToNullN() {
779 <        DelayQueue q = populatedQueue(SIZE);
780 <        try {
1001 <            q.drainTo(null, 0);
1002 <            shouldThrow();
1003 <        } catch (NullPointerException success) {
1004 <        }
1005 <    }
1006 <
1007 <    /**
1008 <     * drainTo(this, n) throws IAE
1009 <     */
1010 <    public void testDrainToSelfN() {
1011 <        DelayQueue q = populatedQueue(SIZE);
1012 <        try {
1013 <            q.drainTo(q, 0);
1014 <            shouldThrow();
1015 <        } catch (IllegalArgumentException success) {
1016 <        }
775 >        t.start();
776 >        ArrayList l = new ArrayList();
777 >        q.drainTo(l);
778 >        assertTrue(l.size() >= SIZE);
779 >        t.join();
780 >        assertTrue(q.size() + l.size() >= SIZE);
781      }
782  
783      /**
784 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
784 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
785       */
786      public void testDrainToN() {
787          for (int i = 0; i < SIZE + 2; ++i) {
788              DelayQueue q = populatedQueue(SIZE);
789              ArrayList l = new ArrayList();
790              q.drainTo(l, i);
791 <            int k = (i < SIZE)? i : SIZE;
791 >            int k = (i < SIZE) ? i : SIZE;
792              assertEquals(q.size(), SIZE-k);
793              assertEquals(l.size(), k);
794          }
795      }
796  
1033
797   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines