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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.20 by jsr166, Sat Nov 21 09:28:16 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
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 <    // Most Q/BQ tests use Pseudodelays, where delays are all elapsed
26 <    // (so, no blocking solely for delays) but are still ordered
27 <
28 <    static class PDelay implements Delayed {
25 >    /**
26 >     * A delayed implementation for testing.
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 {
31          int pseudodelay;
32          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
33 <        public int compareTo(Object y) {
33 >        public int compareTo(PDelay y) {
34              int i = pseudodelay;
35              int j = ((PDelay)y).pseudodelay;
36              if (i < j) return -1;
# Line 34 | Line 38 | public class DelayQueueTest extends JSR1
38              return 0;
39          }
40  
41 <        public int compareTo(PDelay y) {
41 >        public int compareTo(Delayed y) {
42              int i = pseudodelay;
43              int j = ((PDelay)y).pseudodelay;
44              if (i < j) return -1;
# Line 63 | Line 67 | public class DelayQueueTest extends JSR1
67      }
68  
69  
70 +    /**
71 +     * Delayed implementation that actually delays
72 +     */
73 +    static class NanoDelay implements Delayed {
74 +        long trigger;
75 +        NanoDelay(long i) {
76 +            trigger = System.nanoTime() + i;
77 +        }
78 +        public int compareTo(NanoDelay y) {
79 +            long i = trigger;
80 +            long j = ((NanoDelay)y).trigger;
81 +            if (i < j) return -1;
82 +            if (i > j) return 1;
83 +            return 0;
84 +        }
85 +
86 +        public int compareTo(Delayed y) {
87 +            long i = trigger;
88 +            long j = ((NanoDelay)y).trigger;
89 +            if (i < j) return -1;
90 +            if (i > j) return 1;
91 +            return 0;
92 +        }
93 +
94 +        public boolean equals(Object other) {
95 +            return ((NanoDelay)other).trigger == trigger;
96 +        }
97 +        public boolean equals(NanoDelay other) {
98 +            return ((NanoDelay)other).trigger == trigger;
99 +        }
100 +
101 +        public long getDelay(TimeUnit unit) {
102 +            long n = trigger - System.nanoTime();
103 +            return unit.convert(n, TimeUnit.NANOSECONDS);
104 +        }
105 +
106 +        public long getTriggerTime() {
107 +            return trigger;
108 +        }
109 +
110 +        public String toString() {
111 +            return String.valueOf(trigger);
112 +        }
113 +    }
114  
115  
116      /**
# Line 72 | Line 120 | public class DelayQueueTest extends JSR1
120      private DelayQueue populatedQueue(int n) {
121          DelayQueue q = new DelayQueue();
122          assertTrue(q.isEmpty());
123 <        for(int i = n-1; i >= 0; i-=2)
124 <            assertTrue(q.offer(new PDelay(i)));
125 <        for(int i = (n & 1); i < n; i+=2)
126 <            assertTrue(q.offer(new PDelay(i)));
123 >        for (int i = n-1; i >= 0; i-=2)
124 >            assertTrue(q.offer(new PDelay(i)));
125 >        for (int i = (n & 1); i < n; i+=2)
126 >            assertTrue(q.offer(new PDelay(i)));
127          assertFalse(q.isEmpty());
128          assertEquals(NOCAP, q.remainingCapacity());
129 <        assertEquals(n, q.size());
129 >        assertEquals(n, q.size());
130          return q;
131      }
132 <
133 <    public void testConstructor1(){
132 >
133 >    /**
134 >     * A new queue has unbounded capacity
135 >     */
136 >    public void testConstructor1() {
137          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
138      }
139  
140 <    public void testConstructor3(){
141 <
140 >    /**
141 >     * Initializing from null Collection throws NPE
142 >     */
143 >    public void testConstructor3() {
144          try {
145              DelayQueue q = new DelayQueue(null);
146 <            fail("Cannot make from null collection");
147 <        }
95 <        catch (NullPointerException success) {}
146 >            shouldThrow();
147 >        } catch (NullPointerException success) {}
148      }
149  
150 <    public void testConstructor4(){
150 >    /**
151 >     * Initializing from Collection of null elements throws NPE
152 >     */
153 >    public void testConstructor4() {
154          try {
155              PDelay[] ints = new PDelay[SIZE];
156              DelayQueue q = new DelayQueue(Arrays.asList(ints));
157 <            fail("Cannot make with null elements");
158 <        }
104 <        catch (NullPointerException success) {}
157 >            shouldThrow();
158 >        } catch (NullPointerException success) {}
159      }
160  
161 <    public void testConstructor5(){
161 >    /**
162 >     * Initializing from Collection with some null elements throws NPE
163 >     */
164 >    public void testConstructor5() {
165          try {
166              PDelay[] ints = new PDelay[SIZE];
167              for (int i = 0; i < SIZE-1; ++i)
168                  ints[i] = new PDelay(i);
169              DelayQueue q = new DelayQueue(Arrays.asList(ints));
170 <            fail("Cannot make with null elements");
171 <        }
115 <        catch (NullPointerException success) {}
170 >            shouldThrow();
171 >        } catch (NullPointerException success) {}
172      }
173  
174 <    public void testConstructor6(){
174 >    /**
175 >     * Queue contains all elements of collection used to initialize
176 >     */
177 >    public void testConstructor6() {
178          try {
179              PDelay[] ints = new PDelay[SIZE];
180              for (int i = 0; i < SIZE; ++i)
# Line 127 | Line 186 | public class DelayQueueTest extends JSR1
186          finally {}
187      }
188  
189 +    /**
190 +     * isEmpty is true before add, false after
191 +     */
192      public void testEmpty() {
193          DelayQueue q = new DelayQueue();
194          assertTrue(q.isEmpty());
# Line 139 | Line 201 | public class DelayQueueTest extends JSR1
201          assertTrue(q.isEmpty());
202      }
203  
204 <    public void testRemainingCapacity(){
204 >    /**
205 >     * remainingCapacity does not change when elementa added or removed,
206 >     * but size does
207 >     */
208 >    public void testRemainingCapacity() {
209          DelayQueue q = populatedQueue(SIZE);
210          for (int i = 0; i < SIZE; ++i) {
211              assertEquals(NOCAP, q.remainingCapacity());
# Line 153 | Line 219 | public class DelayQueueTest extends JSR1
219          }
220      }
221  
222 <    public void testOfferNull(){
223 <        try {
222 >    /**
223 >     * offer(null) throws NPE
224 >     */
225 >    public void testOfferNull() {
226 >        try {
227              DelayQueue q = new DelayQueue();
228              q.offer(null);
229 <            fail("should throw NPE");
230 <        } catch (NullPointerException success) { }  
229 >            shouldThrow();
230 >        } catch (NullPointerException success) {}
231 >    }
232 >
233 >    /**
234 >     * add(null) throws NPE
235 >     */
236 >    public void testAddNull() {
237 >        try {
238 >            DelayQueue q = new DelayQueue();
239 >            q.add(null);
240 >            shouldThrow();
241 >        } catch (NullPointerException success) {}
242      }
243  
244 +    /**
245 +     * offer non-null succeeds
246 +     */
247      public void testOffer() {
248          DelayQueue q = new DelayQueue();
249          assertTrue(q.offer(new PDelay(0)));
250          assertTrue(q.offer(new PDelay(1)));
251      }
252  
253 <    public void testAdd(){
253 >    /**
254 >     * add succeeds
255 >     */
256 >    public void testAdd() {
257          DelayQueue q = new DelayQueue();
258          for (int i = 0; i < SIZE; ++i) {
259              assertEquals(i, q.size());
# Line 175 | Line 261 | public class DelayQueueTest extends JSR1
261          }
262      }
263  
264 <    public void testAddAll1(){
264 >    /**
265 >     * addAll(null) throws NPE
266 >     */
267 >    public void testAddAll1() {
268          try {
269              DelayQueue q = new DelayQueue();
270              q.addAll(null);
271 <            fail("Cannot add null collection");
272 <        }
273 <        catch (NullPointerException success) {}
271 >            shouldThrow();
272 >        } catch (NullPointerException success) {}
273 >    }
274 >
275 >
276 >    /**
277 >     * addAll(this) throws IAE
278 >     */
279 >    public void testAddAllSelf() {
280 >        try {
281 >            DelayQueue q = populatedQueue(SIZE);
282 >            q.addAll(q);
283 >            shouldThrow();
284 >        } catch (IllegalArgumentException success) {}
285      }
286 <    public void testAddAll2(){
286 >
287 >    /**
288 >     * addAll of a collection with null elements throws NPE
289 >     */
290 >    public void testAddAll2() {
291          try {
292              DelayQueue q = new DelayQueue();
293              PDelay[] ints = new PDelay[SIZE];
294              q.addAll(Arrays.asList(ints));
295 <            fail("Cannot add null elements");
296 <        }
193 <        catch (NullPointerException success) {}
295 >            shouldThrow();
296 >        } catch (NullPointerException success) {}
297      }
298 <    public void testAddAll3(){
298 >    /**
299 >     * addAll of a collection with any null elements throws NPE after
300 >     * possibly adding some elements
301 >     */
302 >    public void testAddAll3() {
303          try {
304              DelayQueue q = new DelayQueue();
305              PDelay[] ints = new PDelay[SIZE];
306              for (int i = 0; i < SIZE-1; ++i)
307                  ints[i] = new PDelay(i);
308              q.addAll(Arrays.asList(ints));
309 <            fail("Cannot add null elements");
310 <        }
204 <        catch (NullPointerException success) {}
309 >            shouldThrow();
310 >        } catch (NullPointerException success) {}
311      }
312  
313 <    public void testAddAll5(){
313 >    /**
314 >     * Queue contains all elements of successful addAll
315 >     */
316 >    public void testAddAll5() {
317          try {
318              PDelay[] empty = new PDelay[0];
319              PDelay[] ints = new PDelay[SIZE];
# Line 219 | Line 328 | public class DelayQueueTest extends JSR1
328          finally {}
329      }
330  
331 +    /**
332 +     * put(null) throws NPE
333 +     */
334       public void testPutNull() {
335 <        try {
335 >        try {
336              DelayQueue q = new DelayQueue();
337              q.put(null);
338 <            fail("put should throw NPE");
339 <        }
228 <        catch (NullPointerException success){
229 <        }  
338 >            shouldThrow();
339 >        } catch (NullPointerException success) {}
340       }
341  
342 +    /**
343 +     * all elements successfully put are contained
344 +     */
345       public void testPut() {
346 <         try {
347 <             DelayQueue q = new DelayQueue();
348 <             for (int i = 0; i < SIZE; ++i) {
349 <                 PDelay I = new PDelay(i);
350 <                 q.put(I);
238 <                 assertTrue(q.contains(I));
239 <             }
240 <             assertEquals(SIZE, q.size());
346 >         DelayQueue q = new DelayQueue();
347 >         for (int i = 0; i < SIZE; ++i) {
348 >             PDelay I = new PDelay(i);
349 >             q.put(I);
350 >             assertTrue(q.contains(I));
351           }
352 <         finally {
243 <        }
352 >         assertEquals(SIZE, q.size());
353      }
354  
355 <    public void testPutWithTake() {
355 >    /**
356 >     * put doesn't block waiting for take
357 >     */
358 >    public void testPutWithTake() throws InterruptedException {
359          final DelayQueue q = new DelayQueue();
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run(){
362 <                    int added = 0;
363 <                    try {
364 <                        q.put(new PDelay(0));
365 <                        ++added;
366 <                        q.put(new PDelay(0));
367 <                        ++added;
368 <                        q.put(new PDelay(0));
369 <                        ++added;
370 <                        q.put(new PDelay(0));
371 <                        ++added;
372 <                        threadAssertTrue(added == 4);
261 <                    } finally {
262 <                    }
263 <                }
264 <            });
265 <        try {
266 <            t.start();
267 <            Thread.sleep(SHORT_DELAY_MS);
268 <            q.take();
269 <            t.interrupt();
270 <            t.join();
271 <        } catch (Exception e){
272 <            fail("Unexpected exception");
273 <        }
360 >        Thread t = new Thread(new CheckedRunnable() {
361 >            public void realRun() {
362 >                q.put(new PDelay(0));
363 >                q.put(new PDelay(0));
364 >                q.put(new PDelay(0));
365 >                q.put(new PDelay(0));
366 >            }});
367 >
368 >        t.start();
369 >        Thread.sleep(SHORT_DELAY_MS);
370 >        q.take();
371 >        t.interrupt();
372 >        t.join();
373      }
374  
375 <    public void testTimedOffer() {
375 >    /**
376 >     * timed offer does not time out
377 >     */
378 >    public void testTimedOffer() throws InterruptedException {
379          final DelayQueue q = new DelayQueue();
380 <        Thread t = new Thread(new Runnable() {
381 <                public void run(){
382 <                    try {
383 <                        q.put(new PDelay(0));
384 <                        q.put(new PDelay(0));
385 <                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
386 <                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
387 <                    } finally { }
388 <                }
389 <            });
390 <        
391 <        try {
290 <            t.start();
291 <            Thread.sleep(SMALL_DELAY_MS);
292 <            t.interrupt();
293 <            t.join();
294 <        } catch (Exception e){
295 <            fail("Unexpected exception");
296 <        }
380 >        Thread t = new Thread(new CheckedRunnable() {
381 >            public void realRun() throws InterruptedException {
382 >                q.put(new PDelay(0));
383 >                q.put(new PDelay(0));
384 >                threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
385 >                threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
386 >            }});
387 >
388 >        t.start();
389 >        Thread.sleep(SMALL_DELAY_MS);
390 >        t.interrupt();
391 >        t.join();
392      }
393  
394 <    public void testTake(){
395 <        try {
396 <            DelayQueue q = populatedQueue(SIZE);
397 <            for (int i = 0; i < SIZE; ++i) {
398 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
399 <            }
400 <        } catch (InterruptedException e){
401 <            fail("Unexpected exception");
307 <        }  
394 >    /**
395 >     * take retrieves elements in priority order
396 >     */
397 >    public void testTake() throws InterruptedException {
398 >        DelayQueue q = populatedQueue(SIZE);
399 >        for (int i = 0; i < SIZE; ++i) {
400 >            assertEquals(new PDelay(i), ((PDelay)q.take()));
401 >        }
402      }
403  
404 <    public void testTakeFromEmpty() {
404 >    /**
405 >     * take blocks interruptibly when empty
406 >     */
407 >    public void testTakeFromEmpty() throws InterruptedException {
408          final DelayQueue q = new DelayQueue();
409 <        Thread t = new Thread(new Runnable() {
410 <                public void run(){
411 <                    try {
412 <                        q.take();
413 <                        threadFail("Should block");
414 <                    } catch (InterruptedException success){ }                
415 <                }
416 <            });
417 <        try {
321 <            t.start();
322 <            Thread.sleep(SHORT_DELAY_MS);
323 <            t.interrupt();
324 <            t.join();
325 <        } catch (Exception e){
326 <            fail("Unexpected exception");
327 <        }
409 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
410 >            public void realRun() throws InterruptedException {
411 >                q.take();
412 >            }};
413 >
414 >        t.start();
415 >        Thread.sleep(SHORT_DELAY_MS);
416 >        t.interrupt();
417 >        t.join();
418      }
419  
420 <    public void testBlockingTake(){
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.take()));
427 <                        }
428 <                        q.take();
429 <                        threadFail("take should block");
430 <                    } catch (InterruptedException success){
431 <                    }  
432 <                }});
420 >    /**
421 >     * Take removes existing elements until empty, then blocks interruptibly
422 >     */
423 >    public void testBlockingTake() throws InterruptedException {
424 >        Thread t = new ThreadShouldThrow(InterruptedException.class) {
425 >            public void realRun() throws InterruptedException {
426 >                DelayQueue q = populatedQueue(SIZE);
427 >                for (int i = 0; i < SIZE; ++i) {
428 >                    threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
429 >                }
430 >                q.take();
431 >            }};
432 >
433          t.start();
434 <        try {
435 <           Thread.sleep(SHORT_DELAY_MS);
436 <           t.interrupt();
347 <           t.join();
348 <        }
349 <        catch (InterruptedException ie) {
350 <            fail("Unexpected exception");
351 <        }
434 >        Thread.sleep(SHORT_DELAY_MS);
435 >        t.interrupt();
436 >        t.join();
437      }
438  
439  
440 <    public void testPoll(){
440 >    /**
441 >     * poll succeeds unless empty
442 >     */
443 >    public void testPoll() {
444          DelayQueue q = populatedQueue(SIZE);
445          for (int i = 0; i < SIZE; ++i) {
446              assertEquals(new PDelay(i), ((PDelay)q.poll()));
447          }
448 <        assertNull(q.poll());
448 >        assertNull(q.poll());
449      }
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 <            }
457 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
458 <        } catch (InterruptedException e){
459 <            fail("Unexpected exception");
372 <        }  
451 >    /**
452 >     * timed pool with zero timeout succeeds when non-empty, else times out
453 >     */
454 >    public void testTimedPoll0() throws InterruptedException {
455 >        DelayQueue q = populatedQueue(SIZE);
456 >        for (int i = 0; i < SIZE; ++i) {
457 >            assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
458 >        }
459 >        assertNull(q.poll(0, MILLISECONDS));
460      }
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 <            }
468 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
382 <        } catch (InterruptedException e){
383 <            fail("Unexpected exception");
384 <        }  
385 <    }
386 <
387 <    public void testInterruptedTimedPoll(){
388 <        Thread t = new Thread(new Runnable() {
389 <                public void run() {
390 <                    try {
391 <                        DelayQueue q = populatedQueue(SIZE);
392 <                        for (int i = 0; i < SIZE; ++i) {
393 <                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
394 <                        }
395 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396 <                    } catch (InterruptedException success){
397 <                    }  
398 <                }});
399 <        t.start();
400 <        try {
401 <           Thread.sleep(SHORT_DELAY_MS);
402 <           t.interrupt();
403 <           t.join();
404 <        }
405 <        catch (InterruptedException ie) {
406 <            fail("Unexpected exception");
462 >    /**
463 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
464 >     */
465 >    public void testTimedPoll() throws InterruptedException {
466 >        DelayQueue q = populatedQueue(SIZE);
467 >        for (int i = 0; i < SIZE; ++i) {
468 >            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
469          }
470 +        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
471      }
472  
473 <    public void testTimedPollWithOffer(){
474 <        final DelayQueue q = new DelayQueue();
475 <        Thread t = new Thread(new Runnable() {
476 <                public void run(){
477 <                    try {
478 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
479 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
480 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
481 <                        threadFail("Should block");
482 <                    } catch (InterruptedException success) { }                
473 >    /**
474 >     * Interrupted timed poll throws InterruptedException instead of
475 >     * returning timeout status
476 >     */
477 >    public void testInterruptedTimedPoll() throws InterruptedException {
478 >        Thread t = new Thread(new CheckedRunnable() {
479 >            public void realRun() throws InterruptedException {
480 >                DelayQueue q = populatedQueue(SIZE);
481 >                for (int i = 0; i < SIZE; ++i) {
482 >                    assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
483                  }
484 <            });
485 <        try {
486 <            t.start();
487 <            Thread.sleep(SMALL_DELAY_MS);
488 <            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489 <            t.interrupt();
490 <            t.join();
491 <        } catch (Exception e){
492 <            fail("Unexpected exception");
493 <        }
494 <    }  
484 >                try {
485 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
486 >                    shouldThrow();
487 >                } catch (InterruptedException success) {}
488 >            }});
489 >
490 >        t.start();
491 >        Thread.sleep(SHORT_DELAY_MS);
492 >        t.interrupt();
493 >        t.join();
494 >    }
495 >
496 >    /**
497 >     *  timed poll before a delayed offer fails; after offer succeeds;
498 >     *  on interruption throws
499 >     */
500 >    public void testTimedPollWithOffer() throws InterruptedException {
501 >        final DelayQueue q = new DelayQueue();
502 >        Thread t = new Thread(new CheckedRunnable() {
503 >            public void realRun() throws InterruptedException {
504 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
505 >                q.poll(LONG_DELAY_MS, MILLISECONDS);
506 >                try {
507 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
508 >                    shouldThrow();
509 >                } catch (InterruptedException success) {}
510 >            }});
511 >
512 >        t.start();
513 >        Thread.sleep(SMALL_DELAY_MS);
514 >        assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
515 >        t.interrupt();
516 >        t.join();
517 >    }
518  
519  
520 <    public void testPeek(){
520 >    /**
521 >     * peek returns next element, or null if empty
522 >     */
523 >    public void testPeek() {
524          DelayQueue q = populatedQueue(SIZE);
525          for (int i = 0; i < SIZE; ++i) {
526              assertEquals(new PDelay(i), ((PDelay)q.peek()));
527              q.poll();
528 <            assertTrue(q.peek() == null ||
529 <                       i != ((PDelay)q.peek()).intValue());
528 >            if (q.isEmpty())
529 >                assertNull(q.peek());
530 >            else
531 >                assertTrue(i != ((PDelay)q.peek()).intValue());
532          }
533 <        assertNull(q.peek());
533 >        assertNull(q.peek());
534      }
535  
536 <    public void testElement(){
536 >    /**
537 >     * element returns next element, or throws NSEE if empty
538 >     */
539 >    public void testElement() {
540          DelayQueue q = populatedQueue(SIZE);
541          for (int i = 0; i < SIZE; ++i) {
542              assertEquals(new PDelay(i), ((PDelay)q.element()));
# Line 450 | Line 544 | public class DelayQueueTest extends JSR1
544          }
545          try {
546              q.element();
547 <            fail("no such element");
548 <        }
455 <        catch (NoSuchElementException success) {}
547 >            shouldThrow();
548 >        } catch (NoSuchElementException success) {}
549      }
550  
551 <    public void testRemove(){
551 >    /**
552 >     * remove removes next element, or throws NSEE if empty
553 >     */
554 >    public void testRemove() {
555          DelayQueue q = populatedQueue(SIZE);
556          for (int i = 0; i < SIZE; ++i) {
557              assertEquals(new PDelay(i), ((PDelay)q.remove()));
558          }
559          try {
560              q.remove();
561 <            fail("remove should throw");
562 <        } catch (NoSuchElementException success){
467 <        }  
561 >            shouldThrow();
562 >        } catch (NoSuchElementException success) {}
563      }
564  
565 <    public void testRemoveElement(){
565 >    /**
566 >     * remove(x) removes x and returns true if present
567 >     */
568 >    public void testRemoveElement() {
569          DelayQueue q = populatedQueue(SIZE);
570          for (int i = 1; i < SIZE; i+=2) {
571              assertTrue(q.remove(new PDelay(i)));
# Line 478 | Line 576 | public class DelayQueueTest extends JSR1
576          }
577          assertTrue(q.isEmpty());
578      }
579 <        
580 <    public void testContains(){
579 >
580 >    /**
581 >     * contains(x) reports true when elements added but not yet removed
582 >     */
583 >    public void testContains() {
584          DelayQueue q = populatedQueue(SIZE);
585          for (int i = 0; i < SIZE; ++i) {
586              assertTrue(q.contains(new PDelay(i)));
# Line 488 | Line 589 | public class DelayQueueTest extends JSR1
589          }
590      }
591  
592 <    public void testClear(){
592 >    /**
593 >     * clear removes all elements
594 >     */
595 >    public void testClear() {
596          DelayQueue q = populatedQueue(SIZE);
597          q.clear();
598          assertTrue(q.isEmpty());
599          assertEquals(0, q.size());
600          assertEquals(NOCAP, q.remainingCapacity());
601 <        q.add(new PDelay(1));
601 >        PDelay x = new PDelay(1);
602 >        q.add(x);
603          assertFalse(q.isEmpty());
604 +        assertTrue(q.contains(x));
605          q.clear();
606          assertTrue(q.isEmpty());
607      }
608  
609 <    public void testContainsAll(){
609 >    /**
610 >     * containsAll(c) is true when c contains a subset of elements
611 >     */
612 >    public void testContainsAll() {
613          DelayQueue q = populatedQueue(SIZE);
614          DelayQueue p = new DelayQueue();
615          for (int i = 0; i < SIZE; ++i) {
# Line 511 | Line 620 | public class DelayQueueTest extends JSR1
620          assertTrue(p.containsAll(q));
621      }
622  
623 <    public void testRetainAll(){
623 >    /**
624 >     * retainAll(c) retains only those elements of c and reports true if changed
625 >     */
626 >    public void testRetainAll() {
627          DelayQueue q = populatedQueue(SIZE);
628          DelayQueue p = populatedQueue(SIZE);
629          for (int i = 0; i < SIZE; ++i) {
# Line 527 | Line 639 | public class DelayQueueTest extends JSR1
639          }
640      }
641  
642 <    public void testRemoveAll(){
642 >    /**
643 >     * removeAll(c) removes only those elements of c and reports true if changed
644 >     */
645 >    public void testRemoveAll() {
646          for (int i = 1; i < SIZE; ++i) {
647              DelayQueue q = populatedQueue(SIZE);
648              DelayQueue p = populatedQueue(i);
# Line 540 | Line 655 | public class DelayQueueTest extends JSR1
655          }
656      }
657  
658 <    public void testToArray(){
658 >    /**
659 >     * toArray contains all elements
660 >     */
661 >    public void testToArray() throws InterruptedException {
662          DelayQueue q = populatedQueue(SIZE);
663 <        Object[] o = q.toArray();
663 >        Object[] o = q.toArray();
664          Arrays.sort(o);
665 <        try {
666 <        for(int i = 0; i < o.length; i++)
549 <            assertEquals(o[i], q.take());
550 <        } catch (InterruptedException e){
551 <            fail("Unexpected exception");
552 <        }    
665 >        for (int i = 0; i < o.length; i++)
666 >            assertEquals(o[i], q.take());
667      }
668  
669 <    public void testToArray2(){
669 >    /**
670 >     * toArray(a) contains all elements
671 >     */
672 >    public void testToArray2() throws InterruptedException {
673          DelayQueue q = populatedQueue(SIZE);
674 <        PDelay[] ints = new PDelay[SIZE];
675 <        ints = (PDelay[])q.toArray(ints);
674 >        PDelay[] ints = new PDelay[SIZE];
675 >        ints = (PDelay[])q.toArray(ints);
676          Arrays.sort(ints);
677 <        try {
678 <            for(int i = 0; i < ints.length; i++)
562 <                assertEquals(ints[i], q.take());
563 <        } catch (InterruptedException e){
564 <            fail("Unexpected exception");
565 <        }    
677 >        for (int i = 0; i < ints.length; i++)
678 >            assertEquals(ints[i], q.take());
679      }
680 <    
681 <    public void testIterator(){
680 >
681 >
682 >    /**
683 >     * toArray(null) throws NPE
684 >     */
685 >    public void testToArray_BadArg() {
686 >        try {
687 >            DelayQueue q = populatedQueue(SIZE);
688 >            Object o[] = q.toArray(null);
689 >            shouldThrow();
690 >        } catch (NullPointerException success) {}
691 >    }
692 >
693 >    /**
694 >     * toArray with incompatible array type throws CCE
695 >     */
696 >    public void testToArray1_BadArg() {
697 >        try {
698 >            DelayQueue q = populatedQueue(SIZE);
699 >            Object o[] = q.toArray(new String[10] );
700 >            shouldThrow();
701 >        } catch (ArrayStoreException success) {}
702 >    }
703 >
704 >    /**
705 >     * iterator iterates through all elements
706 >     */
707 >    public void testIterator() {
708          DelayQueue q = populatedQueue(SIZE);
709          int i = 0;
710 <        Iterator it = q.iterator();
711 <        while(it.hasNext()) {
710 >        Iterator it = q.iterator();
711 >        while (it.hasNext()) {
712              assertTrue(q.contains(it.next()));
713              ++i;
714          }
715          assertEquals(i, SIZE);
716      }
717  
718 +    /**
719 +     * iterator.remove removes current element
720 +     */
721      public void testIteratorRemove () {
580
722          final DelayQueue q = new DelayQueue();
582
723          q.add(new PDelay(2));
724          q.add(new PDelay(1));
725          q.add(new PDelay(3));
586
726          Iterator it = q.iterator();
727          it.next();
728          it.remove();
590
729          it = q.iterator();
730          assertEquals(it.next(), new PDelay(2));
731          assertEquals(it.next(), new PDelay(3));
# Line 595 | Line 733 | public class DelayQueueTest extends JSR1
733      }
734  
735  
736 <    public void testToString(){
736 >    /**
737 >     * toString contains toStrings of elements
738 >     */
739 >    public void testToString() {
740          DelayQueue q = populatedQueue(SIZE);
741          String s = q.toString();
742          for (int i = 0; i < SIZE; ++i) {
743              assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
744          }
745 <    }        
745 >    }
746  
747 +    /**
748 +     * offer transfers elements across Executor tasks
749 +     */
750      public void testPollInExecutor() {
607
751          final DelayQueue q = new DelayQueue();
609
752          ExecutorService executor = Executors.newFixedThreadPool(2);
753 <
754 <        executor.execute(new Runnable() {
613 <            public void run() {
753 >        executor.execute(new CheckedRunnable() {
754 >            public void realRun() throws InterruptedException {
755                  threadAssertNull(q.poll());
756 <                try {
757 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
758 <                    threadAssertTrue(q.isEmpty());
759 <                }
760 <                catch (InterruptedException e) {
761 <                    threadFail("should not be interrupted");
762 <                }
763 <            }
764 <        });
756 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
757 >                threadAssertTrue(q.isEmpty());
758 >            }});
759 >
760 >        executor.execute(new CheckedRunnable() {
761 >            public void realRun() throws InterruptedException {
762 >                Thread.sleep(SHORT_DELAY_MS);
763 >                q.put(new PDelay(1));
764 >            }});
765  
625        executor.execute(new Runnable() {
626            public void run() {
627                try {
628                    Thread.sleep(SHORT_DELAY_MS);
629                    q.put(new PDelay(1));
630                }
631                catch (InterruptedException e) {
632                    threadFail("should not be interrupted");
633                }
634            }
635        });
636        
766          joinPool(executor);
767  
768      }
769  
770 <    static class NanoDelay implements Delayed {
771 <        long trigger;
772 <        NanoDelay(long i) {
773 <            trigger = System.nanoTime() + i;
770 >
771 >    /**
772 >     * Delayed actions do not occur until their delay elapses
773 >     */
774 >    public void testDelay() throws InterruptedException {
775 >        DelayQueue q = new DelayQueue();
776 >        NanoDelay[] elements = new NanoDelay[SIZE];
777 >        for (int i = 0; i < SIZE; ++i) {
778 >            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
779          }
780 <        public int compareTo(Object y) {
781 <            long i = trigger;
648 <            long j = ((NanoDelay)y).trigger;
649 <            if (i < j) return -1;
650 <            if (i > j) return 1;
651 <            return 0;
780 >        for (int i = 0; i < SIZE; ++i) {
781 >            q.add(elements[i]);
782          }
783  
784 <        public int compareTo(NanoDelay y) {
785 <            long i = trigger;
786 <            long j = ((NanoDelay)y).trigger;
787 <            if (i < j) return -1;
788 <            if (i > j) return 1;
789 <            return 0;
784 >        long last = 0;
785 >        for (int i = 0; i < SIZE; ++i) {
786 >            NanoDelay e = (NanoDelay)(q.take());
787 >            long tt = e.getTriggerTime();
788 >            assertTrue(tt <= System.nanoTime());
789 >            if (i != 0)
790 >                assertTrue(tt >= last);
791 >            last = tt;
792          }
793 +    }
794  
795 <        public boolean equals(Object other) {
796 <            return ((NanoDelay)other).trigger == trigger;
797 <        }
798 <        public boolean equals(NanoDelay other) {
799 <            return ((NanoDelay)other).trigger == trigger;
800 <        }
795 >    /**
796 >     * peek of a non-empty queue returns non-null even if not expired
797 >     */
798 >    public void testPeekDelayed() {
799 >        DelayQueue q = new DelayQueue();
800 >        q.add(new NanoDelay(Long.MAX_VALUE));
801 >        assert(q.peek() != null);
802 >    }
803  
669        public long getDelay(TimeUnit unit) {
670            long n = trigger - System.nanoTime();
671            return unit.convert(n, TimeUnit.NANOSECONDS);
672        }
804  
805 <        public long getTriggerTime() {
806 <            return trigger;
807 <        }
805 >    /**
806 >     * poll of a non-empty queue returns null if no expired elements.
807 >     */
808 >    public void testPollDelayed() {
809 >        DelayQueue q = new DelayQueue();
810 >        q.add(new NanoDelay(Long.MAX_VALUE));
811 >        assertNull(q.poll());
812 >    }
813  
814 <        public String toString() {
815 <            return String.valueOf(trigger);
816 <        }
814 >    /**
815 >     * timed poll of a non-empty queue returns null if no expired elements.
816 >     */
817 >    public void testTimedPollDelayed() throws InterruptedException {
818 >        DelayQueue q = new DelayQueue();
819 >        q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
820 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
821      }
822  
823 <    public void testDelay() {
823 >    /**
824 >     * drainTo(null) throws NPE
825 >     */
826 >    public void testDrainToNull() {
827 >        DelayQueue q = populatedQueue(SIZE);
828 >        try {
829 >            q.drainTo(null);
830 >            shouldThrow();
831 >        } catch (NullPointerException success) {}
832 >    }
833 >
834 >    /**
835 >     * drainTo(this) throws IAE
836 >     */
837 >    public void testDrainToSelf() {
838 >        DelayQueue q = populatedQueue(SIZE);
839 >        try {
840 >            q.drainTo(q);
841 >            shouldThrow();
842 >        } catch (IllegalArgumentException success) {}
843 >    }
844 >
845 >    /**
846 >     * drainTo(c) empties queue into another collection c
847 >     */
848 >    public void testDrainTo() {
849          DelayQueue q = new DelayQueue();
850 <        NanoDelay[] elements = new NanoDelay[SIZE];
850 >        PDelay[] elems = new PDelay[SIZE];
851          for (int i = 0; i < SIZE; ++i) {
852 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
853 <        }
689 <        for (int i = 0; i < SIZE; ++i) {
690 <            q.add(elements[i]);
852 >            elems[i] = new PDelay(i);
853 >            q.add(elems[i]);
854          }
855 +        ArrayList l = new ArrayList();
856 +        q.drainTo(l);
857 +        assertEquals(q.size(), 0);
858 +        for (int i = 0; i < SIZE; ++i)
859 +            assertEquals(l.get(i), elems[i]);
860 +        q.add(elems[0]);
861 +        q.add(elems[1]);
862 +        assertFalse(q.isEmpty());
863 +        assertTrue(q.contains(elems[0]));
864 +        assertTrue(q.contains(elems[1]));
865 +        l.clear();
866 +        q.drainTo(l);
867 +        assertEquals(q.size(), 0);
868 +        assertEquals(l.size(), 2);
869 +        for (int i = 0; i < 2; ++i)
870 +            assertEquals(l.get(i), elems[i]);
871 +    }
872 +
873 +    /**
874 +     * drainTo empties queue
875 +     */
876 +    public void testDrainToWithActivePut() throws InterruptedException {
877 +        final DelayQueue q = populatedQueue(SIZE);
878 +        Thread t = new Thread(new CheckedRunnable() {
879 +            public void realRun() {
880 +                q.put(new PDelay(SIZE+1));
881 +            }});
882  
883 +        t.start();
884 +        ArrayList l = new ArrayList();
885 +        q.drainTo(l);
886 +        assertTrue(l.size() >= SIZE);
887 +        t.join();
888 +        assertTrue(q.size() + l.size() >= SIZE);
889 +    }
890 +
891 +    /**
892 +     * drainTo(null, n) throws NPE
893 +     */
894 +    public void testDrainToNullN() {
895 +        DelayQueue q = populatedQueue(SIZE);
896          try {
897 <            long last = 0;
898 <            for (int i = 0; i < SIZE; ++i) {
899 <                NanoDelay e = (NanoDelay)(q.take());
900 <                long tt = e.getTriggerTime();
901 <                assertTrue(tt <= System.nanoTime());
902 <                if (i != 0)
903 <                    assertTrue(tt >= last);
904 <                last = tt;
905 <            }
906 <        }
907 <        catch(InterruptedException ie) {
908 <            fail("Unexpected Exception");
897 >            q.drainTo(null, 0);
898 >            shouldThrow();
899 >        } catch (NullPointerException success) {}
900 >    }
901 >
902 >    /**
903 >     * drainTo(this, n) throws IAE
904 >     */
905 >    public void testDrainToSelfN() {
906 >        DelayQueue q = populatedQueue(SIZE);
907 >        try {
908 >            q.drainTo(q, 0);
909 >            shouldThrow();
910 >        } catch (IllegalArgumentException success) {}
911 >    }
912 >
913 >    /**
914 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
915 >     */
916 >    public void testDrainToN() {
917 >        for (int i = 0; i < SIZE + 2; ++i) {
918 >            DelayQueue q = populatedQueue(SIZE);
919 >            ArrayList l = new ArrayList();
920 >            q.drainTo(l, i);
921 >            int k = (i < SIZE)? i : SIZE;
922 >            assertEquals(q.size(), SIZE-k);
923 >            assertEquals(l.size(), k);
924          }
925      }
926  
927 +
928   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines