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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.50 by jsr166, Fri May 27 20:07:24 2011 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/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 TestCase {
13 <
14 <    private static final int N = 10;
15 <    private static final long SHORT_DELAY_MS = 100;
16 <    private static final long MEDIUM_DELAY_MS = 1000;
17 <    private static final long LONG_DELAY_MS = 10000;
18 <    private static final int NOCAP = Integer.MAX_VALUE;
19 <
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 <    // Most Q/BQ tests use Pseudodelays, where delays are all elapsed
29 <    // (so, no blocking solely for delays) but are still ordered
23 >    private static final int NOCAP = Integer.MAX_VALUE;
24  
25 <    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;
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(PDelay y) {
42 <            int i = pseudodelay;
44 <            int j = ((PDelay)y).pseudodelay;
45 <            if (i < j) return -1;
46 <            if (i > j) return 1;
47 <            return 0;
41 >        public int compareTo(Delayed y) {
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  
57
52          public long getDelay(TimeUnit ignore) {
53              return pseudodelay;
54          }
# Line 67 | Line 61 | public class DelayQueueTest extends Test
61          }
62      }
63  
64 +    /**
65 +     * Delayed implementation that actually delays
66 +     */
67 +    static class NanoDelay implements Delayed {
68 +        long trigger;
69 +        NanoDelay(long i) {
70 +            trigger = System.nanoTime() + i;
71 +        }
72 +        public int compareTo(NanoDelay y) {
73 +            long i = 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 +            return compareTo((NanoDelay)y);
82 +        }
83 +
84 +        public boolean equals(Object other) {
85 +            return equals((NanoDelay)other);
86 +        }
87 +        public boolean equals(NanoDelay other) {
88 +            return other.trigger == trigger;
89 +        }
90 +
91 +        public long getDelay(TimeUnit unit) {
92 +            long n = trigger - System.nanoTime();
93 +            return unit.convert(n, TimeUnit.NANOSECONDS);
94 +        }
95  
96 +        public long getTriggerTime() {
97 +            return trigger;
98 +        }
99  
100 +        public String toString() {
101 +            return String.valueOf(trigger);
102 +        }
103 +    }
104  
105      /**
106       * Create a queue of given size containing consecutive
107       * PDelays 0 ... n.
108       */
109 <    private DelayQueue fullQueue(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 <
122 <    public void testConstructor1(){
121 >
122 >    /**
123 >     * A new queue has unbounded capacity
124 >     */
125 >    public void testConstructor1() {
126          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
127      }
128  
129 <    public void testConstructor3(){
130 <
129 >    /**
130 >     * Initializing from null Collection throws NPE
131 >     */
132 >    public void testConstructor3() {
133          try {
134              DelayQueue q = new DelayQueue(null);
135 <            fail("Cannot make from null collection");
136 <        }
100 <        catch (NullPointerException success) {}
135 >            shouldThrow();
136 >        } catch (NullPointerException success) {}
137      }
138  
139 <    public void testConstructor4(){
139 >    /**
140 >     * Initializing from Collection of null elements throws NPE
141 >     */
142 >    public void testConstructor4() {
143          try {
144 <            PDelay[] ints = new PDelay[N];
144 >            PDelay[] ints = new PDelay[SIZE];
145              DelayQueue q = new DelayQueue(Arrays.asList(ints));
146 <            fail("Cannot make with null elements");
147 <        }
109 <        catch (NullPointerException success) {}
146 >            shouldThrow();
147 >        } catch (NullPointerException success) {}
148      }
149  
150 <    public void testConstructor5(){
150 >    /**
151 >     * Initializing from Collection with some null elements throws NPE
152 >     */
153 >    public void testConstructor5() {
154          try {
155 <            PDelay[] ints = new PDelay[N];
156 <            for (int i = 0; i < N-1; ++i)
155 >            PDelay[] ints = new PDelay[SIZE];
156 >            for (int i = 0; i < SIZE-1; ++i)
157                  ints[i] = new PDelay(i);
158              DelayQueue q = new DelayQueue(Arrays.asList(ints));
159 <            fail("Cannot make with null elements");
160 <        }
120 <        catch (NullPointerException success) {}
159 >            shouldThrow();
160 >        } catch (NullPointerException success) {}
161      }
162  
163 <    public void testConstructor6(){
164 <        try {
165 <            PDelay[] ints = new PDelay[N];
166 <            for (int i = 0; i < N; ++i)
167 <                ints[i] = new PDelay(i);
168 <            DelayQueue q = new DelayQueue(Arrays.asList(ints));
169 <            for (int i = 0; i < N; ++i)
170 <                assertEquals(ints[i], q.poll());
171 <        }
172 <        finally {}
163 >    /**
164 >     * Queue contains all elements of collection used to initialize
165 >     */
166 >    public void testConstructor6() {
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 +    /**
176 +     * isEmpty is true before add, false after
177 +     */
178      public void testEmpty() {
179          DelayQueue q = new DelayQueue();
180          assertTrue(q.isEmpty());
# Line 144 | Line 187 | public class DelayQueueTest extends Test
187          assertTrue(q.isEmpty());
188      }
189  
190 <    public void testRemainingCapacity(){
191 <        DelayQueue q = fullQueue(N);
192 <        for (int i = 0; i < N; ++i) {
190 >    /**
191 >     * remainingCapacity does not change when elements added or removed,
192 >     * but size does
193 >     */
194 >    public void testRemainingCapacity() {
195 >        DelayQueue q = populatedQueue(SIZE);
196 >        for (int i = 0; i < SIZE; ++i) {
197              assertEquals(NOCAP, q.remainingCapacity());
198 <            assertEquals(N-i, q.size());
198 >            assertEquals(SIZE-i, q.size());
199              q.remove();
200          }
201 <        for (int i = 0; i < N; ++i) {
201 >        for (int i = 0; i < SIZE; ++i) {
202              assertEquals(NOCAP, q.remainingCapacity());
203              assertEquals(i, q.size());
204              q.add(new PDelay(i));
205          }
206      }
207  
208 <    public void testOfferNull(){
209 <        try {
208 >    /**
209 >     * offer(null) throws NPE
210 >     */
211 >    public void testOfferNull() {
212 >        try {
213              DelayQueue q = new DelayQueue();
214              q.offer(null);
215 <            fail("should throw NPE");
216 <        } catch (NullPointerException success) { }  
215 >            shouldThrow();
216 >        } catch (NullPointerException success) {}
217      }
218  
219 +    /**
220 +     * add(null) throws NPE
221 +     */
222 +    public void testAddNull() {
223 +        try {
224 +            DelayQueue q = new DelayQueue();
225 +            q.add(null);
226 +            shouldThrow();
227 +        } catch (NullPointerException success) {}
228 +    }
229 +
230 +    /**
231 +     * offer non-null succeeds
232 +     */
233      public void testOffer() {
234          DelayQueue q = new DelayQueue();
235          assertTrue(q.offer(new PDelay(0)));
236          assertTrue(q.offer(new PDelay(1)));
237      }
238  
239 <    public void testAdd(){
239 >    /**
240 >     * add succeeds
241 >     */
242 >    public void testAdd() {
243          DelayQueue q = new DelayQueue();
244 <        for (int i = 0; i < N; ++i) {
244 >        for (int i = 0; i < SIZE; ++i) {
245              assertEquals(i, q.size());
246              assertTrue(q.add(new PDelay(i)));
247          }
248      }
249  
250 <    public void testAddAll1(){
250 >    /**
251 >     * addAll(null) throws NPE
252 >     */
253 >    public void testAddAll1() {
254          try {
255              DelayQueue q = new DelayQueue();
256              q.addAll(null);
257 <            fail("Cannot add null collection");
258 <        }
259 <        catch (NullPointerException success) {}
257 >            shouldThrow();
258 >        } catch (NullPointerException success) {}
259 >    }
260 >
261 >    /**
262 >     * addAll(this) throws IAE
263 >     */
264 >    public void testAddAllSelf() {
265 >        try {
266 >            DelayQueue q = populatedQueue(SIZE);
267 >            q.addAll(q);
268 >            shouldThrow();
269 >        } catch (IllegalArgumentException success) {}
270      }
271 <    public void testAddAll2(){
271 >
272 >    /**
273 >     * addAll of a collection with null elements throws NPE
274 >     */
275 >    public void testAddAll2() {
276          try {
277              DelayQueue q = new DelayQueue();
278 <            PDelay[] ints = new PDelay[N];
278 >            PDelay[] ints = new PDelay[SIZE];
279              q.addAll(Arrays.asList(ints));
280 <            fail("Cannot add null elements");
281 <        }
198 <        catch (NullPointerException success) {}
280 >            shouldThrow();
281 >        } catch (NullPointerException success) {}
282      }
283 <    public void testAddAll3(){
283 >
284 >    /**
285 >     * addAll of a collection with any null elements throws NPE after
286 >     * possibly adding some elements
287 >     */
288 >    public void testAddAll3() {
289          try {
290              DelayQueue q = new DelayQueue();
291 <            PDelay[] ints = new PDelay[N];
292 <            for (int i = 0; i < N-1; ++i)
291 >            PDelay[] ints = new PDelay[SIZE];
292 >            for (int i = 0; i < SIZE-1; ++i)
293                  ints[i] = new PDelay(i);
294              q.addAll(Arrays.asList(ints));
295 <            fail("Cannot add null elements");
296 <        }
209 <        catch (NullPointerException success) {}
295 >            shouldThrow();
296 >        } catch (NullPointerException success) {}
297      }
298  
299 <    public void testAddAll5(){
300 <        try {
301 <            PDelay[] empty = new PDelay[0];
302 <            PDelay[] ints = new PDelay[N];
303 <            for (int i = N-1; i >= 0; --i)
304 <                ints[i] = new PDelay(i);
305 <            DelayQueue q = new DelayQueue();
306 <            assertFalse(q.addAll(Arrays.asList(empty)));
307 <            assertTrue(q.addAll(Arrays.asList(ints)));
308 <            for (int i = 0; i < N; ++i)
309 <                assertEquals(ints[i], q.poll());
310 <        }
311 <        finally {}
299 >    /**
300 >     * Queue contains all elements of successful addAll
301 >     */
302 >    public void testAddAll5() {
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 <     public void testPutNull() {
315 <        try {
314 >    /**
315 >     * put(null) throws NPE
316 >     */
317 >    public void testPutNull() {
318 >        try {
319              DelayQueue q = new DelayQueue();
320              q.put(null);
321 <            fail("put should throw NPE");
322 <        }
233 <        catch (NullPointerException success){
234 <        }  
235 <     }
236 <
237 <     public void testPut() {
238 <         try {
239 <             DelayQueue q = new DelayQueue();
240 <             for (int i = 0; i < N; ++i) {
241 <                 PDelay I = new PDelay(i);
242 <                 q.put(I);
243 <                 assertTrue(q.contains(I));
244 <             }
245 <             assertEquals(N, q.size());
246 <         }
247 <         finally {
248 <        }
321 >            shouldThrow();
322 >        } catch (NullPointerException success) {}
323      }
324  
325 <    public void testPutWithTake() {
326 <        final DelayQueue q = new DelayQueue();
327 <        Thread t = new Thread(new Runnable() {
328 <                public void run(){
329 <                    int added = 0;
330 <                    try {
331 <                        q.put(new PDelay(0));
332 <                        ++added;
333 <                        q.put(new PDelay(0));
260 <                        ++added;
261 <                        q.put(new PDelay(0));
262 <                        ++added;
263 <                        q.put(new PDelay(0));
264 <                        ++added;
265 <                        assertTrue(added == 4);
266 <                    } finally {
267 <                    }
268 <                }
269 <            });
270 <        try {
271 <            t.start();
272 <            Thread.sleep(SHORT_DELAY_MS);
273 <            q.take();
274 <            t.interrupt();
275 <            t.join();
276 <        } catch (Exception e){
277 <            fail("Unexpected exception");
325 >    /**
326 >     * all elements successfully put are contained
327 >     */
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 <    public void testTimedOffer() {
338 >    /**
339 >     * put doesn't block waiting for take
340 >     */
341 >    public void testPutWithTake() throws InterruptedException {
342          final DelayQueue q = new DelayQueue();
343 <        Thread t = new Thread(new Runnable() {
344 <                public void run(){
345 <                    try {
346 <                        q.put(new PDelay(0));
347 <                        q.put(new PDelay(0));
348 <                        assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
349 <                        assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
290 <                    } finally { }
291 <                }
292 <            });
293 <        
294 <        try {
295 <            t.start();
296 <            Thread.sleep(SHORT_DELAY_MS);
297 <            t.interrupt();
298 <            t.join();
299 <        } catch (Exception e){
300 <            fail("Unexpected exception");
301 <        }
302 <    }
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 <    public void testTake(){
352 <        try {
306 <            DelayQueue q = fullQueue(N);
307 <            for (int i = 0; i < N; ++i) {
308 <                assertEquals(new PDelay(i), ((PDelay)q.take()));
309 <            }
310 <        } catch (InterruptedException e){
311 <            fail("Unexpected exception");
312 <        }  
351 >        awaitTermination(t);
352 >        assertEquals(4, q.size());
353      }
354  
355 <    public void testTakeFromEmpty() {
355 >    /**
356 >     * timed offer does not time out
357 >     */
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.take();
364 <                        fail("Should block");
365 <                    } catch (InterruptedException success){ }                
366 <                }
367 <            });
368 <        try {
326 <            t.start();
327 <            Thread.sleep(SHORT_DELAY_MS);
328 <            t.interrupt();
329 <            t.join();
330 <        } catch (Exception e){
331 <            fail("Unexpected exception");
332 <        }
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 <    public void testBlockingTake(){
372 <        Thread t = new Thread(new Runnable() {
373 <                public void run() {
374 <                    try {
375 <                        DelayQueue q = fullQueue(N);
376 <                        for (int i = 0; i < N; ++i) {
377 <                            assertEquals(new PDelay(i), ((PDelay)q.take()));
342 <                        }
343 <                        q.take();
344 <                        fail("take should block");
345 <                    } catch (InterruptedException success){
346 <                    }  
347 <                }});
348 <        t.start();
349 <        try {
350 <           Thread.sleep(SHORT_DELAY_MS);
351 <           t.interrupt();
352 <           t.join();
353 <        }
354 <        catch (InterruptedException ie) {
355 <            fail("Unexpected exception");
371 >    /**
372 >     * take retrieves elements in priority order
373 >     */
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 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() 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 <    public void testPoll(){
424 <        DelayQueue q = fullQueue(N);
425 <        for (int i = 0; i < N; ++i) {
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
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 = fullQueue(N);
451 <            for (int i = 0; i < N; ++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");
377 <        }  
448 >    /**
449 >     * timed poll 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 = fullQueue(N);
462 <            for (int i = 0; i < N; ++i) {
463 <                assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
464 <            }
465 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
387 <        } catch (InterruptedException e){
388 <            fail("Unexpected exception");
389 <        }  
390 <    }
391 <
392 <    public void testInterruptedTimedPoll(){
393 <        Thread t = new Thread(new Runnable() {
394 <                public void run() {
395 <                    try {
396 <                        DelayQueue q = fullQueue(N);
397 <                        for (int i = 0; i < N; ++i) {
398 <                            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
399 <                        }
400 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
401 <                    } catch (InterruptedException success){
402 <                    }  
403 <                }});
404 <        t.start();
405 <        try {
406 <           Thread.sleep(SHORT_DELAY_MS);
407 <           t.interrupt();
408 <           t.join();
409 <        }
410 <        catch (InterruptedException ie) {
411 <            fail("Unexpected exception");
459 >    /**
460 >     * timed poll 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 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
476 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
477 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
478 <                        fail("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 >        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 <        try {
484 <            t.start();
485 <            Thread.sleep(SHORT_DELAY_MS * 2);
486 <            assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
487 <            t.interrupt();
488 <            t.join();
489 <        } catch (Exception e){
490 <            fail("Unexpected exception");
491 <        }
492 <    }  
493 <
494 <
495 <    public void testPeek(){
496 <        DelayQueue q = fullQueue(N);
497 <        for (int i = 0; i < N; ++i) {
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
507 >     */
508 >    public void testTimedPollWithOffer() throws InterruptedException {
509 >        final DelayQueue q = new DelayQueue();
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
547 >     */
548 >    public void testPeek() {
549 >        DelayQueue q = populatedQueue(SIZE);
550 >        for (int i = 0; i < SIZE; ++i) {
551              assertEquals(new PDelay(i), ((PDelay)q.peek()));
552 <            q.poll();
553 <            assertTrue(q.peek() == null ||
554 <                       i != ((PDelay)q.peek()).intValue());
552 >            assertEquals(new PDelay(i), ((PDelay)q.poll()));
553 >            if (q.isEmpty())
554 >                assertNull(q.peek());
555 >            else
556 >                assertFalse(new PDelay(i).equals(q.peek()));
557          }
558 <        assertNull(q.peek());
558 >        assertNull(q.peek());
559      }
560  
561 <    public void testElement(){
562 <        DelayQueue q = fullQueue(N);
563 <        for (int i = 0; i < N; ++i) {
561 >    /**
562 >     * element returns next element, or throws NSEE if empty
563 >     */
564 >    public void testElement() {
565 >        DelayQueue q = populatedQueue(SIZE);
566 >        for (int i = 0; i < SIZE; ++i) {
567              assertEquals(new PDelay(i), ((PDelay)q.element()));
568              q.poll();
569          }
570          try {
571              q.element();
572 <            fail("no such element");
573 <        }
460 <        catch (NoSuchElementException success) {}
572 >            shouldThrow();
573 >        } catch (NoSuchElementException success) {}
574      }
575  
576 <    public void testRemove(){
577 <        DelayQueue q = fullQueue(N);
578 <        for (int i = 0; i < N; ++i) {
576 >    /**
577 >     * remove removes next element, or throws NSEE if empty
578 >     */
579 >    public void testRemove() {
580 >        DelayQueue q = populatedQueue(SIZE);
581 >        for (int i = 0; i < SIZE; ++i) {
582              assertEquals(new PDelay(i), ((PDelay)q.remove()));
583          }
584          try {
585              q.remove();
586 <            fail("remove should throw");
587 <        } catch (NoSuchElementException success){
472 <        }  
586 >            shouldThrow();
587 >        } catch (NoSuchElementException success) {}
588      }
589  
590 <    public void testRemoveElement(){
591 <        DelayQueue q = fullQueue(N);
592 <        for (int i = 1; i < N; i+=2) {
590 >    /**
591 >     * remove(x) removes x and returns true if present
592 >     */
593 >    public void testRemoveElement() {
594 >        DelayQueue q = populatedQueue(SIZE);
595 >        for (int i = 1; i < SIZE; i+=2) {
596              assertTrue(q.remove(new PDelay(i)));
597          }
598 <        for (int i = 0; i < N; i+=2) {
598 >        for (int i = 0; i < SIZE; i+=2) {
599              assertTrue(q.remove(new PDelay(i)));
600              assertFalse(q.remove(new PDelay(i+1)));
601          }
602 <        assert(q.isEmpty());
602 >        assertTrue(q.isEmpty());
603      }
604 <        
605 <    public void testContains(){
606 <        DelayQueue q = fullQueue(N);
607 <        for (int i = 0; i < N; ++i) {
604 >
605 >    /**
606 >     * contains(x) reports true when elements added but not yet removed
607 >     */
608 >    public void testContains() {
609 >        DelayQueue q = populatedQueue(SIZE);
610 >        for (int i = 0; i < SIZE; ++i) {
611              assertTrue(q.contains(new PDelay(i)));
612              q.poll();
613              assertFalse(q.contains(new PDelay(i)));
614          }
615      }
616  
617 <    public void testClear(){
618 <        DelayQueue q = fullQueue(N);
617 >    /**
618 >     * clear removes all elements
619 >     */
620 >    public void testClear() {
621 >        DelayQueue q = populatedQueue(SIZE);
622          q.clear();
623          assertTrue(q.isEmpty());
624          assertEquals(0, q.size());
625          assertEquals(NOCAP, q.remainingCapacity());
626 <        q.add(new PDelay(1));
626 >        PDelay x = new PDelay(1);
627 >        q.add(x);
628          assertFalse(q.isEmpty());
629 +        assertTrue(q.contains(x));
630          q.clear();
631          assertTrue(q.isEmpty());
632      }
633  
634 <    public void testContainsAll(){
635 <        DelayQueue q = fullQueue(N);
634 >    /**
635 >     * containsAll(c) is true when c contains a subset of elements
636 >     */
637 >    public void testContainsAll() {
638 >        DelayQueue q = populatedQueue(SIZE);
639          DelayQueue p = new DelayQueue();
640 <        for (int i = 0; i < N; ++i) {
640 >        for (int i = 0; i < SIZE; ++i) {
641              assertTrue(q.containsAll(p));
642              assertFalse(p.containsAll(q));
643              p.add(new PDelay(i));
# Line 516 | Line 645 | public class DelayQueueTest extends Test
645          assertTrue(p.containsAll(q));
646      }
647  
648 <    public void testRetainAll(){
649 <        DelayQueue q = fullQueue(N);
650 <        DelayQueue p = fullQueue(N);
651 <        for (int i = 0; i < N; ++i) {
648 >    /**
649 >     * retainAll(c) retains only those elements of c and reports true if changed
650 >     */
651 >    public void testRetainAll() {
652 >        DelayQueue q = populatedQueue(SIZE);
653 >        DelayQueue p = populatedQueue(SIZE);
654 >        for (int i = 0; i < SIZE; ++i) {
655              boolean changed = q.retainAll(p);
656              if (i == 0)
657                  assertFalse(changed);
# Line 527 | Line 659 | public class DelayQueueTest extends Test
659                  assertTrue(changed);
660  
661              assertTrue(q.containsAll(p));
662 <            assertEquals(N-i, q.size());
662 >            assertEquals(SIZE-i, q.size());
663              p.remove();
664          }
665      }
666  
667 <    public void testRemoveAll(){
668 <        for (int i = 1; i < N; ++i) {
669 <            DelayQueue q = fullQueue(N);
670 <            DelayQueue p = fullQueue(i);
667 >    /**
668 >     * removeAll(c) removes only those elements of c and reports true if changed
669 >     */
670 >    public void testRemoveAll() {
671 >        for (int i = 1; i < SIZE; ++i) {
672 >            DelayQueue q = populatedQueue(SIZE);
673 >            DelayQueue p = populatedQueue(i);
674              assertTrue(q.removeAll(p));
675 <            assertEquals(N-i, q.size());
675 >            assertEquals(SIZE-i, q.size());
676              for (int j = 0; j < i; ++j) {
677                  PDelay I = (PDelay)(p.remove());
678                  assertFalse(q.contains(I));
# Line 545 | Line 680 | public class DelayQueueTest extends Test
680          }
681      }
682  
683 <    /*
684 <    public void testEqualsAndHashCode(){
685 <        DelayQueue q1 = fullQueue(N);
686 <        DelayQueue q2 = fullQueue(N);
687 <        assertTrue(q1.equals(q2));
688 <        assertTrue(q2.equals(q1));
554 <        assertEquals(q1.hashCode(), q2.hashCode());
555 <        q1.remove();
556 <        assertFalse(q1.equals(q2));
557 <        assertFalse(q2.equals(q1));
558 <        assertFalse(q1.hashCode() == q2.hashCode());
559 <        q2.remove();
560 <        assertTrue(q1.equals(q2));
561 <        assertTrue(q2.equals(q1));
562 <        assertEquals(q1.hashCode(), q2.hashCode());
563 <    }
564 <    */
565 <
566 <    public void testToArray(){
567 <        DelayQueue q = fullQueue(N);
568 <        Object[] o = q.toArray();
683 >    /**
684 >     * toArray contains all elements
685 >     */
686 >    public void testToArray() throws InterruptedException {
687 >        DelayQueue q = populatedQueue(SIZE);
688 >        Object[] o = q.toArray();
689          Arrays.sort(o);
690 <        try {
691 <        for(int i = 0; i < o.length; i++)
572 <            assertEquals(o[i], q.take());
573 <        } catch (InterruptedException e){
574 <            fail("Unexpected exception");
575 <        }    
690 >        for (int i = 0; i < o.length; i++)
691 >            assertSame(o[i], q.take());
692      }
693  
694 <    public void testToArray2(){
695 <        DelayQueue q = fullQueue(N);
696 <        PDelay[] ints = new PDelay[N];
697 <        ints = (PDelay[])q.toArray(ints);
694 >    /**
695 >     * toArray(a) contains all elements
696 >     */
697 >    public void testToArray2() {
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++)
705 <                assertEquals(ints[i], q.take());
706 <        } catch (InterruptedException e){
707 <            fail("Unexpected exception");
708 <        }    
709 <    }
710 <    
711 <    public void testIterator(){
712 <        DelayQueue q = fullQueue(N);
703 >        for (int i = 0; i < ints.length; i++)
704 >            assertSame(ints[i], q.remove());
705 >    }
706 >
707 >    /**
708 >     * toArray(null) throws NullPointerException
709 >     */
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(incompatible array type) throws ArrayStoreException
720 >     */
721 >    public void testToArray1_BadArg() {
722 >        DelayQueue q = populatedQueue(SIZE);
723 >        try {
724 >            q.toArray(new String[10]);
725 >            shouldThrow();
726 >        } catch (ArrayStoreException success) {}
727 >    }
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          }
740 <        assertEquals(i, N);
740 >        assertEquals(i, SIZE);
741      }
742  
743 <    public void testIteratorRemove () {
744 <
743 >    /**
744 >     * iterator.remove removes current element
745 >     */
746 >    public void testIteratorRemove() {
747          final DelayQueue q = new DelayQueue();
605
748          q.add(new PDelay(2));
749          q.add(new PDelay(1));
750          q.add(new PDelay(3));
609
751          Iterator it = q.iterator();
752          it.next();
753          it.remove();
613
754          it = q.iterator();
755          assertEquals(it.next(), new PDelay(2));
756          assertEquals(it.next(), new PDelay(3));
757          assertFalse(it.hasNext());
758      }
759  
760 <
761 <    public void testToString(){
762 <        DelayQueue q = fullQueue(N);
760 >    /**
761 >     * toString contains toStrings of elements
762 >     */
763 >    public void testToString() {
764 >        DelayQueue q = populatedQueue(SIZE);
765          String s = q.toString();
766 <        for (int i = 0; i < N; ++i) {
767 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
766 >        for (int i = 0; i < SIZE; ++i) {
767 >            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
768          }
769 <    }        
769 >    }
770  
771 +    /**
772 +     * timed poll transfers elements across Executor tasks
773 +     */
774      public void testPollInExecutor() {
630
775          final DelayQueue q = new DelayQueue();
776 <
776 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
777          ExecutorService executor = Executors.newFixedThreadPool(2);
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  
792 <        executor.execute(new Runnable() {
636 <            public void run() {
637 <                assertNull("poll should fail", q.poll());
638 <                try {
639 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
640 <                    assertTrue(q.isEmpty());
641 <                }
642 <                catch (InterruptedException e) {
643 <                    fail("should not be interrupted");
644 <                }
645 <            }
646 <        });
647 <
648 <        executor.execute(new Runnable() {
649 <            public void run() {
650 <                try {
651 <                    Thread.sleep(MEDIUM_DELAY_MS);
652 <                    q.put(new PDelay(1));
653 <                }
654 <                catch (InterruptedException e) {
655 <                    fail("should not be interrupted");
656 <                }
657 <            }
658 <        });
659 <        
660 <        executor.shutdown();
661 <
792 >        joinPool(executor);
793      }
794  
795 <    static class NanoDelay implements Delayed {
796 <        long trigger;
797 <        NanoDelay(long i) {
798 <            trigger = System.nanoTime() + i;
799 <        }
800 <        public int compareTo(Object y) {
801 <            long i = trigger;
802 <            long j = ((NanoDelay)y).trigger;
803 <            if (i < j) return -1;
804 <            if (i > j) return 1;
805 <            return 0;
795 >    /**
796 >     * Delayed actions do not occur until their delay elapses
797 >     */
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 <        public int compareTo(NanoDelay y) {
816 <            long i = trigger;
817 <            long j = ((NanoDelay)y).trigger;
818 <            if (i < j) return -1;
819 <            if (i > j) return 1;
820 <            return 0;
821 <        }
815 >    /**
816 >     * peek of a non-empty queue returns non-null even if not expired
817 >     */
818 >    public void testPeekDelayed() {
819 >        DelayQueue q = new DelayQueue();
820 >        q.add(new NanoDelay(Long.MAX_VALUE));
821 >        assertNotNull(q.peek());
822 >    }
823  
824 <        public boolean equals(Object other) {
825 <            return ((NanoDelay)other).trigger == trigger;
826 <        }
827 <        public boolean equals(NanoDelay other) {
828 <            return ((NanoDelay)other).trigger == trigger;
829 <        }
824 >    /**
825 >     * poll of a non-empty queue returns null if no expired elements.
826 >     */
827 >    public void testPollDelayed() {
828 >        DelayQueue q = new DelayQueue();
829 >        q.add(new NanoDelay(Long.MAX_VALUE));
830 >        assertNull(q.poll());
831 >    }
832  
833 <        public long getDelay(TimeUnit unit) {
834 <            long n = trigger - System.nanoTime();
835 <            return unit.convert(n, TimeUnit.NANOSECONDS);
836 <        }
833 >    /**
834 >     * timed poll of a non-empty queue returns null if no expired elements.
835 >     */
836 >    public void testTimedPollDelayed() throws InterruptedException {
837 >        DelayQueue q = new DelayQueue();
838 >        q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
839 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
840 >    }
841  
842 <        public long getTriggerTime() {
843 <            return trigger;
844 <        }
842 >    /**
843 >     * drainTo(null) throws NPE
844 >     */
845 >    public void testDrainToNull() {
846 >        DelayQueue q = populatedQueue(SIZE);
847 >        try {
848 >            q.drainTo(null);
849 >            shouldThrow();
850 >        } catch (NullPointerException success) {}
851 >    }
852  
853 <        public String toString() {
854 <            return String.valueOf(trigger);
855 <        }
853 >    /**
854 >     * drainTo(this) throws IAE
855 >     */
856 >    public void testDrainToSelf() {
857 >        DelayQueue q = populatedQueue(SIZE);
858 >        try {
859 >            q.drainTo(q);
860 >            shouldThrow();
861 >        } catch (IllegalArgumentException success) {}
862      }
863  
864 <    public void testDelay() {
864 >    /**
865 >     * drainTo(c) empties queue into another collection c
866 >     */
867 >    public void testDrainTo() {
868          DelayQueue q = new DelayQueue();
869 <        NanoDelay[] elements = new NanoDelay[N];
870 <        for (int i = 0; i < N; ++i) {
871 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (N - i));
872 <        }
873 <        for (int i = 0; i < N; ++i) {
874 <            q.add(elements[i]);
875 <        }
869 >        PDelay[] elems = new PDelay[SIZE];
870 >        for (int i = 0; i < SIZE; ++i) {
871 >            elems[i] = new PDelay(i);
872 >            q.add(elems[i]);
873 >        }
874 >        ArrayList l = new ArrayList();
875 >        q.drainTo(l);
876 >        assertEquals(q.size(), 0);
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]);
881 >        assertFalse(q.isEmpty());
882 >        assertTrue(q.contains(elems[0]));
883 >        assertTrue(q.contains(elems[1]));
884 >        l.clear();
885 >        q.drainTo(l);
886 >        assertEquals(q.size(), 0);
887 >        assertEquals(l.size(), 2);
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() throws InterruptedException {
896 +        final DelayQueue q = populatedQueue(SIZE);
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 +     */
913 +    public void testDrainToNullN() {
914 +        DelayQueue q = populatedQueue(SIZE);
915          try {
916 <            long last = 0;
917 <            for (int i = 0; i < N; ++i) {
918 <                NanoDelay e = (NanoDelay)(q.take());
919 <                long tt = e.getTriggerTime();
920 <                assertTrue(tt <= System.nanoTime());
921 <                if (i != 0)
922 <                    assertTrue(tt >= last);
923 <                last = tt;
924 <            }
925 <        }
926 <        catch(InterruptedException ie) {
927 <            fail("Unexpected Exception");
916 >            q.drainTo(null, 0);
917 >            shouldThrow();
918 >        } catch (NullPointerException success) {}
919 >    }
920 >
921 >    /**
922 >     * drainTo(this, n) throws IAE
923 >     */
924 >    public void testDrainToSelfN() {
925 >        DelayQueue q = populatedQueue(SIZE);
926 >        try {
927 >            q.drainTo(q, 0);
928 >            shouldThrow();
929 >        } catch (IllegalArgumentException success) {}
930 >    }
931 >
932 >    /**
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;
941 >            assertEquals(q.size(), SIZE-k);
942 >            assertEquals(l.size(), k);
943          }
944      }
945  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines