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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC

# Line 9 | Line 9 | import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11  
12 < 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 <
12 > public class DelayQueueTest extends JSR166TestCase {
13      public static void main(String[] args) {
14          junit.textui.TestRunner.run (suite());  
15      }
# Line 25 | Line 18 | public class DelayQueueTest extends Test
18          return new TestSuite(DelayQueueTest.class);
19      }
20  
21 <    // Most Q/BQ tests use Pseudodelays, where delays are all elapsed
29 <    // (so, no blocking solely for delays) but are still ordered
21 >    private static final int NOCAP = Integer.MAX_VALUE;
22  
23 +    /**
24 +     * A delayed implmentation for testing.
25 +     * Most Q/BQ tests use Pseudodelays, where delays are all elapsed
26 +     * (so, no blocking solely for delays) but are still ordered
27 +     */
28      static class PDelay implements Delayed {
29          int pseudodelay;
30          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
# Line 68 | Line 65 | public class DelayQueueTest extends Test
65      }
66  
67  
68 +    /**
69 +     * Delayed implementation that actually delays
70 +     */
71 +    static class NanoDelay implements Delayed {
72 +        long trigger;
73 +        NanoDelay(long i) {
74 +            trigger = System.nanoTime() + i;
75 +        }
76 +        public int compareTo(Object y) {
77 +            long i = trigger;
78 +            long j = ((NanoDelay)y).trigger;
79 +            if (i < j) return -1;
80 +            if (i > j) return 1;
81 +            return 0;
82 +        }
83 +
84 +        public int compareTo(NanoDelay y) {
85 +            long i = trigger;
86 +            long j = ((NanoDelay)y).trigger;
87 +            if (i < j) return -1;
88 +            if (i > j) return 1;
89 +            return 0;
90 +        }
91 +
92 +        public boolean equals(Object other) {
93 +            return ((NanoDelay)other).trigger == trigger;
94 +        }
95 +        public boolean equals(NanoDelay other) {
96 +            return ((NanoDelay)other).trigger == trigger;
97 +        }
98 +
99 +        public long getDelay(TimeUnit unit) {
100 +            long n = trigger - System.nanoTime();
101 +            return unit.convert(n, TimeUnit.NANOSECONDS);
102 +        }
103 +
104 +        public long getTriggerTime() {
105 +            return trigger;
106 +        }
107 +
108 +        public String toString() {
109 +            return String.valueOf(trigger);
110 +        }
111 +    }
112  
113  
114      /**
115       * Create a queue of given size containing consecutive
116       * PDelays 0 ... n.
117       */
118 <    private DelayQueue fullQueue(int n) {
118 >    private DelayQueue populatedQueue(int n) {
119          DelayQueue q = new DelayQueue();
120          assertTrue(q.isEmpty());
121          for(int i = n-1; i >= 0; i-=2)
# Line 87 | Line 128 | public class DelayQueueTest extends Test
128          return q;
129      }
130  
131 <    public void testConstructor1(){
131 >    /**
132 >     *
133 >     */
134 >    public void testConstructor1() {
135          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
136      }
137  
138 <    public void testConstructor3(){
139 <
138 >    /**
139 >     *
140 >     */
141 >    public void testConstructor3() {
142          try {
143              DelayQueue q = new DelayQueue(null);
144 <            fail("Cannot make from null collection");
144 >            shouldThrow();
145          }
146          catch (NullPointerException success) {}
147      }
148  
149 <    public void testConstructor4(){
149 >    /**
150 >     *
151 >     */
152 >    public void testConstructor4() {
153          try {
154 <            PDelay[] ints = new PDelay[N];
154 >            PDelay[] ints = new PDelay[SIZE];
155              DelayQueue q = new DelayQueue(Arrays.asList(ints));
156 <            fail("Cannot make with null elements");
156 >            shouldThrow();
157          }
158          catch (NullPointerException success) {}
159      }
160  
161 <    public void testConstructor5(){
161 >    /**
162 >     *
163 >     */
164 >    public void testConstructor5() {
165          try {
166 <            PDelay[] ints = new PDelay[N];
167 <            for (int i = 0; i < N-1; ++i)
166 >            PDelay[] ints = new PDelay[SIZE];
167 >            for (int i = 0; i < SIZE-1; ++i)
168                  ints[i] = new PDelay(i);
169              DelayQueue q = new DelayQueue(Arrays.asList(ints));
170 <            fail("Cannot make with null elements");
170 >            shouldThrow();
171          }
172          catch (NullPointerException success) {}
173      }
174  
175 <    public void testConstructor6(){
175 >    /**
176 >     *
177 >     */
178 >    public void testConstructor6() {
179          try {
180 <            PDelay[] ints = new PDelay[N];
181 <            for (int i = 0; i < N; ++i)
180 >            PDelay[] ints = new PDelay[SIZE];
181 >            for (int i = 0; i < SIZE; ++i)
182                  ints[i] = new PDelay(i);
183              DelayQueue q = new DelayQueue(Arrays.asList(ints));
184 <            for (int i = 0; i < N; ++i)
184 >            for (int i = 0; i < SIZE; ++i)
185                  assertEquals(ints[i], q.poll());
186          }
187          finally {}
188      }
189  
190 +    /**
191 +     *
192 +     */
193      public void testEmpty() {
194          DelayQueue q = new DelayQueue();
195          assertTrue(q.isEmpty());
# Line 144 | Line 202 | public class DelayQueueTest extends Test
202          assertTrue(q.isEmpty());
203      }
204  
205 <    public void testRemainingCapacity(){
206 <        DelayQueue q = fullQueue(N);
207 <        for (int i = 0; i < N; ++i) {
205 >    /**
206 >     *
207 >     */
208 >    public void testRemainingCapacity() {
209 >        DelayQueue q = populatedQueue(SIZE);
210 >        for (int i = 0; i < SIZE; ++i) {
211              assertEquals(NOCAP, q.remainingCapacity());
212 <            assertEquals(N-i, q.size());
212 >            assertEquals(SIZE-i, q.size());
213              q.remove();
214          }
215 <        for (int i = 0; i < N; ++i) {
215 >        for (int i = 0; i < SIZE; ++i) {
216              assertEquals(NOCAP, q.remainingCapacity());
217              assertEquals(i, q.size());
218              q.add(new PDelay(i));
219          }
220      }
221  
222 <    public void testOfferNull(){
222 >    /**
223 >     *
224 >     */
225 >    public void testOfferNull() {
226          try {
227              DelayQueue q = new DelayQueue();
228              q.offer(null);
229 <            fail("should throw NPE");
229 >            shouldThrow();
230          } catch (NullPointerException success) { }  
231      }
232  
233 +    /**
234 +     *
235 +     */
236      public void testOffer() {
237          DelayQueue q = new DelayQueue();
238          assertTrue(q.offer(new PDelay(0)));
239          assertTrue(q.offer(new PDelay(1)));
240      }
241  
242 <    public void testAdd(){
242 >    /**
243 >     *
244 >     */
245 >    public void testAdd() {
246          DelayQueue q = new DelayQueue();
247 <        for (int i = 0; i < N; ++i) {
247 >        for (int i = 0; i < SIZE; ++i) {
248              assertEquals(i, q.size());
249              assertTrue(q.add(new PDelay(i)));
250          }
251      }
252  
253 <    public void testAddAll1(){
253 >    /**
254 >     *
255 >     */
256 >    public void testAddAll1() {
257          try {
258              DelayQueue q = new DelayQueue();
259              q.addAll(null);
260 <            fail("Cannot add null collection");
260 >            shouldThrow();
261          }
262          catch (NullPointerException success) {}
263      }
264 <    public void testAddAll2(){
264 >    /**
265 >     *
266 >     */
267 >    public void testAddAll2() {
268          try {
269              DelayQueue q = new DelayQueue();
270 <            PDelay[] ints = new PDelay[N];
270 >            PDelay[] ints = new PDelay[SIZE];
271              q.addAll(Arrays.asList(ints));
272 <            fail("Cannot add null elements");
272 >            shouldThrow();
273          }
274          catch (NullPointerException success) {}
275      }
276 <    public void testAddAll3(){
276 >    /**
277 >     *
278 >     */
279 >    public void testAddAll3() {
280          try {
281              DelayQueue q = new DelayQueue();
282 <            PDelay[] ints = new PDelay[N];
283 <            for (int i = 0; i < N-1; ++i)
282 >            PDelay[] ints = new PDelay[SIZE];
283 >            for (int i = 0; i < SIZE-1; ++i)
284                  ints[i] = new PDelay(i);
285              q.addAll(Arrays.asList(ints));
286 <            fail("Cannot add null elements");
286 >            shouldThrow();
287          }
288          catch (NullPointerException success) {}
289      }
290  
291 <    public void testAddAll5(){
291 >    /**
292 >     *
293 >     */
294 >    public void testAddAll5() {
295          try {
296              PDelay[] empty = new PDelay[0];
297 <            PDelay[] ints = new PDelay[N];
298 <            for (int i = N-1; i >= 0; --i)
297 >            PDelay[] ints = new PDelay[SIZE];
298 >            for (int i = SIZE-1; i >= 0; --i)
299                  ints[i] = new PDelay(i);
300              DelayQueue q = new DelayQueue();
301              assertFalse(q.addAll(Arrays.asList(empty)));
302              assertTrue(q.addAll(Arrays.asList(ints)));
303 <            for (int i = 0; i < N; ++i)
303 >            for (int i = 0; i < SIZE; ++i)
304                  assertEquals(ints[i], q.poll());
305          }
306          finally {}
307      }
308  
309 +    /**
310 +     *
311 +     */
312       public void testPutNull() {
313          try {
314              DelayQueue q = new DelayQueue();
315              q.put(null);
316 <            fail("put should throw NPE");
316 >            shouldThrow();
317          }
318          catch (NullPointerException success){
319          }  
320       }
321  
322 +    /**
323 +     *
324 +     */
325       public void testPut() {
326           try {
327               DelayQueue q = new DelayQueue();
328 <             for (int i = 0; i < N; ++i) {
328 >             for (int i = 0; i < SIZE; ++i) {
329                   PDelay I = new PDelay(i);
330                   q.put(I);
331                   assertTrue(q.contains(I));
332               }
333 <             assertEquals(N, q.size());
333 >             assertEquals(SIZE, q.size());
334           }
335           finally {
336          }
337      }
338  
339 +    /**
340 +     *
341 +     */
342      public void testPutWithTake() {
343          final DelayQueue q = new DelayQueue();
344          Thread t = new Thread(new Runnable() {
345 <                public void run(){
345 >                public void run() {
346                      int added = 0;
347                      try {
348                          q.put(new PDelay(0));
# Line 262 | Line 353 | public class DelayQueueTest extends Test
353                          ++added;
354                          q.put(new PDelay(0));
355                          ++added;
356 <                        assertTrue(added == 4);
356 >                        threadAssertTrue(added == 4);
357                      } finally {
358                      }
359                  }
# Line 274 | Line 365 | public class DelayQueueTest extends Test
365              t.interrupt();
366              t.join();
367          } catch (Exception e){
368 <            fail("Unexpected exception");
368 >            unexpectedException();
369          }
370      }
371  
372 +    /**
373 +     *
374 +     */
375      public void testTimedOffer() {
376          final DelayQueue q = new DelayQueue();
377          Thread t = new Thread(new Runnable() {
378 <                public void run(){
378 >                public void run() {
379                      try {
380                          q.put(new PDelay(0));
381                          q.put(new PDelay(0));
382 <                        assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
383 <                        assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
382 >                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
383 >                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
384                      } finally { }
385                  }
386              });
387          
388          try {
389              t.start();
390 <            Thread.sleep(SHORT_DELAY_MS);
390 >            Thread.sleep(SMALL_DELAY_MS);
391              t.interrupt();
392              t.join();
393          } catch (Exception e){
394 <            fail("Unexpected exception");
394 >            unexpectedException();
395          }
396      }
397  
398 <    public void testTake(){
398 >    /**
399 >     *
400 >     */
401 >    public void testTake() {
402          try {
403 <            DelayQueue q = fullQueue(N);
404 <            for (int i = 0; i < N; ++i) {
403 >            DelayQueue q = populatedQueue(SIZE);
404 >            for (int i = 0; i < SIZE; ++i) {
405                  assertEquals(new PDelay(i), ((PDelay)q.take()));
406              }
407          } catch (InterruptedException e){
408 <            fail("Unexpected exception");
408 >            unexpectedException();
409          }  
410      }
411  
412 +    /**
413 +     *
414 +     */
415      public void testTakeFromEmpty() {
416          final DelayQueue q = new DelayQueue();
417          Thread t = new Thread(new Runnable() {
418 <                public void run(){
418 >                public void run() {
419                      try {
420                          q.take();
421 <                        fail("Should block");
421 >                        threadShouldThrow();
422                      } catch (InterruptedException success){ }                
423                  }
424              });
# Line 328 | Line 428 | public class DelayQueueTest extends Test
428              t.interrupt();
429              t.join();
430          } catch (Exception e){
431 <            fail("Unexpected exception");
431 >            unexpectedException();
432          }
433      }
434  
435 <    public void testBlockingTake(){
435 >    /**
436 >     *
437 >     */
438 >    public void testBlockingTake() {
439          Thread t = new Thread(new Runnable() {
440                  public void run() {
441                      try {
442 <                        DelayQueue q = fullQueue(N);
443 <                        for (int i = 0; i < N; ++i) {
444 <                            assertEquals(new PDelay(i), ((PDelay)q.take()));
442 >                        DelayQueue q = populatedQueue(SIZE);
443 >                        for (int i = 0; i < SIZE; ++i) {
444 >                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
445                          }
446                          q.take();
447 <                        fail("take should block");
447 >                        threadShouldThrow();
448                      } catch (InterruptedException success){
449                      }  
450                  }});
# Line 352 | Line 455 | public class DelayQueueTest extends Test
455             t.join();
456          }
457          catch (InterruptedException ie) {
458 <            fail("Unexpected exception");
458 >            unexpectedException();
459          }
460      }
461  
462  
463 <    public void testPoll(){
464 <        DelayQueue q = fullQueue(N);
465 <        for (int i = 0; i < N; ++i) {
463 >    /**
464 >     *
465 >     */
466 >    public void testPoll() {
467 >        DelayQueue q = populatedQueue(SIZE);
468 >        for (int i = 0; i < SIZE; ++i) {
469              assertEquals(new PDelay(i), ((PDelay)q.poll()));
470          }
471          assertNull(q.poll());
472      }
473  
474 +    /**
475 +     *
476 +     */
477      public void testTimedPoll0() {
478          try {
479 <            DelayQueue q = fullQueue(N);
480 <            for (int i = 0; i < N; ++i) {
479 >            DelayQueue q = populatedQueue(SIZE);
480 >            for (int i = 0; i < SIZE; ++i) {
481                  assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
482              }
483              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484          } catch (InterruptedException e){
485 <            fail("Unexpected exception");
485 >            unexpectedException();
486          }  
487      }
488  
489 +    /**
490 +     *
491 +     */
492      public void testTimedPoll() {
493          try {
494 <            DelayQueue q = fullQueue(N);
495 <            for (int i = 0; i < N; ++i) {
494 >            DelayQueue q = populatedQueue(SIZE);
495 >            for (int i = 0; i < SIZE; ++i) {
496                  assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
497              }
498              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499          } catch (InterruptedException e){
500 <            fail("Unexpected exception");
500 >            unexpectedException();
501          }  
502      }
503  
504 <    public void testInterruptedTimedPoll(){
504 >    /**
505 >     *
506 >     */
507 >    public void testInterruptedTimedPoll() {
508          Thread t = new Thread(new Runnable() {
509                  public void run() {
510                      try {
511 <                        DelayQueue q = fullQueue(N);
512 <                        for (int i = 0; i < N; ++i) {
513 <                            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
511 >                        DelayQueue q = populatedQueue(SIZE);
512 >                        for (int i = 0; i < SIZE; ++i) {
513 >                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
514                          }
515 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
515 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516                      } catch (InterruptedException success){
517                      }  
518                  }});
# Line 408 | Line 523 | public class DelayQueueTest extends Test
523             t.join();
524          }
525          catch (InterruptedException ie) {
526 <            fail("Unexpected exception");
526 >            unexpectedException();
527          }
528      }
529  
530 <    public void testTimedPollWithOffer(){
530 >    /**
531 >     *
532 >     */
533 >    public void testTimedPollWithOffer() {
534          final DelayQueue q = new DelayQueue();
535          Thread t = new Thread(new Runnable() {
536 <                public void run(){
536 >                public void run() {
537                      try {
538 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
538 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
539                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
540                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
541 <                        fail("Should block");
541 >                        threadFail("Should block");
542                      } catch (InterruptedException success) { }                
543                  }
544              });
545          try {
546              t.start();
547 <            Thread.sleep(SHORT_DELAY_MS * 2);
547 >            Thread.sleep(SMALL_DELAY_MS);
548              assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
549              t.interrupt();
550              t.join();
551          } catch (Exception e){
552 <            fail("Unexpected exception");
552 >            unexpectedException();
553          }
554      }  
555  
556  
557 <    public void testPeek(){
558 <        DelayQueue q = fullQueue(N);
559 <        for (int i = 0; i < N; ++i) {
557 >    /**
558 >     *
559 >     */
560 >    public void testPeek() {
561 >        DelayQueue q = populatedQueue(SIZE);
562 >        for (int i = 0; i < SIZE; ++i) {
563              assertEquals(new PDelay(i), ((PDelay)q.peek()));
564              q.poll();
565              assertTrue(q.peek() == null ||
# Line 447 | Line 568 | public class DelayQueueTest extends Test
568          assertNull(q.peek());
569      }
570  
571 <    public void testElement(){
572 <        DelayQueue q = fullQueue(N);
573 <        for (int i = 0; i < N; ++i) {
571 >    /**
572 >     *
573 >     */
574 >    public void testElement() {
575 >        DelayQueue q = populatedQueue(SIZE);
576 >        for (int i = 0; i < SIZE; ++i) {
577              assertEquals(new PDelay(i), ((PDelay)q.element()));
578              q.poll();
579          }
580          try {
581              q.element();
582 <            fail("no such element");
582 >            shouldThrow();
583          }
584          catch (NoSuchElementException success) {}
585      }
586  
587 <    public void testRemove(){
588 <        DelayQueue q = fullQueue(N);
589 <        for (int i = 0; i < N; ++i) {
587 >    /**
588 >     *
589 >     */
590 >    public void testRemove() {
591 >        DelayQueue q = populatedQueue(SIZE);
592 >        for (int i = 0; i < SIZE; ++i) {
593              assertEquals(new PDelay(i), ((PDelay)q.remove()));
594          }
595          try {
596              q.remove();
597 <            fail("remove should throw");
597 >            shouldThrow();
598          } catch (NoSuchElementException success){
599          }  
600      }
601  
602 <    public void testRemoveElement(){
603 <        DelayQueue q = fullQueue(N);
604 <        for (int i = 1; i < N; i+=2) {
602 >    /**
603 >     *
604 >     */
605 >    public void testRemoveElement() {
606 >        DelayQueue q = populatedQueue(SIZE);
607 >        for (int i = 1; i < SIZE; i+=2) {
608              assertTrue(q.remove(new PDelay(i)));
609          }
610 <        for (int i = 0; i < N; i+=2) {
610 >        for (int i = 0; i < SIZE; i+=2) {
611              assertTrue(q.remove(new PDelay(i)));
612              assertFalse(q.remove(new PDelay(i+1)));
613          }
614          assertTrue(q.isEmpty());
615      }
616          
617 <    public void testContains(){
618 <        DelayQueue q = fullQueue(N);
619 <        for (int i = 0; i < N; ++i) {
617 >    /**
618 >     *
619 >     */
620 >    public void testContains() {
621 >        DelayQueue q = populatedQueue(SIZE);
622 >        for (int i = 0; i < SIZE; ++i) {
623              assertTrue(q.contains(new PDelay(i)));
624              q.poll();
625              assertFalse(q.contains(new PDelay(i)));
626          }
627      }
628  
629 <    public void testClear(){
630 <        DelayQueue q = fullQueue(N);
629 >    /**
630 >     *
631 >     */
632 >    public void testClear() {
633 >        DelayQueue q = populatedQueue(SIZE);
634          q.clear();
635          assertTrue(q.isEmpty());
636          assertEquals(0, q.size());
# Line 505 | Line 641 | public class DelayQueueTest extends Test
641          assertTrue(q.isEmpty());
642      }
643  
644 <    public void testContainsAll(){
645 <        DelayQueue q = fullQueue(N);
644 >    /**
645 >     *
646 >     */
647 >    public void testContainsAll() {
648 >        DelayQueue q = populatedQueue(SIZE);
649          DelayQueue p = new DelayQueue();
650 <        for (int i = 0; i < N; ++i) {
650 >        for (int i = 0; i < SIZE; ++i) {
651              assertTrue(q.containsAll(p));
652              assertFalse(p.containsAll(q));
653              p.add(new PDelay(i));
# Line 516 | Line 655 | public class DelayQueueTest extends Test
655          assertTrue(p.containsAll(q));
656      }
657  
658 <    public void testRetainAll(){
659 <        DelayQueue q = fullQueue(N);
660 <        DelayQueue p = fullQueue(N);
661 <        for (int i = 0; i < N; ++i) {
658 >    /**
659 >     *
660 >     */
661 >    public void testRetainAll() {
662 >        DelayQueue q = populatedQueue(SIZE);
663 >        DelayQueue p = populatedQueue(SIZE);
664 >        for (int i = 0; i < SIZE; ++i) {
665              boolean changed = q.retainAll(p);
666              if (i == 0)
667                  assertFalse(changed);
# Line 527 | Line 669 | public class DelayQueueTest extends Test
669                  assertTrue(changed);
670  
671              assertTrue(q.containsAll(p));
672 <            assertEquals(N-i, q.size());
672 >            assertEquals(SIZE-i, q.size());
673              p.remove();
674          }
675      }
676  
677 <    public void testRemoveAll(){
678 <        for (int i = 1; i < N; ++i) {
679 <            DelayQueue q = fullQueue(N);
680 <            DelayQueue p = fullQueue(i);
677 >    /**
678 >     *
679 >     */
680 >    public void testRemoveAll() {
681 >        for (int i = 1; i < SIZE; ++i) {
682 >            DelayQueue q = populatedQueue(SIZE);
683 >            DelayQueue p = populatedQueue(i);
684              assertTrue(q.removeAll(p));
685 <            assertEquals(N-i, q.size());
685 >            assertEquals(SIZE-i, q.size());
686              for (int j = 0; j < i; ++j) {
687                  PDelay I = (PDelay)(p.remove());
688                  assertFalse(q.contains(I));
# Line 545 | Line 690 | public class DelayQueueTest extends Test
690          }
691      }
692  
693 <    /*
694 <    public void testEqualsAndHashCode(){
695 <        DelayQueue q1 = fullQueue(N);
696 <        DelayQueue q2 = fullQueue(N);
697 <        assertTrue(q1.equals(q2));
553 <        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);
693 >    /**
694 >     *
695 >     */
696 >    public void testToArray() {
697 >        DelayQueue q = populatedQueue(SIZE);
698          Object[] o = q.toArray();
699          Arrays.sort(o);
700          try {
701          for(int i = 0; i < o.length; i++)
702              assertEquals(o[i], q.take());
703          } catch (InterruptedException e){
704 <            fail("Unexpected exception");
704 >            unexpectedException();
705          }    
706      }
707  
708 <    public void testToArray2(){
709 <        DelayQueue q = fullQueue(N);
710 <        PDelay[] ints = new PDelay[N];
708 >    /**
709 >     *
710 >     */
711 >    public void testToArray2() {
712 >        DelayQueue q = populatedQueue(SIZE);
713 >        PDelay[] ints = new PDelay[SIZE];
714          ints = (PDelay[])q.toArray(ints);
715          Arrays.sort(ints);
716          try {
717              for(int i = 0; i < ints.length; i++)
718                  assertEquals(ints[i], q.take());
719          } catch (InterruptedException e){
720 <            fail("Unexpected exception");
720 >            unexpectedException();
721          }    
722      }
723      
724 <    public void testIterator(){
725 <        DelayQueue q = fullQueue(N);
724 >    /**
725 >     *
726 >     */
727 >    public void testIterator() {
728 >        DelayQueue q = populatedQueue(SIZE);
729          int i = 0;
730          Iterator it = q.iterator();
731          while(it.hasNext()) {
732              assertTrue(q.contains(it.next()));
733              ++i;
734          }
735 <        assertEquals(i, N);
735 >        assertEquals(i, SIZE);
736      }
737  
738 +    /**
739 +     *
740 +     */
741      public void testIteratorRemove () {
742  
743          final DelayQueue q = new DelayQueue();
# Line 618 | Line 757 | public class DelayQueueTest extends Test
757      }
758  
759  
760 <    public void testToString(){
761 <        DelayQueue q = fullQueue(N);
760 >    /**
761 >     *
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.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
768          }
769      }        
770  
771 +    /**
772 +     *
773 +     */
774      public void testPollInExecutor() {
775  
776          final DelayQueue q = new DelayQueue();
# Line 634 | Line 779 | public class DelayQueueTest extends Test
779  
780          executor.execute(new Runnable() {
781              public void run() {
782 <                assertNull("poll should fail", q.poll());
782 >                threadAssertNull(q.poll());
783                  try {
784 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
785 <                    assertTrue(q.isEmpty());
784 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
785 >                    threadAssertTrue(q.isEmpty());
786                  }
787                  catch (InterruptedException e) {
788 <                    fail("should not be interrupted");
788 >                    threadUnexpectedException();
789                  }
790              }
791          });
# Line 648 | Line 793 | public class DelayQueueTest extends Test
793          executor.execute(new Runnable() {
794              public void run() {
795                  try {
796 <                    Thread.sleep(MEDIUM_DELAY_MS);
796 >                    Thread.sleep(SHORT_DELAY_MS);
797                      q.put(new PDelay(1));
798                  }
799                  catch (InterruptedException e) {
800 <                    fail("should not be interrupted");
800 >                    threadUnexpectedException();
801                  }
802              }
803          });
804          
805 <        executor.shutdown();
805 >        joinPool(executor);
806  
807      }
808  
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        }
676
677        public int compareTo(NanoDelay y) {
678            long i = trigger;
679            long j = ((NanoDelay)y).trigger;
680            if (i < j) return -1;
681            if (i > j) return 1;
682            return 0;
683        }
684
685        public boolean equals(Object other) {
686            return ((NanoDelay)other).trigger == trigger;
687        }
688        public boolean equals(NanoDelay other) {
689            return ((NanoDelay)other).trigger == trigger;
690        }
691
692        public long getDelay(TimeUnit unit) {
693            long n = trigger - System.nanoTime();
694            return unit.convert(n, TimeUnit.NANOSECONDS);
695        }
696
697        public long getTriggerTime() {
698            return trigger;
699        }
700
701        public String toString() {
702            return String.valueOf(trigger);
703        }
704    }
809  
810 +    /**
811 +     *
812 +     */
813      public void testDelay() {
814          DelayQueue q = new DelayQueue();
815 <        NanoDelay[] elements = new NanoDelay[N];
816 <        for (int i = 0; i < N; ++i) {
817 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (N - i));
815 >        NanoDelay[] elements = new NanoDelay[SIZE];
816 >        for (int i = 0; i < SIZE; ++i) {
817 >            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
818          }
819 <        for (int i = 0; i < N; ++i) {
819 >        for (int i = 0; i < SIZE; ++i) {
820              q.add(elements[i]);
821          }
822  
823          try {
824              long last = 0;
825 <            for (int i = 0; i < N; ++i) {
825 >            for (int i = 0; i < SIZE; ++i) {
826                  NanoDelay e = (NanoDelay)(q.take());
827                  long tt = e.getTriggerTime();
828                  assertTrue(tt <= System.nanoTime());
# Line 725 | Line 832 | public class DelayQueueTest extends Test
832              }
833          }
834          catch(InterruptedException ie) {
835 <            fail("Unexpected Exception");
835 >            unexpectedException();
836          }
837      }
838  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines