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.54 by jsr166, Tue May 31 16:16:23 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.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.concurrent.BlockingQueue;
15 > import java.util.concurrent.CountDownLatch;
16 > import java.util.concurrent.Delayed;
17 > import java.util.concurrent.DelayQueue;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.TimeUnit;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
12 import java.util.concurrent.*;
22  
23   public class DelayQueueTest extends JSR166TestCase {
24 +
25 +    public static class Generic extends BlockingQueueTest {
26 +        protected BlockingQueue emptyCollection() {
27 +            return new DelayQueue();
28 +        }
29 +        protected PDelay makeElement(int i) {
30 +            return new PDelay(i);
31 +        }
32 +    }
33 +
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        junit.textui.TestRunner.run(suite());
36      }
37  
38      public static Test suite() {
39 <        return new TestSuite(DelayQueueTest.class);
39 >        return newTestSuite(DelayQueueTest.class,
40 >                            new Generic().testSuite());
41      }
42  
43      private static final int NOCAP = Integer.MAX_VALUE;
44  
45      /**
46       * A delayed implementation for testing.
47 <     * Most  tests use Pseudodelays, where delays are all elapsed
47 >     * Most tests use Pseudodelays, where delays are all elapsed
48       * (so, no blocking solely for delays) but are still ordered
49       */
50      static class PDelay implements Delayed {
# Line 32 | Line 52 | public class DelayQueueTest extends JSR1
52          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
53          public int compareTo(PDelay y) {
54              int i = pseudodelay;
55 <            int j = ((PDelay)y).pseudodelay;
55 >            int j = y.pseudodelay;
56              if (i < j) return -1;
57              if (i > j) return 1;
58              return 0;
59          }
60  
61          public int compareTo(Delayed y) {
62 <            int i = pseudodelay;
43 <            int j = ((PDelay)y).pseudodelay;
44 <            if (i < j) return -1;
45 <            if (i > j) return 1;
46 <            return 0;
62 >            return compareTo((PDelay)y);
63          }
64  
65          public boolean equals(Object other) {
66 <            return ((PDelay)other).pseudodelay == pseudodelay;
66 >            return equals((PDelay)other);
67          }
68          public boolean equals(PDelay other) {
69 <            return ((PDelay)other).pseudodelay == pseudodelay;
69 >            return other.pseudodelay == pseudodelay;
70          }
71  
56
72          public long getDelay(TimeUnit ignore) {
73              return pseudodelay;
74          }
# Line 66 | Line 81 | public class DelayQueueTest extends JSR1
81          }
82      }
83  
69
84      /**
85       * Delayed implementation that actually delays
86       */
# Line 77 | Line 91 | public class DelayQueueTest extends JSR1
91          }
92          public int compareTo(NanoDelay y) {
93              long i = trigger;
94 <            long j = ((NanoDelay)y).trigger;
94 >            long j = y.trigger;
95              if (i < j) return -1;
96              if (i > j) return 1;
97              return 0;
98          }
99  
100          public int compareTo(Delayed y) {
101 <            long i = trigger;
88 <            long j = ((NanoDelay)y).trigger;
89 <            if (i < j) return -1;
90 <            if (i > j) return 1;
91 <            return 0;
101 >            return compareTo((NanoDelay)y);
102          }
103  
104          public boolean equals(Object other) {
105 <            return ((NanoDelay)other).trigger == trigger;
105 >            return equals((NanoDelay)other);
106          }
107          public boolean equals(NanoDelay other) {
108 <            return ((NanoDelay)other).trigger == trigger;
108 >            return other.trigger == trigger;
109          }
110  
111          public long getDelay(TimeUnit unit) {
# Line 112 | Line 122 | public class DelayQueueTest extends JSR1
122          }
123      }
124  
115
125      /**
126       * Create a queue of given size containing consecutive
127       * PDelays 0 ... n.
128       */
129 <    private DelayQueue populatedQueue(int n) {
130 <        DelayQueue q = new DelayQueue();
129 >    private DelayQueue<PDelay> populatedQueue(int n) {
130 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
131          assertTrue(q.isEmpty());
132          for (int i = n-1; i >= 0; i-=2)
133              assertTrue(q.offer(new PDelay(i)));
# Line 144 | Line 153 | public class DelayQueueTest extends JSR1
153          try {
154              DelayQueue q = new DelayQueue(null);
155              shouldThrow();
156 <        }
148 <        catch (NullPointerException success) {}
156 >        } catch (NullPointerException success) {}
157      }
158  
159      /**
# Line 156 | Line 164 | public class DelayQueueTest extends JSR1
164              PDelay[] ints = new PDelay[SIZE];
165              DelayQueue q = new DelayQueue(Arrays.asList(ints));
166              shouldThrow();
167 <        }
160 <        catch (NullPointerException success) {}
167 >        } catch (NullPointerException success) {}
168      }
169  
170      /**
# Line 170 | Line 177 | public class DelayQueueTest extends JSR1
177                  ints[i] = new PDelay(i);
178              DelayQueue q = new DelayQueue(Arrays.asList(ints));
179              shouldThrow();
180 <        }
174 <        catch (NullPointerException success) {}
180 >        } catch (NullPointerException success) {}
181      }
182  
183      /**
184       * Queue contains all elements of collection used to initialize
185       */
186      public void testConstructor6() {
187 <        try {
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)
187 <                assertEquals(ints[i], q.poll());
188 <        }
189 <        finally {}
187 >        PDelay[] ints = new PDelay[SIZE];
188 >        for (int i = 0; i < SIZE; ++i)
189 >            ints[i] = new PDelay(i);
190 >        DelayQueue q = new DelayQueue(Arrays.asList(ints));
191 >        for (int i = 0; i < SIZE; ++i)
192 >            assertEquals(ints[i], q.poll());
193      }
194  
195      /**
# Line 205 | Line 208 | public class DelayQueueTest extends JSR1
208      }
209  
210      /**
211 <     * remainingCapacity does not change when elementa added or removed,
211 >     * remainingCapacity does not change when elements added or removed,
212       * but size does
213       */
214      public void testRemainingCapacity() {
# Line 223 | Line 226 | public class DelayQueueTest extends JSR1
226      }
227  
228      /**
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    /**
229       * offer non-null succeeds
230       */
231      public void testOffer() {
# Line 265 | Line 246 | public class DelayQueueTest extends JSR1
246      }
247  
248      /**
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    /**
249       * addAll(this) throws IAE
250       */
251      public void testAddAllSelf() {
# Line 285 | Line 253 | public class DelayQueueTest extends JSR1
253              DelayQueue q = populatedQueue(SIZE);
254              q.addAll(q);
255              shouldThrow();
256 <        }
289 <        catch (IllegalArgumentException success) {}
256 >        } catch (IllegalArgumentException success) {}
257      }
258  
259      /**
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    /**
260       * addAll of a collection with any null elements throws NPE after
261       * possibly adding some elements
262       */
# Line 313 | Line 268 | public class DelayQueueTest extends JSR1
268                  ints[i] = new PDelay(i);
269              q.addAll(Arrays.asList(ints));
270              shouldThrow();
271 <        }
317 <        catch (NullPointerException success) {}
271 >        } catch (NullPointerException success) {}
272      }
273  
274      /**
275       * Queue contains all elements of successful addAll
276       */
277      public void testAddAll5() {
278 <        try {
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)
333 <                assertEquals(ints[i], q.poll());
334 <        }
335 <        finally {}
278 >        PDelay[] empty = new PDelay[0];
279 >        PDelay[] ints = new PDelay[SIZE];
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)));
284 >        assertTrue(q.addAll(Arrays.asList(ints)));
285 >        for (int i = 0; i < SIZE; ++i)
286 >            assertEquals(ints[i], q.poll());
287      }
288  
289      /**
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    /**
290       * all elements successfully put are contained
291       */
292 <     public void testPut() {
293 <         try {
294 <             DelayQueue q = new DelayQueue();
295 <             for (int i = 0; i < SIZE; ++i) {
296 <                 PDelay I = new PDelay(i);
297 <                 q.put(I);
360 <                 assertTrue(q.contains(I));
361 <             }
362 <             assertEquals(SIZE, q.size());
363 <         }
364 <         finally {
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());
300      }
301  
302      /**
303       * put doesn't block waiting for take
304       */
305 <    public void testPutWithTake() {
305 >    public void testPutWithTake() throws InterruptedException {
306          final DelayQueue q = new DelayQueue();
307 <        Thread t = new Thread(new Runnable() {
308 <                public void run() {
309 <                    int added = 0;
310 <                    try {
311 <                        q.put(new PDelay(0));
312 <                        ++added;
313 <                        q.put(new PDelay(0));
314 <                        ++added;
315 <                        q.put(new PDelay(0));
316 <                        ++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 <        }
307 >        Thread t = newStartedThread(new CheckedRunnable() {
308 >            public void realRun() {
309 >                q.put(new PDelay(0));
310 >                q.put(new PDelay(0));
311 >                q.put(new PDelay(0));
312 >                q.put(new PDelay(0));
313 >            }});
314 >
315 >        awaitTermination(t);
316 >        assertEquals(4, q.size());
317      }
318  
319      /**
320       * timed offer does not time out
321       */
322 <    public void testTimedOffer() {
322 >    public void testTimedOffer() throws InterruptedException {
323          final DelayQueue q = new DelayQueue();
324 <        Thread t = new Thread(new Runnable() {
325 <                public void run() {
326 <                    try {
327 <                        q.put(new PDelay(0));
328 <                        q.put(new PDelay(0));
329 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
330 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
413 <                    } finally { }
414 <                }
415 <            });
324 >        Thread t = newStartedThread(new CheckedRunnable() {
325 >            public void realRun() throws InterruptedException {
326 >                q.put(new PDelay(0));
327 >                q.put(new PDelay(0));
328 >                assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
329 >                assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
330 >            }});
331  
332 <        try {
418 <            t.start();
419 <            Thread.sleep(SMALL_DELAY_MS);
420 <            t.interrupt();
421 <            t.join();
422 <        } catch (Exception e) {
423 <            unexpectedException();
424 <        }
332 >        awaitTermination(t);
333      }
334  
335      /**
336       * take retrieves elements in priority order
337       */
338 <    public void testTake() {
339 <        try {
340 <            DelayQueue q = populatedQueue(SIZE);
341 <            for (int i = 0; i < SIZE; ++i) {
434 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
435 <            }
436 <        } catch (InterruptedException e) {
437 <            unexpectedException();
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()));
342          }
343      }
344  
345      /**
346 <     * take blocks interruptibly when empty
346 >     * Take removes existing elements until empty, then blocks interruptibly
347       */
348 <    public void testTakeFromEmpty() {
349 <        final DelayQueue q = new DelayQueue();
350 <        Thread t = new Thread(new Runnable() {
351 <                public void run() {
352 <                    try {
353 <                        q.take();
354 <                        threadShouldThrow();
451 <                    } catch (InterruptedException success) { }
348 >    public void testBlockingTake() throws InterruptedException {
349 >        final DelayQueue q = populatedQueue(SIZE);
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                  }
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    }
356  
357 <    /**
358 <     * Take removes existing elements until empty, then blocks interruptibly
359 <     */
360 <    public void testBlockingTake() {
361 <        Thread t = new Thread(new Runnable() {
362 <                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 <    }
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 +        await(pleaseInterrupt);
373 +        assertThreadStaysAlive(t);
374 +        t.interrupt();
375 +        awaitTermination(t);
376 +    }
377  
378      /**
379       * poll succeeds unless empty
# Line 501 | Line 387 | public class DelayQueueTest extends JSR1
387      }
388  
389      /**
390 <     * timed pool with zero timeout succeeds when non-empty, else times out
390 >     * timed poll with zero timeout succeeds when non-empty, else times out
391       */
392 <    public void testTimedPoll0() {
393 <        try {
394 <            DelayQueue q = populatedQueue(SIZE);
395 <            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();
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)));
396          }
397 +        assertNull(q.poll(0, MILLISECONDS));
398      }
399  
400      /**
401 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
401 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
402       */
403 <    public void testTimedPoll() {
404 <        try {
405 <            DelayQueue q = populatedQueue(SIZE);
406 <            for (int i = 0; i < SIZE; ++i) {
407 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
408 <            }
527 <            assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
528 <        } catch (InterruptedException e) {
529 <            unexpectedException();
403 >    public void testTimedPoll() throws InterruptedException {
404 >        DelayQueue q = populatedQueue(SIZE);
405 >        for (int i = 0; i < SIZE; ++i) {
406 >            long startTime = System.nanoTime();
407 >            assertEquals(new PDelay(i), ((PDelay)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      /**
417       * Interrupted timed poll throws InterruptedException instead of
418       * returning timeout status
419       */
420 <    public void testInterruptedTimedPoll() {
421 <        Thread t = new Thread(new Runnable() {
422 <                public void run() {
423 <                    try {
424 <                        DelayQueue q = populatedQueue(SIZE);
425 <                        for (int i = 0; i < SIZE; ++i) {
426 <                            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) { }
420 >    public void testInterruptedTimedPoll() throws InterruptedException {
421 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
422 >        Thread t = newStartedThread(new CheckedRunnable() {
423 >            public void realRun() throws InterruptedException {
424 >                DelayQueue q = populatedQueue(SIZE);
425 >                for (int i = 0; i < SIZE; ++i) {
426 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
427                  }
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    }
428  
429 +                Thread.currentThread().interrupt();
430 +                try {
431 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
432 +                    shouldThrow();
433 +                } catch (InterruptedException success) {}
434 +                assertFalse(Thread.interrupted());
435 +
436 +                pleaseInterrupt.countDown();
437 +                try {
438 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
439 +                    shouldThrow();
440 +                } catch (InterruptedException success) {}
441 +                assertFalse(Thread.interrupted());
442 +            }});
443 +
444 +        await(pleaseInterrupt);
445 +        assertThreadStaysAlive(t);
446 +        t.interrupt();
447 +        awaitTermination(t);
448 +    }
449  
450      /**
451       * peek returns next element, or null if empty
# Line 592 | Line 454 | public class DelayQueueTest extends JSR1
454          DelayQueue q = populatedQueue(SIZE);
455          for (int i = 0; i < SIZE; ++i) {
456              assertEquals(new PDelay(i), ((PDelay)q.peek()));
457 <            q.poll();
457 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
458              if (q.isEmpty())
459                  assertNull(q.peek());
460              else
461 <                assertTrue(i != ((PDelay)q.peek()).intValue());
461 >                assertFalse(new PDelay(i).equals(q.peek()));
462          }
463          assertNull(q.peek());
464      }
# Line 613 | Line 475 | public class DelayQueueTest extends JSR1
475          try {
476              q.element();
477              shouldThrow();
478 <        }
617 <        catch (NoSuchElementException success) {}
478 >        } catch (NoSuchElementException success) {}
479      }
480  
481      /**
# Line 628 | Line 489 | public class DelayQueueTest extends JSR1
489          try {
490              q.remove();
491              shouldThrow();
492 <        } catch (NoSuchElementException success) {
632 <        }
492 >        } catch (NoSuchElementException success) {}
493      }
494  
495      /**
# Line 728 | Line 588 | public class DelayQueueTest extends JSR1
588      /**
589       * toArray contains all elements
590       */
591 <    public void testToArray() {
591 >    public void testToArray() throws InterruptedException {
592          DelayQueue q = populatedQueue(SIZE);
593          Object[] o = q.toArray();
594          Arrays.sort(o);
735        try {
595          for (int i = 0; i < o.length; i++)
596 <            assertEquals(o[i], q.take());
738 <        } catch (InterruptedException e) {
739 <            unexpectedException();
740 <        }
596 >            assertSame(o[i], q.take());
597      }
598  
599      /**
600       * toArray(a) contains all elements
601       */
602      public void testToArray2() {
603 <        DelayQueue q = populatedQueue(SIZE);
603 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
604          PDelay[] ints = new PDelay[SIZE];
605 <        ints = (PDelay[])q.toArray(ints);
605 >        PDelay[] array = q.toArray(ints);
606 >        assertSame(ints, array);
607          Arrays.sort(ints);
608 <        try {
609 <            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) {}
608 >        for (int i = 0; i < ints.length; i++)
609 >            assertSame(ints[i], q.remove());
610      }
611  
612      /**
613 <     * toArray with incompatible array type throws CCE
613 >     * toArray(incompatible array type) throws ArrayStoreException
614       */
615      public void testToArray1_BadArg() {
616 +        DelayQueue q = populatedQueue(SIZE);
617          try {
618 <            DelayQueue q = populatedQueue(SIZE);
777 <            Object o[] = q.toArray(new String[10] );
618 >            q.toArray(new String[10]);
619              shouldThrow();
620 <        } catch (ArrayStoreException  success) {}
620 >        } catch (ArrayStoreException success) {}
621      }
622  
623      /**
# Line 796 | Line 637 | public class DelayQueueTest extends JSR1
637      /**
638       * iterator.remove removes current element
639       */
640 <    public void testIteratorRemove () {
640 >    public void testIteratorRemove() {
641          final DelayQueue q = new DelayQueue();
642          q.add(new PDelay(2));
643          q.add(new PDelay(1));
# Line 810 | Line 651 | public class DelayQueueTest extends JSR1
651          assertFalse(it.hasNext());
652      }
653  
813
654      /**
655       * toString contains toStrings of elements
656       */
# Line 818 | Line 658 | public class DelayQueueTest extends JSR1
658          DelayQueue q = populatedQueue(SIZE);
659          String s = q.toString();
660          for (int i = 0; i < SIZE; ++i) {
661 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
661 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
662          }
663      }
664  
665      /**
666 <     * offer transfers elements across Executor tasks
666 >     * timed poll transfers elements across Executor tasks
667       */
668      public void testPollInExecutor() {
669          final DelayQueue q = new DelayQueue();
670 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
671          ExecutorService executor = Executors.newFixedThreadPool(2);
672 <        executor.execute(new Runnable() {
673 <            public void run() {
674 <                threadAssertNull(q.poll());
675 <                try {
676 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
677 <                    threadAssertTrue(q.isEmpty());
678 <                }
679 <                catch (InterruptedException e) {
680 <                    threadUnexpectedException();
681 <                }
682 <            }
683 <        });
672 >        executor.execute(new CheckedRunnable() {
673 >            public void realRun() throws InterruptedException {
674 >                assertNull(q.poll());
675 >                threadsStarted.await();
676 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
677 >                checkEmpty(q);
678 >            }});
679 >
680 >        executor.execute(new CheckedRunnable() {
681 >            public void realRun() throws InterruptedException {
682 >                threadsStarted.await();
683 >                q.put(new PDelay(1));
684 >            }});
685  
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        });
686          joinPool(executor);
856
687      }
688  
859
689      /**
690       * Delayed actions do not occur until their delay elapses
691       */
692 <    public void testDelay() {
693 <        DelayQueue q = new DelayQueue();
694 <        NanoDelay[] elements = new NanoDelay[SIZE];
695 <        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 <        }
692 >    public void testDelay() throws InterruptedException {
693 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
694 >        for (int i = 0; i < SIZE; ++i)
695 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
696  
697 <        try {
698 <            long last = 0;
699 <            for (int i = 0; i < SIZE; ++i) {
700 <                NanoDelay e = (NanoDelay)(q.take());
701 <                long tt = e.getTriggerTime();
702 <                assertTrue(tt <= System.nanoTime());
703 <                if (i != 0)
704 <                    assertTrue(tt >= last);
881 <                last = tt;
882 <            }
883 <        }
884 <        catch (InterruptedException ie) {
885 <            unexpectedException();
697 >        long last = 0;
698 >        for (int i = 0; i < SIZE; ++i) {
699 >            NanoDelay e = q.take();
700 >            long tt = e.getTriggerTime();
701 >            assertTrue(System.nanoTime() - tt >= 0);
702 >            if (i != 0)
703 >                assertTrue(tt >= last);
704 >            last = tt;
705          }
706 +        assertTrue(q.isEmpty());
707      }
708  
709      /**
# Line 892 | Line 712 | public class DelayQueueTest extends JSR1
712      public void testPeekDelayed() {
713          DelayQueue q = new DelayQueue();
714          q.add(new NanoDelay(Long.MAX_VALUE));
715 <        assert(q.peek() != null);
715 >        assertNotNull(q.peek());
716      }
717  
898
718      /**
719       * poll of a non-empty queue returns null if no expired elements.
720       */
# Line 908 | Line 727 | public class DelayQueueTest extends JSR1
727      /**
728       * timed poll of a non-empty queue returns null if no expired elements.
729       */
730 <    public void testTimedPollDelayed() {
730 >    public void testTimedPollDelayed() throws InterruptedException {
731          DelayQueue q = new DelayQueue();
732          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
733 <        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 <        }
733 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
734      }
735  
736      /**
# Line 973 | Line 764 | public class DelayQueueTest extends JSR1
764      /**
765       * drainTo empties queue
766       */
767 <    public void testDrainToWithActivePut() {
767 >    public void testDrainToWithActivePut() throws InterruptedException {
768          final DelayQueue q = populatedQueue(SIZE);
769 <        Thread t = new Thread(new Runnable() {
770 <                public void run() {
771 <                    q.put(new PDelay(SIZE+1));
772 <                }
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 <    }
769 >        Thread t = new Thread(new CheckedRunnable() {
770 >            public void realRun() {
771 >                q.put(new PDelay(SIZE+1));
772 >            }});
773  
774 <    /**
775 <     * drainTo(null, n) throws NPE
776 <     */
777 <    public void testDrainToNullN() {
778 <        DelayQueue q = populatedQueue(SIZE);
779 <        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 <        }
774 >        t.start();
775 >        ArrayList l = new ArrayList();
776 >        q.drainTo(l);
777 >        assertTrue(l.size() >= SIZE);
778 >        t.join();
779 >        assertTrue(q.size() + l.size() >= SIZE);
780      }
781  
782      /**
783 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
783 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
784       */
785      public void testDrainToN() {
786          for (int i = 0; i < SIZE + 2; ++i) {
787              DelayQueue q = populatedQueue(SIZE);
788              ArrayList l = new ArrayList();
789              q.drainTo(l, i);
790 <            int k = (i < SIZE)? i : SIZE;
790 >            int k = (i < SIZE) ? i : SIZE;
791              assertEquals(q.size(), SIZE-k);
792              assertEquals(l.size(), k);
793          }
794      }
795  
1033
796   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines