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.3 by dl, Sun Sep 14 20:42:40 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 +    private static final int NOCAP = Integer.MAX_VALUE;
22 +
23      // Most Q/BQ tests use Pseudodelays, where delays are all elapsed
24      // (so, no blocking solely for delays) but are still ordered
25  
# Line 74 | Line 69 | public class DelayQueueTest extends Test
69       * Create a queue of given size containing consecutive
70       * PDelays 0 ... n.
71       */
72 <    private DelayQueue fullQueue(int n) {
72 >    private DelayQueue populatedQueue(int n) {
73          DelayQueue q = new DelayQueue();
74          assertTrue(q.isEmpty());
75          for(int i = n-1; i >= 0; i-=2)
# Line 102 | Line 97 | public class DelayQueueTest extends Test
97  
98      public void testConstructor4(){
99          try {
100 <            PDelay[] ints = new PDelay[N];
100 >            PDelay[] ints = new PDelay[SIZE];
101              DelayQueue q = new DelayQueue(Arrays.asList(ints));
102              fail("Cannot make with null elements");
103          }
# Line 111 | Line 106 | public class DelayQueueTest extends Test
106  
107      public void testConstructor5(){
108          try {
109 <            PDelay[] ints = new PDelay[N];
110 <            for (int i = 0; i < N-1; ++i)
109 >            PDelay[] ints = new PDelay[SIZE];
110 >            for (int i = 0; i < SIZE-1; ++i)
111                  ints[i] = new PDelay(i);
112              DelayQueue q = new DelayQueue(Arrays.asList(ints));
113              fail("Cannot make with null elements");
# Line 122 | Line 117 | public class DelayQueueTest extends Test
117  
118      public void testConstructor6(){
119          try {
120 <            PDelay[] ints = new PDelay[N];
121 <            for (int i = 0; i < N; ++i)
120 >            PDelay[] ints = new PDelay[SIZE];
121 >            for (int i = 0; i < SIZE; ++i)
122                  ints[i] = new PDelay(i);
123              DelayQueue q = new DelayQueue(Arrays.asList(ints));
124 <            for (int i = 0; i < N; ++i)
124 >            for (int i = 0; i < SIZE; ++i)
125                  assertEquals(ints[i], q.poll());
126          }
127          finally {}
# Line 145 | Line 140 | public class DelayQueueTest extends Test
140      }
141  
142      public void testRemainingCapacity(){
143 <        DelayQueue q = fullQueue(N);
144 <        for (int i = 0; i < N; ++i) {
143 >        DelayQueue q = populatedQueue(SIZE);
144 >        for (int i = 0; i < SIZE; ++i) {
145              assertEquals(NOCAP, q.remainingCapacity());
146 <            assertEquals(N-i, q.size());
146 >            assertEquals(SIZE-i, q.size());
147              q.remove();
148          }
149 <        for (int i = 0; i < N; ++i) {
149 >        for (int i = 0; i < SIZE; ++i) {
150              assertEquals(NOCAP, q.remainingCapacity());
151              assertEquals(i, q.size());
152              q.add(new PDelay(i));
# Line 174 | Line 169 | public class DelayQueueTest extends Test
169  
170      public void testAdd(){
171          DelayQueue q = new DelayQueue();
172 <        for (int i = 0; i < N; ++i) {
172 >        for (int i = 0; i < SIZE; ++i) {
173              assertEquals(i, q.size());
174              assertTrue(q.add(new PDelay(i)));
175          }
# Line 191 | Line 186 | public class DelayQueueTest extends Test
186      public void testAddAll2(){
187          try {
188              DelayQueue q = new DelayQueue();
189 <            PDelay[] ints = new PDelay[N];
189 >            PDelay[] ints = new PDelay[SIZE];
190              q.addAll(Arrays.asList(ints));
191              fail("Cannot add null elements");
192          }
# Line 200 | Line 195 | public class DelayQueueTest extends Test
195      public void testAddAll3(){
196          try {
197              DelayQueue q = new DelayQueue();
198 <            PDelay[] ints = new PDelay[N];
199 <            for (int i = 0; i < N-1; ++i)
198 >            PDelay[] ints = new PDelay[SIZE];
199 >            for (int i = 0; i < SIZE-1; ++i)
200                  ints[i] = new PDelay(i);
201              q.addAll(Arrays.asList(ints));
202              fail("Cannot add null elements");
# Line 212 | Line 207 | public class DelayQueueTest extends Test
207      public void testAddAll5(){
208          try {
209              PDelay[] empty = new PDelay[0];
210 <            PDelay[] ints = new PDelay[N];
211 <            for (int i = N-1; i >= 0; --i)
210 >            PDelay[] ints = new PDelay[SIZE];
211 >            for (int i = SIZE-1; i >= 0; --i)
212                  ints[i] = new PDelay(i);
213              DelayQueue q = new DelayQueue();
214              assertFalse(q.addAll(Arrays.asList(empty)));
215              assertTrue(q.addAll(Arrays.asList(ints)));
216 <            for (int i = 0; i < N; ++i)
216 >            for (int i = 0; i < SIZE; ++i)
217                  assertEquals(ints[i], q.poll());
218          }
219          finally {}
# Line 237 | Line 232 | public class DelayQueueTest extends Test
232       public void testPut() {
233           try {
234               DelayQueue q = new DelayQueue();
235 <             for (int i = 0; i < N; ++i) {
235 >             for (int i = 0; i < SIZE; ++i) {
236                   PDelay I = new PDelay(i);
237                   q.put(I);
238                   assertTrue(q.contains(I));
239               }
240 <             assertEquals(N, q.size());
240 >             assertEquals(SIZE, q.size());
241           }
242           finally {
243          }
# Line 262 | Line 257 | public class DelayQueueTest extends Test
257                          ++added;
258                          q.put(new PDelay(0));
259                          ++added;
260 <                        assertTrue(added == 4);
260 >                        threadAssertTrue(added == 4);
261                      } finally {
262                      }
263                  }
# Line 285 | Line 280 | public class DelayQueueTest extends Test
280                      try {
281                          q.put(new PDelay(0));
282                          q.put(new PDelay(0));
283 <                        assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
284 <                        assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
283 >                        threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
284 >                        threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
285                      } finally { }
286                  }
287              });
288          
289          try {
290              t.start();
291 <            Thread.sleep(SHORT_DELAY_MS);
291 >            Thread.sleep(SMALL_DELAY_MS);
292              t.interrupt();
293              t.join();
294          } catch (Exception e){
# Line 303 | Line 298 | public class DelayQueueTest extends Test
298  
299      public void testTake(){
300          try {
301 <            DelayQueue q = fullQueue(N);
302 <            for (int i = 0; i < N; ++i) {
301 >            DelayQueue q = populatedQueue(SIZE);
302 >            for (int i = 0; i < SIZE; ++i) {
303                  assertEquals(new PDelay(i), ((PDelay)q.take()));
304              }
305          } catch (InterruptedException e){
# Line 318 | Line 313 | public class DelayQueueTest extends Test
313                  public void run(){
314                      try {
315                          q.take();
316 <                        fail("Should block");
316 >                        threadFail("Should block");
317                      } catch (InterruptedException success){ }                
318                  }
319              });
# Line 336 | Line 331 | public class DelayQueueTest extends Test
331          Thread t = new Thread(new Runnable() {
332                  public void run() {
333                      try {
334 <                        DelayQueue q = fullQueue(N);
335 <                        for (int i = 0; i < N; ++i) {
336 <                            assertEquals(new PDelay(i), ((PDelay)q.take()));
334 >                        DelayQueue q = populatedQueue(SIZE);
335 >                        for (int i = 0; i < SIZE; ++i) {
336 >                            threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
337                          }
338                          q.take();
339 <                        fail("take should block");
339 >                        threadFail("take should block");
340                      } catch (InterruptedException success){
341                      }  
342                  }});
# Line 358 | Line 353 | public class DelayQueueTest extends Test
353  
354  
355      public void testPoll(){
356 <        DelayQueue q = fullQueue(N);
357 <        for (int i = 0; i < N; ++i) {
356 >        DelayQueue q = populatedQueue(SIZE);
357 >        for (int i = 0; i < SIZE; ++i) {
358              assertEquals(new PDelay(i), ((PDelay)q.poll()));
359          }
360          assertNull(q.poll());
# Line 367 | Line 362 | public class DelayQueueTest extends Test
362  
363      public void testTimedPoll0() {
364          try {
365 <            DelayQueue q = fullQueue(N);
366 <            for (int i = 0; i < N; ++i) {
365 >            DelayQueue q = populatedQueue(SIZE);
366 >            for (int i = 0; i < SIZE; ++i) {
367                  assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
368              }
369              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
# Line 379 | Line 374 | public class DelayQueueTest extends Test
374  
375      public void testTimedPoll() {
376          try {
377 <            DelayQueue q = fullQueue(N);
378 <            for (int i = 0; i < N; ++i) {
377 >            DelayQueue q = populatedQueue(SIZE);
378 >            for (int i = 0; i < SIZE; ++i) {
379                  assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
380              }
381              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 393 | Line 388 | public class DelayQueueTest extends Test
388          Thread t = new Thread(new Runnable() {
389                  public void run() {
390                      try {
391 <                        DelayQueue q = fullQueue(N);
392 <                        for (int i = 0; i < N; ++i) {
393 <                            assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
391 >                        DelayQueue q = populatedQueue(SIZE);
392 >                        for (int i = 0; i < SIZE; ++i) {
393 >                            threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
394                          }
395 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
395 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396                      } catch (InterruptedException success){
397                      }  
398                  }});
# Line 417 | Line 412 | public class DelayQueueTest extends Test
412          Thread t = new Thread(new Runnable() {
413                  public void run(){
414                      try {
415 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
415 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
416                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
417                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
418 <                        fail("Should block");
418 >                        threadFail("Should block");
419                      } catch (InterruptedException success) { }                
420                  }
421              });
422          try {
423              t.start();
424 <            Thread.sleep(SHORT_DELAY_MS * 2);
424 >            Thread.sleep(SMALL_DELAY_MS);
425              assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
426              t.interrupt();
427              t.join();
# Line 437 | Line 432 | public class DelayQueueTest extends Test
432  
433  
434      public void testPeek(){
435 <        DelayQueue q = fullQueue(N);
436 <        for (int i = 0; i < N; ++i) {
435 >        DelayQueue q = populatedQueue(SIZE);
436 >        for (int i = 0; i < SIZE; ++i) {
437              assertEquals(new PDelay(i), ((PDelay)q.peek()));
438              q.poll();
439              assertTrue(q.peek() == null ||
# Line 448 | Line 443 | public class DelayQueueTest extends Test
443      }
444  
445      public void testElement(){
446 <        DelayQueue q = fullQueue(N);
447 <        for (int i = 0; i < N; ++i) {
446 >        DelayQueue q = populatedQueue(SIZE);
447 >        for (int i = 0; i < SIZE; ++i) {
448              assertEquals(new PDelay(i), ((PDelay)q.element()));
449              q.poll();
450          }
# Line 461 | Line 456 | public class DelayQueueTest extends Test
456      }
457  
458      public void testRemove(){
459 <        DelayQueue q = fullQueue(N);
460 <        for (int i = 0; i < N; ++i) {
459 >        DelayQueue q = populatedQueue(SIZE);
460 >        for (int i = 0; i < SIZE; ++i) {
461              assertEquals(new PDelay(i), ((PDelay)q.remove()));
462          }
463          try {
# Line 473 | Line 468 | public class DelayQueueTest extends Test
468      }
469  
470      public void testRemoveElement(){
471 <        DelayQueue q = fullQueue(N);
472 <        for (int i = 1; i < N; i+=2) {
471 >        DelayQueue q = populatedQueue(SIZE);
472 >        for (int i = 1; i < SIZE; i+=2) {
473              assertTrue(q.remove(new PDelay(i)));
474          }
475 <        for (int i = 0; i < N; i+=2) {
475 >        for (int i = 0; i < SIZE; i+=2) {
476              assertTrue(q.remove(new PDelay(i)));
477              assertFalse(q.remove(new PDelay(i+1)));
478          }
# Line 485 | Line 480 | public class DelayQueueTest extends Test
480      }
481          
482      public void testContains(){
483 <        DelayQueue q = fullQueue(N);
484 <        for (int i = 0; i < N; ++i) {
483 >        DelayQueue q = populatedQueue(SIZE);
484 >        for (int i = 0; i < SIZE; ++i) {
485              assertTrue(q.contains(new PDelay(i)));
486              q.poll();
487              assertFalse(q.contains(new PDelay(i)));
# Line 494 | Line 489 | public class DelayQueueTest extends Test
489      }
490  
491      public void testClear(){
492 <        DelayQueue q = fullQueue(N);
492 >        DelayQueue q = populatedQueue(SIZE);
493          q.clear();
494          assertTrue(q.isEmpty());
495          assertEquals(0, q.size());
# Line 506 | Line 501 | public class DelayQueueTest extends Test
501      }
502  
503      public void testContainsAll(){
504 <        DelayQueue q = fullQueue(N);
504 >        DelayQueue q = populatedQueue(SIZE);
505          DelayQueue p = new DelayQueue();
506 <        for (int i = 0; i < N; ++i) {
506 >        for (int i = 0; i < SIZE; ++i) {
507              assertTrue(q.containsAll(p));
508              assertFalse(p.containsAll(q));
509              p.add(new PDelay(i));
# Line 517 | Line 512 | public class DelayQueueTest extends Test
512      }
513  
514      public void testRetainAll(){
515 <        DelayQueue q = fullQueue(N);
516 <        DelayQueue p = fullQueue(N);
517 <        for (int i = 0; i < N; ++i) {
515 >        DelayQueue q = populatedQueue(SIZE);
516 >        DelayQueue p = populatedQueue(SIZE);
517 >        for (int i = 0; i < SIZE; ++i) {
518              boolean changed = q.retainAll(p);
519              if (i == 0)
520                  assertFalse(changed);
# Line 527 | Line 522 | public class DelayQueueTest extends Test
522                  assertTrue(changed);
523  
524              assertTrue(q.containsAll(p));
525 <            assertEquals(N-i, q.size());
525 >            assertEquals(SIZE-i, q.size());
526              p.remove();
527          }
528      }
529  
530      public void testRemoveAll(){
531 <        for (int i = 1; i < N; ++i) {
532 <            DelayQueue q = fullQueue(N);
533 <            DelayQueue p = fullQueue(i);
531 >        for (int i = 1; i < SIZE; ++i) {
532 >            DelayQueue q = populatedQueue(SIZE);
533 >            DelayQueue p = populatedQueue(i);
534              assertTrue(q.removeAll(p));
535 <            assertEquals(N-i, q.size());
535 >            assertEquals(SIZE-i, q.size());
536              for (int j = 0; j < i; ++j) {
537                  PDelay I = (PDelay)(p.remove());
538                  assertFalse(q.contains(I));
# Line 545 | Line 540 | public class DelayQueueTest extends Test
540          }
541      }
542  
548    /*
549    public void testEqualsAndHashCode(){
550        DelayQueue q1 = fullQueue(N);
551        DelayQueue q2 = fullQueue(N);
552        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
543      public void testToArray(){
544 <        DelayQueue q = fullQueue(N);
544 >        DelayQueue q = populatedQueue(SIZE);
545          Object[] o = q.toArray();
546          Arrays.sort(o);
547          try {
# Line 576 | Line 553 | public class DelayQueueTest extends Test
553      }
554  
555      public void testToArray2(){
556 <        DelayQueue q = fullQueue(N);
557 <        PDelay[] ints = new PDelay[N];
556 >        DelayQueue q = populatedQueue(SIZE);
557 >        PDelay[] ints = new PDelay[SIZE];
558          ints = (PDelay[])q.toArray(ints);
559          Arrays.sort(ints);
560          try {
# Line 589 | Line 566 | public class DelayQueueTest extends Test
566      }
567      
568      public void testIterator(){
569 <        DelayQueue q = fullQueue(N);
569 >        DelayQueue q = populatedQueue(SIZE);
570          int i = 0;
571          Iterator it = q.iterator();
572          while(it.hasNext()) {
573              assertTrue(q.contains(it.next()));
574              ++i;
575          }
576 <        assertEquals(i, N);
576 >        assertEquals(i, SIZE);
577      }
578  
579      public void testIteratorRemove () {
# Line 619 | Line 596 | public class DelayQueueTest extends Test
596  
597  
598      public void testToString(){
599 <        DelayQueue q = fullQueue(N);
599 >        DelayQueue q = populatedQueue(SIZE);
600          String s = q.toString();
601 <        for (int i = 0; i < N; ++i) {
602 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
601 >        for (int i = 0; i < SIZE; ++i) {
602 >            assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
603          }
604      }        
605  
# Line 634 | Line 611 | public class DelayQueueTest extends Test
611  
612          executor.execute(new Runnable() {
613              public void run() {
614 <                assertNull("poll should fail", q.poll());
614 >                threadAssertNull(q.poll());
615                  try {
616 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
617 <                    assertTrue(q.isEmpty());
616 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
617 >                    threadAssertTrue(q.isEmpty());
618                  }
619                  catch (InterruptedException e) {
620 <                    fail("should not be interrupted");
620 >                    threadFail("should not be interrupted");
621                  }
622              }
623          });
# Line 648 | Line 625 | public class DelayQueueTest extends Test
625          executor.execute(new Runnable() {
626              public void run() {
627                  try {
628 <                    Thread.sleep(MEDIUM_DELAY_MS);
628 >                    Thread.sleep(SHORT_DELAY_MS);
629                      q.put(new PDelay(1));
630                  }
631                  catch (InterruptedException e) {
632 <                    fail("should not be interrupted");
632 >                    threadFail("should not be interrupted");
633                  }
634              }
635          });
636          
637 <        executor.shutdown();
637 >        joinPool(executor);
638  
639      }
640  
# Line 705 | Line 682 | public class DelayQueueTest extends Test
682  
683      public void testDelay() {
684          DelayQueue q = new DelayQueue();
685 <        NanoDelay[] elements = new NanoDelay[N];
686 <        for (int i = 0; i < N; ++i) {
687 <            elements[i] = new NanoDelay(1000000000L + 1000000L * (N - i));
685 >        NanoDelay[] elements = new NanoDelay[SIZE];
686 >        for (int i = 0; i < SIZE; ++i) {
687 >            elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
688          }
689 <        for (int i = 0; i < N; ++i) {
689 >        for (int i = 0; i < SIZE; ++i) {
690              q.add(elements[i]);
691          }
692  
693          try {
694              long last = 0;
695 <            for (int i = 0; i < N; ++i) {
695 >            for (int i = 0; i < SIZE; ++i) {
696                  NanoDelay e = (NanoDelay)(q.take());
697                  long tt = e.getTriggerTime();
698                  assertTrue(tt <= System.nanoTime());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines