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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines