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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines