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.13 by dl, Thu Sep 22 00:32:09 2005 UTC vs.
Revision 1.50 by jsr166, Fri May 27 20:07:24 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 static java.util.concurrent.TimeUnit.MILLISECONDS;
12   import java.util.concurrent.*;
13  
14   public class DelayQueueTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run(suite());
17      }
18  
19      public static Test suite() {
20 <        return new TestSuite(DelayQueueTest.class);
20 >        return new TestSuite(DelayQueueTest.class);
21      }
22  
23      private static final int NOCAP = Integer.MAX_VALUE;
24  
25      /**
26       * A delayed implementation for testing.
27 <     * Most  tests use Pseudodelays, where delays are all elapsed
27 >     * Most tests use Pseudodelays, where delays are all elapsed
28       * (so, no blocking solely for delays) but are still ordered
29 <     */
30 <    static class PDelay implements Delayed {
29 >     */
30 >    static class PDelay implements Delayed {
31          int pseudodelay;
32          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
33          public int compareTo(PDelay y) {
34              int i = pseudodelay;
35 <            int j = ((PDelay)y).pseudodelay;
35 >            int j = y.pseudodelay;
36              if (i < j) return -1;
37              if (i > j) return 1;
38              return 0;
39          }
40  
41          public int compareTo(Delayed y) {
42 <            int i = pseudodelay;
42 <            int j = ((PDelay)y).pseudodelay;
43 <            if (i < j) return -1;
44 <            if (i > j) return 1;
45 <            return 0;
42 >            return compareTo((PDelay)y);
43          }
44  
45          public boolean equals(Object other) {
46 <            return ((PDelay)other).pseudodelay == pseudodelay;
46 >            return equals((PDelay)other);
47          }
48          public boolean equals(PDelay other) {
49 <            return ((PDelay)other).pseudodelay == pseudodelay;
49 >            return other.pseudodelay == pseudodelay;
50          }
51  
55
52          public long getDelay(TimeUnit ignore) {
53              return pseudodelay;
54          }
# Line 65 | Line 61 | public class DelayQueueTest extends JSR1
61          }
62      }
63  
68
64      /**
65       * Delayed implementation that actually delays
66       */
67 <    static class NanoDelay implements Delayed {
67 >    static class NanoDelay implements Delayed {
68          long trigger;
69 <        NanoDelay(long i) {
69 >        NanoDelay(long i) {
70              trigger = System.nanoTime() + i;
71          }
72          public int compareTo(NanoDelay y) {
73              long i = trigger;
74 <            long j = ((NanoDelay)y).trigger;
74 >            long j = y.trigger;
75              if (i < j) return -1;
76              if (i > j) return 1;
77              return 0;
78          }
79  
80          public int compareTo(Delayed y) {
81 <            long i = trigger;
87 <            long j = ((NanoDelay)y).trigger;
88 <            if (i < j) return -1;
89 <            if (i > j) return 1;
90 <            return 0;
81 >            return compareTo((NanoDelay)y);
82          }
83  
84          public boolean equals(Object other) {
85 <            return ((NanoDelay)other).trigger == trigger;
85 >            return equals((NanoDelay)other);
86          }
87          public boolean equals(NanoDelay other) {
88 <            return ((NanoDelay)other).trigger == trigger;
88 >            return other.trigger == trigger;
89          }
90  
91          public long getDelay(TimeUnit unit) {
# Line 111 | Line 102 | public class DelayQueueTest extends JSR1
102          }
103      }
104  
114
105      /**
106       * Create a queue of given size containing consecutive
107       * PDelays 0 ... n.
108       */
109 <    private DelayQueue populatedQueue(int n) {
110 <        DelayQueue q = new DelayQueue();
109 >    private DelayQueue<PDelay> populatedQueue(int n) {
110 >        DelayQueue<PDelay> q = new DelayQueue<PDelay>();
111          assertTrue(q.isEmpty());
112 <        for(int i = n-1; i >= 0; i-=2)
113 <            assertTrue(q.offer(new PDelay(i)));
114 <        for(int i = (n & 1); i < n; i+=2)
115 <            assertTrue(q.offer(new PDelay(i)));
112 >        for (int i = n-1; i >= 0; i-=2)
113 >            assertTrue(q.offer(new PDelay(i)));
114 >        for (int i = (n & 1); i < n; i+=2)
115 >            assertTrue(q.offer(new PDelay(i)));
116          assertFalse(q.isEmpty());
117          assertEquals(NOCAP, q.remainingCapacity());
118 <        assertEquals(n, q.size());
118 >        assertEquals(n, q.size());
119          return q;
120      }
121 <
121 >
122      /**
123       * A new queue has unbounded capacity
124       */
# Line 143 | Line 133 | public class DelayQueueTest extends JSR1
133          try {
134              DelayQueue q = new DelayQueue(null);
135              shouldThrow();
136 <        }
147 <        catch (NullPointerException success) {}
136 >        } catch (NullPointerException success) {}
137      }
138  
139      /**
# Line 155 | Line 144 | public class DelayQueueTest extends JSR1
144              PDelay[] ints = new PDelay[SIZE];
145              DelayQueue q = new DelayQueue(Arrays.asList(ints));
146              shouldThrow();
147 <        }
159 <        catch (NullPointerException success) {}
147 >        } catch (NullPointerException success) {}
148      }
149  
150      /**
# Line 169 | Line 157 | public class DelayQueueTest extends JSR1
157                  ints[i] = new PDelay(i);
158              DelayQueue q = new DelayQueue(Arrays.asList(ints));
159              shouldThrow();
160 <        }
173 <        catch (NullPointerException success) {}
160 >        } catch (NullPointerException success) {}
161      }
162  
163      /**
164       * Queue contains all elements of collection used to initialize
165       */
166      public void testConstructor6() {
167 <        try {
168 <            PDelay[] ints = new PDelay[SIZE];
169 <            for (int i = 0; i < SIZE; ++i)
170 <                ints[i] = new PDelay(i);
171 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
172 <            for (int i = 0; i < SIZE; ++i)
186 <                assertEquals(ints[i], q.poll());
187 <        }
188 <        finally {}
167 >        PDelay[] ints = new PDelay[SIZE];
168 >        for (int i = 0; i < SIZE; ++i)
169 >            ints[i] = new PDelay(i);
170 >        DelayQueue q = new DelayQueue(Arrays.asList(ints));
171 >        for (int i = 0; i < SIZE; ++i)
172 >            assertEquals(ints[i], q.poll());
173      }
174  
175      /**
# Line 204 | Line 188 | public class DelayQueueTest extends JSR1
188      }
189  
190      /**
191 <     * remainingCapacity does not change when elementa added or removed,
191 >     * remainingCapacity does not change when elements added or removed,
192       * but size does
193       */
194      public void testRemainingCapacity() {
# Line 225 | Line 209 | public class DelayQueueTest extends JSR1
209       * offer(null) throws NPE
210       */
211      public void testOfferNull() {
212 <        try {
212 >        try {
213              DelayQueue q = new DelayQueue();
214              q.offer(null);
215              shouldThrow();
216 <        } catch (NullPointerException success) { }  
216 >        } catch (NullPointerException success) {}
217      }
218  
219      /**
220       * add(null) throws NPE
221       */
222      public void testAddNull() {
223 <        try {
223 >        try {
224              DelayQueue q = new DelayQueue();
225              q.add(null);
226              shouldThrow();
227 <        } catch (NullPointerException success) { }  
227 >        } catch (NullPointerException success) {}
228      }
229  
230      /**
# Line 271 | Line 255 | public class DelayQueueTest extends JSR1
255              DelayQueue q = new DelayQueue();
256              q.addAll(null);
257              shouldThrow();
258 <        }
275 <        catch (NullPointerException success) {}
258 >        } catch (NullPointerException success) {}
259      }
260  
278
261      /**
262       * addAll(this) throws IAE
263       */
# Line 284 | Line 266 | public class DelayQueueTest extends JSR1
266              DelayQueue q = populatedQueue(SIZE);
267              q.addAll(q);
268              shouldThrow();
269 <        }
288 <        catch (IllegalArgumentException success) {}
269 >        } catch (IllegalArgumentException success) {}
270      }
271  
272      /**
# Line 297 | Line 278 | public class DelayQueueTest extends JSR1
278              PDelay[] ints = new PDelay[SIZE];
279              q.addAll(Arrays.asList(ints));
280              shouldThrow();
281 <        }
301 <        catch (NullPointerException success) {}
281 >        } catch (NullPointerException success) {}
282      }
283 +
284      /**
285       * addAll of a collection with any null elements throws NPE after
286       * possibly adding some elements
# Line 312 | Line 293 | public class DelayQueueTest extends JSR1
293                  ints[i] = new PDelay(i);
294              q.addAll(Arrays.asList(ints));
295              shouldThrow();
296 <        }
316 <        catch (NullPointerException success) {}
296 >        } catch (NullPointerException success) {}
297      }
298  
299      /**
300       * Queue contains all elements of successful addAll
301       */
302      public void testAddAll5() {
303 <        try {
304 <            PDelay[] empty = new PDelay[0];
305 <            PDelay[] ints = new PDelay[SIZE];
306 <            for (int i = SIZE-1; i >= 0; --i)
307 <                ints[i] = new PDelay(i);
308 <            DelayQueue q = new DelayQueue();
309 <            assertFalse(q.addAll(Arrays.asList(empty)));
310 <            assertTrue(q.addAll(Arrays.asList(ints)));
311 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
303 >        PDelay[] empty = new PDelay[0];
304 >        PDelay[] ints = new PDelay[SIZE];
305 >        for (int i = SIZE-1; i >= 0; --i)
306 >            ints[i] = new PDelay(i);
307 >        DelayQueue q = new DelayQueue();
308 >        assertFalse(q.addAll(Arrays.asList(empty)));
309 >        assertTrue(q.addAll(Arrays.asList(ints)));
310 >        for (int i = 0; i < SIZE; ++i)
311 >            assertEquals(ints[i], q.poll());
312      }
313  
314      /**
315       * put(null) throws NPE
316       */
317 <     public void testPutNull() {
318 <        try {
317 >    public void testPutNull() {
318 >        try {
319              DelayQueue q = new DelayQueue();
320              q.put(null);
321              shouldThrow();
322 <        }
323 <        catch (NullPointerException success){
347 <        }  
348 <     }
322 >        } catch (NullPointerException success) {}
323 >    }
324  
325      /**
326       * all elements successfully put are contained
327       */
328 <     public void testPut() {
329 <         try {
330 <             DelayQueue q = new DelayQueue();
331 <             for (int i = 0; i < SIZE; ++i) {
332 <                 PDelay I = new PDelay(i);
333 <                 q.put(I);
359 <                 assertTrue(q.contains(I));
360 <             }
361 <             assertEquals(SIZE, q.size());
362 <         }
363 <         finally {
328 >    public void testPut() {
329 >        DelayQueue q = new DelayQueue();
330 >        for (int i = 0; i < SIZE; ++i) {
331 >            PDelay I = new PDelay(i);
332 >            q.put(I);
333 >            assertTrue(q.contains(I));
334          }
335 +        assertEquals(SIZE, q.size());
336      }
337  
338      /**
339       * put doesn't block waiting for take
340       */
341 <    public void testPutWithTake() {
341 >    public void testPutWithTake() throws InterruptedException {
342          final DelayQueue q = new DelayQueue();
343 <        Thread t = new Thread(new Runnable() {
344 <                public void run() {
345 <                    int added = 0;
346 <                    try {
347 <                        q.put(new PDelay(0));
348 <                        ++added;
349 <                        q.put(new PDelay(0));
350 <                        ++added;
351 <                        q.put(new PDelay(0));
352 <                        ++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 <        }
343 >        Thread t = newStartedThread(new CheckedRunnable() {
344 >            public void realRun() {
345 >                q.put(new PDelay(0));
346 >                q.put(new PDelay(0));
347 >                q.put(new PDelay(0));
348 >                q.put(new PDelay(0));
349 >            }});
350 >
351 >        awaitTermination(t);
352 >        assertEquals(4, q.size());
353      }
354  
355      /**
356       * timed offer does not time out
357       */
358 <    public void testTimedOffer() {
358 >    public void testTimedOffer() throws InterruptedException {
359          final DelayQueue q = new DelayQueue();
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run() {
362 <                    try {
363 <                        q.put(new PDelay(0));
364 <                        q.put(new PDelay(0));
365 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
366 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
367 <                    } finally { }
368 <                }
414 <            });
415 <        
416 <        try {
417 <            t.start();
418 <            Thread.sleep(SMALL_DELAY_MS);
419 <            t.interrupt();
420 <            t.join();
421 <        } catch (Exception e){
422 <            unexpectedException();
423 <        }
360 >        Thread t = newStartedThread(new CheckedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                q.put(new PDelay(0));
363 >                q.put(new PDelay(0));
364 >                assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
365 >                assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
366 >            }});
367 >
368 >        awaitTermination(t);
369      }
370  
371      /**
372       * take retrieves elements in priority order
373       */
374 <    public void testTake() {
375 <        try {
376 <            DelayQueue q = populatedQueue(SIZE);
377 <            for (int i = 0; i < SIZE; ++i) {
378 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
434 <            }
435 <        } catch (InterruptedException e){
436 <            unexpectedException();
437 <        }  
374 >    public void testTake() throws InterruptedException {
375 >        DelayQueue q = populatedQueue(SIZE);
376 >        for (int i = 0; i < SIZE; ++i) {
377 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
378 >        }
379      }
380  
381      /**
382       * take blocks interruptibly when empty
383       */
384 <    public void testTakeFromEmpty() {
385 <        final DelayQueue q = new DelayQueue();
386 <        Thread t = new Thread(new Runnable() {
387 <                public void run() {
388 <                    try {
389 <                        q.take();
390 <                        threadShouldThrow();
391 <                    } catch (InterruptedException success){ }                
392 <                }
393 <            });
394 <        try {
395 <            t.start();
396 <            Thread.sleep(SHORT_DELAY_MS);
397 <            t.interrupt();
398 <            t.join();
399 <        } catch (Exception e){
400 <            unexpectedException();
401 <        }
384 >    public void testTakeFromEmptyBlocksInterruptibly()
385 >            throws InterruptedException {
386 >        final BlockingQueue q = new DelayQueue();
387 >        final CountDownLatch threadStarted = new CountDownLatch(1);
388 >        Thread t = newStartedThread(new CheckedRunnable() {
389 >            public void realRun() {
390 >                threadStarted.countDown();
391 >                try {
392 >                    q.take();
393 >                    shouldThrow();
394 >                } catch (InterruptedException success) {}
395 >                assertFalse(Thread.interrupted());
396 >            }});
397 >
398 >        await(threadStarted);
399 >        assertThreadStaysAlive(t);
400 >        t.interrupt();
401 >        awaitTermination(t);
402      }
403  
404      /**
405       * Take removes existing elements until empty, then blocks interruptibly
406       */
407 <    public void testBlockingTake() {
408 <        Thread t = new Thread(new Runnable() {
409 <                public void run() {
410 <                    try {
411 <                        DelayQueue q = populatedQueue(SIZE);
412 <                        for (int i = 0; i < SIZE; ++i) {
413 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
414 <                        }
415 <                        q.take();
416 <                        threadShouldThrow();
417 <                    } catch (InterruptedException success){
418 <                    }  
419 <                }});
420 <        t.start();
421 <        try {
481 <           Thread.sleep(SHORT_DELAY_MS);
482 <           t.interrupt();
483 <           t.join();
484 <        }
485 <        catch (InterruptedException ie) {
486 <            unexpectedException();
487 <        }
488 <    }
407 >    public void testBlockingTake() throws InterruptedException {
408 >        final DelayQueue q = populatedQueue(SIZE);
409 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
410 >        Thread t = newStartedThread(new CheckedRunnable() {
411 >            public void realRun() throws InterruptedException {
412 >                for (int i = 0; i < SIZE; ++i) {
413 >                    assertEquals(new PDelay(i), ((PDelay)q.take()));
414 >                }
415 >
416 >                Thread.currentThread().interrupt();
417 >                try {
418 >                    q.take();
419 >                    shouldThrow();
420 >                } catch (InterruptedException success) {}
421 >                assertFalse(Thread.interrupted());
422  
423 +                pleaseInterrupt.countDown();
424 +                try {
425 +                    q.take();
426 +                    shouldThrow();
427 +                } catch (InterruptedException success) {}
428 +                assertFalse(Thread.interrupted());
429 +            }});
430 +
431 +        await(pleaseInterrupt);
432 +        assertThreadStaysAlive(t);
433 +        t.interrupt();
434 +        awaitTermination(t);
435 +    }
436  
437      /**
438       * poll succeeds unless empty
# Line 496 | Line 442 | public class DelayQueueTest extends JSR1
442          for (int i = 0; i < SIZE; ++i) {
443              assertEquals(new PDelay(i), ((PDelay)q.poll()));
444          }
445 <        assertNull(q.poll());
445 >        assertNull(q.poll());
446      }
447  
448      /**
449 <     * timed pool with zero timeout succeeds when non-empty, else times out
449 >     * timed poll with zero timeout succeeds when non-empty, else times out
450       */
451 <    public void testTimedPoll0() {
452 <        try {
453 <            DelayQueue q = populatedQueue(SIZE);
454 <            for (int i = 0; i < SIZE; ++i) {
455 <                assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
456 <            }
511 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
512 <        } catch (InterruptedException e){
513 <            unexpectedException();
514 <        }  
451 >    public void testTimedPoll0() throws InterruptedException {
452 >        DelayQueue q = populatedQueue(SIZE);
453 >        for (int i = 0; i < SIZE; ++i) {
454 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
455 >        }
456 >        assertNull(q.poll(0, MILLISECONDS));
457      }
458  
459      /**
460 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
460 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
461       */
462 <    public void testTimedPoll() {
463 <        try {
464 <            DelayQueue q = populatedQueue(SIZE);
465 <            for (int i = 0; i < SIZE; ++i) {
466 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
467 <            }
526 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
527 <        } catch (InterruptedException e){
528 <            unexpectedException();
529 <        }  
462 >    public void testTimedPoll() throws InterruptedException {
463 >        DelayQueue q = populatedQueue(SIZE);
464 >        for (int i = 0; i < SIZE; ++i) {
465 >            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
466 >        }
467 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
468      }
469  
470      /**
471       * Interrupted timed poll throws InterruptedException instead of
472       * returning timeout status
473       */
474 <    public void testInterruptedTimedPoll() {
475 <        Thread t = new Thread(new Runnable() {
476 <                public void run() {
477 <                    try {
478 <                        DelayQueue q = populatedQueue(SIZE);
479 <                        for (int i = 0; i < SIZE; ++i) {
480 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
481 <                        }
482 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
483 <                    } catch (InterruptedException success){
484 <                    }  
485 <                }});
486 <        t.start();
487 <        try {
488 <           Thread.sleep(SHORT_DELAY_MS);
489 <           t.interrupt();
490 <           t.join();
491 <        }
492 <        catch (InterruptedException ie) {
493 <            unexpectedException();
494 <        }
474 >    public void testInterruptedTimedPoll() throws InterruptedException {
475 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
476 >        Thread t = newStartedThread(new CheckedRunnable() {
477 >            public void realRun() throws InterruptedException {
478 >                DelayQueue q = populatedQueue(SIZE);
479 >                for (int i = 0; i < SIZE; ++i) {
480 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
481 >                }
482 >
483 >                Thread.currentThread().interrupt();
484 >                try {
485 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
486 >                    shouldThrow();
487 >                } catch (InterruptedException success) {}
488 >                assertFalse(Thread.interrupted());
489 >
490 >                pleaseInterrupt.countDown();
491 >                try {
492 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
493 >                    shouldThrow();
494 >                } catch (InterruptedException success) {}
495 >                assertFalse(Thread.interrupted());
496 >            }});
497 >
498 >        await(pleaseInterrupt);
499 >        assertThreadStaysAlive(t);
500 >        t.interrupt();
501 >        awaitTermination(t);
502      }
503  
504      /**
505 <     *  timed poll before a delayed offer fails; after offer succeeds;
506 <     *  on interruption throws
505 >     * timed poll before a delayed offer fails; after offer succeeds;
506 >     * on interruption throws
507       */
508 <    public void testTimedPollWithOffer() {
508 >    public void testTimedPollWithOffer() throws InterruptedException {
509          final DelayQueue q = new DelayQueue();
510 <        Thread t = new Thread(new Runnable() {
511 <                public void run() {
512 <                    try {
513 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
514 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
515 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
516 <                        threadFail("Should block");
517 <                    } catch (InterruptedException success) { }                
518 <                }
519 <            });
520 <        try {
521 <            t.start();
522 <            Thread.sleep(SMALL_DELAY_MS);
523 <            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
524 <            t.interrupt();
580 <            t.join();
581 <        } catch (Exception e){
582 <            unexpectedException();
583 <        }
584 <    }  
510 >        final PDelay pdelay = new PDelay(0);
511 >        final CheckedBarrier barrier = new CheckedBarrier(2);
512 >        Thread t = newStartedThread(new CheckedRunnable() {
513 >            public void realRun() throws InterruptedException {
514 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
515 >
516 >                barrier.await();
517 >                assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS));
518 >
519 >                Thread.currentThread().interrupt();
520 >                try {
521 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
522 >                    shouldThrow();
523 >                } catch (InterruptedException success) {}
524 >                assertFalse(Thread.interrupted());
525  
526 +                barrier.await();
527 +                try {
528 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
529 +                    shouldThrow();
530 +                } catch (InterruptedException success) {}
531 +                assertFalse(Thread.interrupted());
532 +            }});
533 +
534 +        barrier.await();
535 +        long startTime = System.nanoTime();
536 +        assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS));
537 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
538 +
539 +        barrier.await();
540 +        assertThreadStaysAlive(t);
541 +        t.interrupt();
542 +        awaitTermination(t);
543 +    }
544  
545      /**
546       * peek returns next element, or null if empty
# Line 591 | Line 549 | public class DelayQueueTest extends JSR1
549          DelayQueue q = populatedQueue(SIZE);
550          for (int i = 0; i < SIZE; ++i) {
551              assertEquals(new PDelay(i), ((PDelay)q.peek()));
552 <            q.poll();
552 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
553              if (q.isEmpty())
554                  assertNull(q.peek());
555              else
556 <                assertTrue(i != ((PDelay)q.peek()).intValue());
556 >                assertFalse(new PDelay(i).equals(q.peek()));
557          }
558 <        assertNull(q.peek());
558 >        assertNull(q.peek());
559      }
560  
561      /**
# Line 612 | Line 570 | public class DelayQueueTest extends JSR1
570          try {
571              q.element();
572              shouldThrow();
573 <        }
616 <        catch (NoSuchElementException success) {}
573 >        } catch (NoSuchElementException success) {}
574      }
575  
576      /**
# Line 627 | Line 584 | public class DelayQueueTest extends JSR1
584          try {
585              q.remove();
586              shouldThrow();
587 <        } catch (NoSuchElementException success){
631 <        }  
587 >        } catch (NoSuchElementException success) {}
588      }
589  
590      /**
# Line 645 | Line 601 | public class DelayQueueTest extends JSR1
601          }
602          assertTrue(q.isEmpty());
603      }
604 <        
604 >
605      /**
606       * contains(x) reports true when elements added but not yet removed
607       */
# Line 727 | Line 683 | public class DelayQueueTest extends JSR1
683      /**
684       * toArray contains all elements
685       */
686 <    public void testToArray() {
686 >    public void testToArray() throws InterruptedException {
687          DelayQueue q = populatedQueue(SIZE);
688 <        Object[] o = q.toArray();
688 >        Object[] o = q.toArray();
689          Arrays.sort(o);
690 <        try {
691 <        for(int i = 0; i < o.length; i++)
736 <            assertEquals(o[i], q.take());
737 <        } catch (InterruptedException e){
738 <            unexpectedException();
739 <        }    
690 >        for (int i = 0; i < o.length; i++)
691 >            assertSame(o[i], q.take());
692      }
693  
694      /**
695       * toArray(a) contains all elements
696       */
697      public void testToArray2() {
698 <        DelayQueue q = populatedQueue(SIZE);
699 <        PDelay[] ints = new PDelay[SIZE];
700 <        ints = (PDelay[])q.toArray(ints);
698 >        DelayQueue<PDelay> q = populatedQueue(SIZE);
699 >        PDelay[] ints = new PDelay[SIZE];
700 >        PDelay[] array = q.toArray(ints);
701 >        assertSame(ints, array);
702          Arrays.sort(ints);
703 <        try {
704 <            for(int i = 0; i < ints.length; i++)
752 <                assertEquals(ints[i], q.take());
753 <        } catch (InterruptedException e){
754 <            unexpectedException();
755 <        }    
703 >        for (int i = 0; i < ints.length; i++)
704 >            assertSame(ints[i], q.remove());
705      }
706  
758
707      /**
708 <     * toArray(null) throws NPE
708 >     * toArray(null) throws NullPointerException
709       */
710 <    public void testToArray_BadArg() {
711 <        try {
712 <            DelayQueue q = populatedQueue(SIZE);
713 <            Object o[] = q.toArray(null);
714 <            shouldThrow();
715 <        } catch(NullPointerException success){}
710 >    public void testToArray_NullArg() {
711 >        DelayQueue q = populatedQueue(SIZE);
712 >        try {
713 >            q.toArray(null);
714 >            shouldThrow();
715 >        } catch (NullPointerException success) {}
716      }
717  
718      /**
719 <     * toArray with incompatible array type throws CCE
719 >     * toArray(incompatible array type) throws ArrayStoreException
720       */
721      public void testToArray1_BadArg() {
722 <        try {
723 <            DelayQueue q = populatedQueue(SIZE);
724 <            Object o[] = q.toArray(new String[10] );
725 <            shouldThrow();
726 <        } catch(ArrayStoreException  success){}
722 >        DelayQueue q = populatedQueue(SIZE);
723 >        try {
724 >            q.toArray(new String[10]);
725 >            shouldThrow();
726 >        } catch (ArrayStoreException success) {}
727      }
728 <    
728 >
729      /**
730       * iterator iterates through all elements
731       */
732      public void testIterator() {
733          DelayQueue q = populatedQueue(SIZE);
734          int i = 0;
735 <        Iterator it = q.iterator();
736 <        while(it.hasNext()) {
735 >        Iterator it = q.iterator();
736 >        while (it.hasNext()) {
737              assertTrue(q.contains(it.next()));
738              ++i;
739          }
# Line 795 | Line 743 | public class DelayQueueTest extends JSR1
743      /**
744       * iterator.remove removes current element
745       */
746 <    public void testIteratorRemove () {
746 >    public void testIteratorRemove() {
747          final DelayQueue q = new DelayQueue();
748          q.add(new PDelay(2));
749          q.add(new PDelay(1));
# Line 809 | Line 757 | public class DelayQueueTest extends JSR1
757          assertFalse(it.hasNext());
758      }
759  
812
760      /**
761       * toString contains toStrings of elements
762       */
# Line 817 | Line 764 | public class DelayQueueTest extends JSR1
764          DelayQueue q = populatedQueue(SIZE);
765          String s = q.toString();
766          for (int i = 0; i < SIZE; ++i) {
767 <            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
767 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
768          }
769 <    }        
769 >    }
770  
771      /**
772 <     * offer transfers elements across Executor tasks
772 >     * timed poll transfers elements across Executor tasks
773       */
774      public void testPollInExecutor() {
775          final DelayQueue q = new DelayQueue();
776 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
777          ExecutorService executor = Executors.newFixedThreadPool(2);
778 <        executor.execute(new Runnable() {
779 <            public void run() {
780 <                threadAssertNull(q.poll());
781 <                try {
782 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
783 <                    threadAssertTrue(q.isEmpty());
784 <                }
785 <                catch (InterruptedException e) {
786 <                    threadUnexpectedException();
787 <                }
788 <            }
789 <        });
778 >        executor.execute(new CheckedRunnable() {
779 >            public void realRun() throws InterruptedException {
780 >                assertNull(q.poll());
781 >                threadsStarted.await();
782 >                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
783 >                checkEmpty(q);
784 >            }});
785 >
786 >        executor.execute(new CheckedRunnable() {
787 >            public void realRun() throws InterruptedException {
788 >                threadsStarted.await();
789 >                q.put(new PDelay(1));
790 >            }});
791  
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        });
792          joinPool(executor);
855
793      }
794  
858
795      /**
796       * Delayed actions do not occur until their delay elapses
797       */
798 <    public void testDelay() {
799 <        DelayQueue q = new DelayQueue();
800 <        NanoDelay[] elements = new NanoDelay[SIZE];
801 <        for (int i = 0; i < SIZE; ++i) {
802 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
803 <        }
804 <        for (int i = 0; i < SIZE; ++i) {
805 <            q.add(elements[i]);
806 <        }
807 <
808 <        try {
809 <            long last = 0;
810 <            for (int i = 0; i < SIZE; ++i) {
875 <                NanoDelay e = (NanoDelay)(q.take());
876 <                long tt = e.getTriggerTime();
877 <                assertTrue(tt <= System.nanoTime());
878 <                if (i != 0)
879 <                    assertTrue(tt >= last);
880 <                last = tt;
881 <            }
882 <        }
883 <        catch(InterruptedException ie) {
884 <            unexpectedException();
798 >    public void testDelay() throws InterruptedException {
799 >        DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
800 >        for (int i = 0; i < SIZE; ++i)
801 >            q.add(new NanoDelay(1000000L * (SIZE - i)));
802 >
803 >        long last = 0;
804 >        for (int i = 0; i < SIZE; ++i) {
805 >            NanoDelay e = q.take();
806 >            long tt = e.getTriggerTime();
807 >            assertTrue(System.nanoTime() - tt >= 0);
808 >            if (i != 0)
809 >                assertTrue(tt >= last);
810 >            last = tt;
811          }
812 +        assertTrue(q.isEmpty());
813      }
814  
815      /**
# Line 891 | Line 818 | public class DelayQueueTest extends JSR1
818      public void testPeekDelayed() {
819          DelayQueue q = new DelayQueue();
820          q.add(new NanoDelay(Long.MAX_VALUE));
821 <        assert(q.peek() != null);
821 >        assertNotNull(q.peek());
822      }
823  
897
824      /**
825       * poll of a non-empty queue returns null if no expired elements.
826       */
# Line 907 | Line 833 | public class DelayQueueTest extends JSR1
833      /**
834       * timed poll of a non-empty queue returns null if no expired elements.
835       */
836 <    public void testTimedPollDelayed() {
836 >    public void testTimedPollDelayed() throws InterruptedException {
837          DelayQueue q = new DelayQueue();
838 <        q.add(new NanoDelay(Long.MAX_VALUE));
839 <        try {
914 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
915 <        } catch (Exception ex) {
916 <            unexpectedException();
917 <        }
838 >        q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
839 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
840      }
841  
842      /**
843       * drainTo(null) throws NPE
844 <     */
844 >     */
845      public void testDrainToNull() {
846          DelayQueue q = populatedQueue(SIZE);
847          try {
848              q.drainTo(null);
849              shouldThrow();
850 <        } catch(NullPointerException success) {
929 <        }
850 >        } catch (NullPointerException success) {}
851      }
852  
853      /**
854       * drainTo(this) throws IAE
855 <     */
855 >     */
856      public void testDrainToSelf() {
857          DelayQueue q = populatedQueue(SIZE);
858          try {
859              q.drainTo(q);
860              shouldThrow();
861 <        } catch(IllegalArgumentException success) {
941 <        }
861 >        } catch (IllegalArgumentException success) {}
862      }
863  
864      /**
865       * drainTo(c) empties queue into another collection c
866 <     */
866 >     */
867      public void testDrainTo() {
868          DelayQueue q = new DelayQueue();
869          PDelay[] elems = new PDelay[SIZE];
# Line 954 | Line 874 | public class DelayQueueTest extends JSR1
874          ArrayList l = new ArrayList();
875          q.drainTo(l);
876          assertEquals(q.size(), 0);
877 <        for (int i = 0; i < SIZE; ++i)
877 >        for (int i = 0; i < SIZE; ++i)
878              assertEquals(l.get(i), elems[i]);
879          q.add(elems[0]);
880          q.add(elems[1]);
# Line 965 | Line 885 | public class DelayQueueTest extends JSR1
885          q.drainTo(l);
886          assertEquals(q.size(), 0);
887          assertEquals(l.size(), 2);
888 <        for (int i = 0; i < 2; ++i)
888 >        for (int i = 0; i < 2; ++i)
889              assertEquals(l.get(i), elems[i]);
890      }
891  
892      /**
893       * drainTo empties queue
894 <     */
895 <    public void testDrainToWithActivePut() {
894 >     */
895 >    public void testDrainToWithActivePut() throws InterruptedException {
896          final DelayQueue q = populatedQueue(SIZE);
897 <        Thread t = new Thread(new Runnable() {
898 <                public void run() {
899 <                    q.put(new PDelay(SIZE+1));
900 <                }
901 <            });
902 <        try {
903 <            t.start();
904 <            ArrayList l = new ArrayList();
905 <            q.drainTo(l);
906 <            assertTrue(l.size() >= SIZE);
907 <            t.join();
988 <            assertTrue(q.size() + l.size() >= SIZE);
989 <        } catch(Exception e){
990 <            unexpectedException();
991 <        }
897 >        Thread t = new Thread(new CheckedRunnable() {
898 >            public void realRun() {
899 >                q.put(new PDelay(SIZE+1));
900 >            }});
901 >
902 >        t.start();
903 >        ArrayList l = new ArrayList();
904 >        q.drainTo(l);
905 >        assertTrue(l.size() >= SIZE);
906 >        t.join();
907 >        assertTrue(q.size() + l.size() >= SIZE);
908      }
909  
910      /**
911       * drainTo(null, n) throws NPE
912 <     */
912 >     */
913      public void testDrainToNullN() {
914          DelayQueue q = populatedQueue(SIZE);
915          try {
916              q.drainTo(null, 0);
917              shouldThrow();
918 <        } catch(NullPointerException success) {
1003 <        }
918 >        } catch (NullPointerException success) {}
919      }
920  
921      /**
922       * drainTo(this, n) throws IAE
923 <     */
923 >     */
924      public void testDrainToSelfN() {
925          DelayQueue q = populatedQueue(SIZE);
926          try {
927              q.drainTo(q, 0);
928              shouldThrow();
929 <        } catch(IllegalArgumentException success) {
1015 <        }
929 >        } catch (IllegalArgumentException success) {}
930      }
931  
932      /**
933 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
934 <     */
933 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
934 >     */
935      public void testDrainToN() {
936          for (int i = 0; i < SIZE + 2; ++i) {
937              DelayQueue q = populatedQueue(SIZE);
938              ArrayList l = new ArrayList();
939              q.drainTo(l, i);
940 <            int k = (i < SIZE)? i : SIZE;
940 >            int k = (i < SIZE) ? i : SIZE;
941              assertEquals(q.size(), SIZE-k);
942              assertEquals(l.size(), k);
943          }
944      }
945  
1032
946   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines