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.16 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.55 by jsr166, Mon Jun 27 20:37:32 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.*;
11 < import java.util.concurrent.*;
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;
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 {
51          int pseudodelay;
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;
56 <            if (i < j) return -1;
36 <            if (i > j) return 1;
37 <            return 0;
52 >        PDelay(int i) { pseudodelay = i; }
53 >        public int compareTo(PDelay other) {
54 >            int a = this.pseudodelay;
55 >            int b = other.pseudodelay;
56 >            return (a < b) ? -1 : (a > b) ? 1 : 0;
57          }
39
58          public int compareTo(Delayed y) {
59 <            int i = pseudodelay;
42 <            int j = ((PDelay)y).pseudodelay;
43 <            if (i < j) return -1;
44 <            if (i > j) return 1;
45 <            return 0;
59 >            return compareTo((PDelay)y);
60          }
47
61          public boolean equals(Object other) {
62 <            return ((PDelay)other).pseudodelay == pseudodelay;
63 <        }
51 <        public boolean equals(PDelay other) {
52 <            return ((PDelay)other).pseudodelay == pseudodelay;
62 >            return (other instanceof PDelay) &&
63 >                this.pseudodelay == ((PDelay)other).pseudodelay;
64          }
54
55
65          public long getDelay(TimeUnit ignore) {
66 <            return pseudodelay;
66 >            return Integer.MIN_VALUE + pseudodelay;
67          }
59        public int intValue() {
60            return pseudodelay;
61        }
62
68          public String toString() {
69              return String.valueOf(pseudodelay);
70          }
71      }
72  
68
73      /**
74       * Delayed implementation that actually delays
75       */
# Line 76 | Line 80 | public class DelayQueueTest extends JSR1
80          }
81          public int compareTo(NanoDelay y) {
82              long i = trigger;
83 <            long j = ((NanoDelay)y).trigger;
83 >            long j = y.trigger;
84              if (i < j) return -1;
85              if (i > j) return 1;
86              return 0;
87          }
88  
89          public int compareTo(Delayed y) {
90 <            long i = trigger;
87 <            long j = ((NanoDelay)y).trigger;
88 <            if (i < j) return -1;
89 <            if (i > j) return 1;
90 <            return 0;
90 >            return compareTo((NanoDelay)y);
91          }
92  
93          public boolean equals(Object other) {
94 <            return ((NanoDelay)other).trigger == trigger;
94 >            return equals((NanoDelay)other);
95          }
96          public boolean equals(NanoDelay other) {
97 <            return ((NanoDelay)other).trigger == trigger;
97 >            return other.trigger == trigger;
98          }
99  
100          public long getDelay(TimeUnit unit) {
# Line 111 | Line 111 | public class DelayQueueTest extends JSR1
111          }
112      }
113  
114
114      /**
115       * Create a queue of given size containing consecutive
116       * PDelays 0 ... n.
117       */
118 <    private DelayQueue populatedQueue(int n) {
119 <        DelayQueue q = new DelayQueue();
118 >    private DelayQueue<PDelay> populatedQueue(int n) {
119 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
120          assertTrue(q.isEmpty());
121 <        for (int i = n-1; i >= 0; i-=2)
122 <            assertTrue(q.offer(new PDelay(i)));
123 <        for (int i = (n & 1); i < n; i+=2)
124 <            assertTrue(q.offer(new PDelay(i)));
121 >        for (int i = n-1; i >= 0; i-=2)
122 >            assertTrue(q.offer(new PDelay(i)));
123 >        for (int i = (n & 1); i < n; i+=2)
124 >            assertTrue(q.offer(new PDelay(i)));
125          assertFalse(q.isEmpty());
126          assertEquals(NOCAP, q.remainingCapacity());
127 <        assertEquals(n, q.size());
127 >        assertEquals(n, q.size());
128          return q;
129      }
130  
# Line 143 | Line 142 | public class DelayQueueTest extends JSR1
142          try {
143              DelayQueue q = new DelayQueue(null);
144              shouldThrow();
145 <        }
147 <        catch (NullPointerException success) {}
145 >        } catch (NullPointerException success) {}
146      }
147  
148      /**
# Line 155 | Line 153 | public class DelayQueueTest extends JSR1
153              PDelay[] ints = new PDelay[SIZE];
154              DelayQueue q = new DelayQueue(Arrays.asList(ints));
155              shouldThrow();
156 <        }
159 <        catch (NullPointerException success) {}
156 >        } catch (NullPointerException success) {}
157      }
158  
159      /**
# Line 169 | Line 166 | public class DelayQueueTest extends JSR1
166                  ints[i] = new PDelay(i);
167              DelayQueue q = new DelayQueue(Arrays.asList(ints));
168              shouldThrow();
169 <        }
173 <        catch (NullPointerException success) {}
169 >        } catch (NullPointerException success) {}
170      }
171  
172      /**
173       * Queue contains all elements of collection used to initialize
174       */
175      public void testConstructor6() {
176 <        try {
177 <            PDelay[] ints = new PDelay[SIZE];
178 <            for (int i = 0; i < SIZE; ++i)
179 <                ints[i] = new PDelay(i);
180 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
181 <            for (int i = 0; i < SIZE; ++i)
186 <                assertEquals(ints[i], q.poll());
187 <        }
188 <        finally {}
176 >        PDelay[] ints = new PDelay[SIZE];
177 >        for (int i = 0; i < SIZE; ++i)
178 >            ints[i] = new PDelay(i);
179 >        DelayQueue q = new DelayQueue(Arrays.asList(ints));
180 >        for (int i = 0; i < SIZE; ++i)
181 >            assertEquals(ints[i], q.poll());
182      }
183  
184      /**
# Line 204 | Line 197 | public class DelayQueueTest extends JSR1
197      }
198  
199      /**
200 <     * remainingCapacity does not change when elementa added or removed,
200 >     * remainingCapacity does not change when elements added or removed,
201       * but size does
202       */
203      public void testRemainingCapacity() {
# Line 222 | Line 215 | public class DelayQueueTest extends JSR1
215      }
216  
217      /**
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    /**
218       * offer non-null succeeds
219       */
220      public void testOffer() {
# Line 264 | Line 235 | public class DelayQueueTest extends JSR1
235      }
236  
237      /**
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    /**
238       * addAll(this) throws IAE
239       */
240      public void testAddAllSelf() {
# Line 284 | Line 242 | public class DelayQueueTest extends JSR1
242              DelayQueue q = populatedQueue(SIZE);
243              q.addAll(q);
244              shouldThrow();
245 <        }
288 <        catch (IllegalArgumentException success) {}
245 >        } catch (IllegalArgumentException success) {}
246      }
247  
248      /**
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    /**
249       * addAll of a collection with any null elements throws NPE after
250       * possibly adding some elements
251       */
# Line 312 | Line 257 | public class DelayQueueTest extends JSR1
257                  ints[i] = new PDelay(i);
258              q.addAll(Arrays.asList(ints));
259              shouldThrow();
260 <        }
316 <        catch (NullPointerException success) {}
260 >        } catch (NullPointerException success) {}
261      }
262  
263      /**
264       * Queue contains all elements of successful addAll
265       */
266      public void testAddAll5() {
267 <        try {
268 <            PDelay[] empty = new PDelay[0];
269 <            PDelay[] ints = new PDelay[SIZE];
270 <            for (int i = SIZE-1; i >= 0; --i)
271 <                ints[i] = new PDelay(i);
272 <            DelayQueue q = new DelayQueue();
273 <            assertFalse(q.addAll(Arrays.asList(empty)));
274 <            assertTrue(q.addAll(Arrays.asList(ints)));
275 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
267 >        PDelay[] empty = new PDelay[0];
268 >        PDelay[] ints = new PDelay[SIZE];
269 >        for (int i = SIZE-1; i >= 0; --i)
270 >            ints[i] = new PDelay(i);
271 >        DelayQueue q = new DelayQueue();
272 >        assertFalse(q.addAll(Arrays.asList(empty)));
273 >        assertTrue(q.addAll(Arrays.asList(ints)));
274 >        for (int i = 0; i < SIZE; ++i)
275 >            assertEquals(ints[i], q.poll());
276      }
277  
278      /**
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    /**
279       * all elements successfully put are contained
280       */
281 <     public void testPut() {
282 <         try {
283 <             DelayQueue q = new DelayQueue();
284 <             for (int i = 0; i < SIZE; ++i) {
285 <                 PDelay I = new PDelay(i);
286 <                 q.put(I);
359 <                 assertTrue(q.contains(I));
360 <             }
361 <             assertEquals(SIZE, q.size());
362 <         }
363 <         finally {
281 >    public void testPut() {
282 >        DelayQueue q = new DelayQueue();
283 >        for (int i = 0; i < SIZE; ++i) {
284 >            PDelay I = new PDelay(i);
285 >            q.put(I);
286 >            assertTrue(q.contains(I));
287          }
288 +        assertEquals(SIZE, q.size());
289      }
290  
291      /**
292       * put doesn't block waiting for take
293       */
294 <    public void testPutWithTake() {
294 >    public void testPutWithTake() throws InterruptedException {
295          final DelayQueue q = new DelayQueue();
296 <        Thread t = new Thread(new Runnable() {
297 <                public void run() {
298 <                    int added = 0;
299 <                    try {
300 <                        q.put(new PDelay(0));
301 <                        ++added;
302 <                        q.put(new PDelay(0));
303 <                        ++added;
304 <                        q.put(new PDelay(0));
305 <                        ++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 <        }
296 >        Thread t = newStartedThread(new CheckedRunnable() {
297 >            public void realRun() {
298 >                q.put(new PDelay(0));
299 >                q.put(new PDelay(0));
300 >                q.put(new PDelay(0));
301 >                q.put(new PDelay(0));
302 >            }});
303 >
304 >        awaitTermination(t);
305 >        assertEquals(4, q.size());
306      }
307  
308      /**
309       * timed offer does not time out
310       */
311 <    public void testTimedOffer() {
311 >    public void testTimedOffer() throws InterruptedException {
312          final DelayQueue q = new DelayQueue();
313 <        Thread t = new Thread(new Runnable() {
314 <                public void run() {
315 <                    try {
316 <                        q.put(new PDelay(0));
317 <                        q.put(new PDelay(0));
318 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
319 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
412 <                    } finally { }
413 <                }
414 <            });
313 >        Thread t = newStartedThread(new CheckedRunnable() {
314 >            public void realRun() throws InterruptedException {
315 >                q.put(new PDelay(0));
316 >                q.put(new PDelay(0));
317 >                assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
318 >                assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
319 >            }});
320  
321 <        try {
417 <            t.start();
418 <            Thread.sleep(SMALL_DELAY_MS);
419 <            t.interrupt();
420 <            t.join();
421 <        } catch (Exception e){
422 <            unexpectedException();
423 <        }
321 >        awaitTermination(t);
322      }
323  
324      /**
325       * take retrieves elements in priority order
326       */
327 <    public void testTake() {
328 <        try {
329 <            DelayQueue q = populatedQueue(SIZE);
330 <            for (int i = 0; i < SIZE; ++i) {
433 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
434 <            }
435 <        } catch (InterruptedException e){
436 <            unexpectedException();
437 <        }
438 <    }
439 <
440 <    /**
441 <     * take blocks interruptibly when empty
442 <     */
443 <    public void testTakeFromEmpty() {
444 <        final DelayQueue q = new DelayQueue();
445 <        Thread t = new Thread(new Runnable() {
446 <                public void run() {
447 <                    try {
448 <                        q.take();
449 <                        threadShouldThrow();
450 <                    } catch (InterruptedException success){ }
451 <                }
452 <            });
453 <        try {
454 <            t.start();
455 <            Thread.sleep(SHORT_DELAY_MS);
456 <            t.interrupt();
457 <            t.join();
458 <        } catch (Exception e){
459 <            unexpectedException();
327 >    public void testTake() throws InterruptedException {
328 >        DelayQueue q = populatedQueue(SIZE);
329 >        for (int i = 0; i < SIZE; ++i) {
330 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
331          }
332      }
333  
334      /**
335       * Take removes existing elements until empty, then blocks interruptibly
336       */
337 <    public void testBlockingTake() {
338 <        Thread t = new Thread(new Runnable() {
339 <                public void run() {
340 <                    try {
341 <                        DelayQueue q = populatedQueue(SIZE);
342 <                        for (int i = 0; i < SIZE; ++i) {
343 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
344 <                        }
345 <                        q.take();
346 <                        threadShouldThrow();
347 <                    } catch (InterruptedException success){
348 <                    }
349 <                }});
350 <        t.start();
351 <        try {
481 <           Thread.sleep(SHORT_DELAY_MS);
482 <           t.interrupt();
483 <           t.join();
484 <        }
485 <        catch (InterruptedException ie) {
486 <            unexpectedException();
487 <        }
488 <    }
337 >    public void testBlockingTake() throws InterruptedException {
338 >        final DelayQueue q = populatedQueue(SIZE);
339 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
340 >        Thread t = newStartedThread(new CheckedRunnable() {
341 >            public void realRun() throws InterruptedException {
342 >                for (int i = 0; i < SIZE; ++i) {
343 >                    assertEquals(new PDelay(i), ((PDelay)q.take()));
344 >                }
345 >
346 >                Thread.currentThread().interrupt();
347 >                try {
348 >                    q.take();
349 >                    shouldThrow();
350 >                } catch (InterruptedException success) {}
351 >                assertFalse(Thread.interrupted());
352  
353 +                pleaseInterrupt.countDown();
354 +                try {
355 +                    q.take();
356 +                    shouldThrow();
357 +                } catch (InterruptedException success) {}
358 +                assertFalse(Thread.interrupted());
359 +            }});
360 +
361 +        await(pleaseInterrupt);
362 +        assertThreadStaysAlive(t);
363 +        t.interrupt();
364 +        awaitTermination(t);
365 +    }
366  
367      /**
368       * poll succeeds unless empty
# Line 496 | Line 372 | public class DelayQueueTest extends JSR1
372          for (int i = 0; i < SIZE; ++i) {
373              assertEquals(new PDelay(i), ((PDelay)q.poll()));
374          }
375 <        assertNull(q.poll());
375 >        assertNull(q.poll());
376      }
377  
378      /**
379 <     * timed pool with zero timeout succeeds when non-empty, else times out
379 >     * timed poll with zero timeout succeeds when non-empty, else times out
380       */
381 <    public void testTimedPoll0() {
382 <        try {
383 <            DelayQueue q = populatedQueue(SIZE);
384 <            for (int i = 0; i < SIZE; ++i) {
385 <                assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
386 <            }
511 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
512 <        } catch (InterruptedException e){
513 <            unexpectedException();
514 <        }
381 >    public void testTimedPoll0() throws InterruptedException {
382 >        DelayQueue q = populatedQueue(SIZE);
383 >        for (int i = 0; i < SIZE; ++i) {
384 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
385 >        }
386 >        assertNull(q.poll(0, MILLISECONDS));
387      }
388  
389      /**
390 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
390 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
391       */
392 <    public void testTimedPoll() {
393 <        try {
394 <            DelayQueue q = populatedQueue(SIZE);
395 <            for (int i = 0; i < SIZE; ++i) {
396 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
397 <            }
398 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
399 <        } catch (InterruptedException e){
400 <            unexpectedException();
401 <        }
392 >    public void testTimedPoll() throws InterruptedException {
393 >        DelayQueue q = populatedQueue(SIZE);
394 >        for (int i = 0; i < SIZE; ++i) {
395 >            long startTime = System.nanoTime();
396 >            assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
397 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
398 >        }
399 >        long startTime = System.nanoTime();
400 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
401 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
402 >        checkEmpty(q);
403      }
404  
405      /**
406       * Interrupted timed poll throws InterruptedException instead of
407       * returning timeout status
408       */
409 <    public void testInterruptedTimedPoll() {
410 <        Thread t = new Thread(new Runnable() {
411 <                public void run() {
412 <                    try {
413 <                        DelayQueue q = populatedQueue(SIZE);
414 <                        for (int i = 0; i < SIZE; ++i) {
415 <                            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) { }
409 >    public void testInterruptedTimedPoll() throws InterruptedException {
410 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
411 >        Thread t = newStartedThread(new CheckedRunnable() {
412 >            public void realRun() throws InterruptedException {
413 >                DelayQueue q = populatedQueue(SIZE);
414 >                for (int i = 0; i < SIZE; ++i) {
415 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
416                  }
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    }
417  
418 +                Thread.currentThread().interrupt();
419 +                try {
420 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
421 +                    shouldThrow();
422 +                } catch (InterruptedException success) {}
423 +                assertFalse(Thread.interrupted());
424 +
425 +                pleaseInterrupt.countDown();
426 +                try {
427 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
428 +                    shouldThrow();
429 +                } catch (InterruptedException success) {}
430 +                assertFalse(Thread.interrupted());
431 +            }});
432 +
433 +        await(pleaseInterrupt);
434 +        assertThreadStaysAlive(t);
435 +        t.interrupt();
436 +        awaitTermination(t);
437 +    }
438  
439      /**
440       * peek returns next element, or null if empty
# Line 591 | Line 443 | public class DelayQueueTest extends JSR1
443          DelayQueue q = populatedQueue(SIZE);
444          for (int i = 0; i < SIZE; ++i) {
445              assertEquals(new PDelay(i), ((PDelay)q.peek()));
446 <            q.poll();
446 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
447              if (q.isEmpty())
448                  assertNull(q.peek());
449              else
450 <                assertTrue(i != ((PDelay)q.peek()).intValue());
450 >                assertFalse(new PDelay(i).equals(q.peek()));
451          }
452 <        assertNull(q.peek());
452 >        assertNull(q.peek());
453      }
454  
455      /**
# Line 612 | Line 464 | public class DelayQueueTest extends JSR1
464          try {
465              q.element();
466              shouldThrow();
467 <        }
616 <        catch (NoSuchElementException success) {}
467 >        } catch (NoSuchElementException success) {}
468      }
469  
470      /**
# Line 627 | Line 478 | public class DelayQueueTest extends JSR1
478          try {
479              q.remove();
480              shouldThrow();
481 <        } catch (NoSuchElementException success){
631 <        }
481 >        } catch (NoSuchElementException success) {}
482      }
483  
484      /**
# Line 727 | Line 577 | public class DelayQueueTest extends JSR1
577      /**
578       * toArray contains all elements
579       */
580 <    public void testToArray() {
580 >    public void testToArray() throws InterruptedException {
581          DelayQueue q = populatedQueue(SIZE);
582 <        Object[] o = q.toArray();
582 >        Object[] o = q.toArray();
583          Arrays.sort(o);
584 <        try {
585 <        for (int i = 0; i < o.length; i++)
736 <            assertEquals(o[i], q.take());
737 <        } catch (InterruptedException e){
738 <            unexpectedException();
739 <        }
584 >        for (int i = 0; i < o.length; i++)
585 >            assertSame(o[i], q.take());
586      }
587  
588      /**
589       * toArray(a) contains all elements
590       */
591      public void testToArray2() {
592 <        DelayQueue q = populatedQueue(SIZE);
593 <        PDelay[] ints = new PDelay[SIZE];
594 <        ints = (PDelay[])q.toArray(ints);
592 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
593 >        PDelay[] ints = new PDelay[SIZE];
594 >        PDelay[] array = q.toArray(ints);
595 >        assertSame(ints, array);
596          Arrays.sort(ints);
597 <        try {
598 <            for (int i = 0; i < ints.length; i++)
752 <                assertEquals(ints[i], q.take());
753 <        } catch (InterruptedException e){
754 <            unexpectedException();
755 <        }
756 <    }
757 <
758 <
759 <    /**
760 <     * 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){}
597 >        for (int i = 0; i < ints.length; i++)
598 >            assertSame(ints[i], q.remove());
599      }
600  
601      /**
602 <     * toArray with incompatible array type throws CCE
602 >     * toArray(incompatible array type) throws ArrayStoreException
603       */
604      public void testToArray1_BadArg() {
605 <        try {
606 <            DelayQueue q = populatedQueue(SIZE);
607 <            Object o[] = q.toArray(new String[10] );
608 <            shouldThrow();
609 <        } catch (ArrayStoreException  success){}
605 >        DelayQueue q = populatedQueue(SIZE);
606 >        try {
607 >            q.toArray(new String[10]);
608 >            shouldThrow();
609 >        } catch (ArrayStoreException success) {}
610      }
611  
612      /**
# Line 784 | Line 615 | public class DelayQueueTest extends JSR1
615      public void testIterator() {
616          DelayQueue q = populatedQueue(SIZE);
617          int i = 0;
618 <        Iterator it = q.iterator();
618 >        Iterator it = q.iterator();
619          while (it.hasNext()) {
620              assertTrue(q.contains(it.next()));
621              ++i;
# Line 795 | Line 626 | public class DelayQueueTest extends JSR1
626      /**
627       * iterator.remove removes current element
628       */
629 <    public void testIteratorRemove () {
629 >    public void testIteratorRemove() {
630          final DelayQueue q = new DelayQueue();
631          q.add(new PDelay(2));
632          q.add(new PDelay(1));
# Line 809 | Line 640 | public class DelayQueueTest extends JSR1
640          assertFalse(it.hasNext());
641      }
642  
812
643      /**
644       * toString contains toStrings of elements
645       */
646      public void testToString() {
647          DelayQueue q = populatedQueue(SIZE);
648          String s = q.toString();
649 <        for (int i = 0; i < SIZE; ++i) {
650 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
821 <        }
649 >        for (Object e : q)
650 >            assertTrue(s.contains(e.toString()));
651      }
652  
653      /**
654 <     * offer transfers elements across Executor tasks
654 >     * timed poll transfers elements across Executor tasks
655       */
656      public void testPollInExecutor() {
657          final DelayQueue q = new DelayQueue();
658 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
659          ExecutorService executor = Executors.newFixedThreadPool(2);
660 <        executor.execute(new Runnable() {
661 <            public void run() {
662 <                threadAssertNull(q.poll());
663 <                try {
664 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
665 <                    threadAssertTrue(q.isEmpty());
666 <                }
667 <                catch (InterruptedException e) {
668 <                    threadUnexpectedException();
669 <                }
670 <            }
671 <        });
660 >        executor.execute(new CheckedRunnable() {
661 >            public void realRun() throws InterruptedException {
662 >                assertNull(q.poll());
663 >                threadsStarted.await();
664 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
665 >                checkEmpty(q);
666 >            }});
667 >
668 >        executor.execute(new CheckedRunnable() {
669 >            public void realRun() throws InterruptedException {
670 >                threadsStarted.await();
671 >                q.put(new PDelay(1));
672 >            }});
673  
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        });
674          joinPool(executor);
855
675      }
676  
858
677      /**
678       * Delayed actions do not occur until their delay elapses
679       */
680 <    public void testDelay() {
681 <        DelayQueue q = new DelayQueue();
682 <        NanoDelay[] elements = new NanoDelay[SIZE];
683 <        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 <        }
680 >    public void testDelay() throws InterruptedException {
681 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
682 >        for (int i = 0; i < SIZE; ++i)
683 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
684  
685 <        try {
686 <            long last = 0;
687 <            for (int i = 0; i < SIZE; ++i) {
688 <                NanoDelay e = (NanoDelay)(q.take());
689 <                long tt = e.getTriggerTime();
690 <                assertTrue(tt <= System.nanoTime());
691 <                if (i != 0)
692 <                    assertTrue(tt >= last);
880 <                last = tt;
881 <            }
882 <        }
883 <        catch (InterruptedException ie) {
884 <            unexpectedException();
685 >        long last = 0;
686 >        for (int i = 0; i < SIZE; ++i) {
687 >            NanoDelay e = q.take();
688 >            long tt = e.getTriggerTime();
689 >            assertTrue(System.nanoTime() - tt >= 0);
690 >            if (i != 0)
691 >                assertTrue(tt >= last);
692 >            last = tt;
693          }
694 +        assertTrue(q.isEmpty());
695      }
696  
697      /**
# Line 891 | Line 700 | public class DelayQueueTest extends JSR1
700      public void testPeekDelayed() {
701          DelayQueue q = new DelayQueue();
702          q.add(new NanoDelay(Long.MAX_VALUE));
703 <        assert(q.peek() != null);
703 >        assertNotNull(q.peek());
704      }
705  
897
706      /**
707       * poll of a non-empty queue returns null if no expired elements.
708       */
# Line 907 | Line 715 | public class DelayQueueTest extends JSR1
715      /**
716       * timed poll of a non-empty queue returns null if no expired elements.
717       */
718 <    public void testTimedPollDelayed() {
718 >    public void testTimedPollDelayed() throws InterruptedException {
719          DelayQueue q = new DelayQueue();
720          q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
721 <        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 <        }
721 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
722      }
723  
724      /**
# Line 972 | Line 752 | public class DelayQueueTest extends JSR1
752      /**
753       * drainTo empties queue
754       */
755 <    public void testDrainToWithActivePut() {
755 >    public void testDrainToWithActivePut() throws InterruptedException {
756          final DelayQueue q = populatedQueue(SIZE);
757 <        Thread t = new Thread(new Runnable() {
758 <                public void run() {
759 <                    q.put(new PDelay(SIZE+1));
760 <                }
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 <    }
757 >        Thread t = new Thread(new CheckedRunnable() {
758 >            public void realRun() {
759 >                q.put(new PDelay(SIZE+1));
760 >            }});
761  
762 <    /**
763 <     * drainTo(null, n) throws NPE
764 <     */
765 <    public void testDrainToNullN() {
766 <        DelayQueue q = populatedQueue(SIZE);
767 <        try {
1000 <            q.drainTo(null, 0);
1001 <            shouldThrow();
1002 <        } catch (NullPointerException success) {
1003 <        }
1004 <    }
1005 <
1006 <    /**
1007 <     * drainTo(this, n) throws IAE
1008 <     */
1009 <    public void testDrainToSelfN() {
1010 <        DelayQueue q = populatedQueue(SIZE);
1011 <        try {
1012 <            q.drainTo(q, 0);
1013 <            shouldThrow();
1014 <        } catch (IllegalArgumentException success) {
1015 <        }
762 >        t.start();
763 >        ArrayList l = new ArrayList();
764 >        q.drainTo(l);
765 >        assertTrue(l.size() >= SIZE);
766 >        t.join();
767 >        assertTrue(q.size() + l.size() >= SIZE);
768      }
769  
770      /**
771 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
771 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
772       */
773      public void testDrainToN() {
774          for (int i = 0; i < SIZE + 2; ++i) {
775              DelayQueue q = populatedQueue(SIZE);
776              ArrayList l = new ArrayList();
777              q.drainTo(l, i);
778 <            int k = (i < SIZE)? i : SIZE;
778 >            int k = (i < SIZE) ? i : SIZE;
779              assertEquals(q.size(), SIZE-k);
780              assertEquals(l.size(), k);
781          }
782      }
783  
1032
784   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines