ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.20 by jsr166, Sat Nov 21 19:11:53 2009 UTC vs.
Revision 1.44 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 13 | Line 13 | import static java.util.concurrent.TimeU
13   import java.io.*;
14  
15   public class PriorityBlockingQueueTest extends JSR166TestCase {
16 +
17 +    public static class Generic extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new PriorityBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class InitialCapacity extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new PriorityBlockingQueue(20);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32 +
33      public static Test suite() {
34 <        return new TestSuite(PriorityBlockingQueueTest.class);
34 >        return newTestSuite(PriorityBlockingQueueTest.class,
35 >                            new Generic().testSuite(),
36 >                            new InitialCapacity().testSuite());
37      }
38  
39      private static final int NOCAP = Integer.MAX_VALUE;
# Line 25 | Line 41 | public class PriorityBlockingQueueTest e
41      /** Sample Comparator */
42      static class MyReverseComparator implements Comparator {
43          public int compare(Object x, Object y) {
44 <            int i = ((Integer)x).intValue();
29 <            int j = ((Integer)y).intValue();
30 <            if (i < j) return 1;
31 <            if (i > j) return -1;
32 <            return 0;
44 >            return ((Comparable)y).compareTo(x);
45          }
46      }
47  
# Line 37 | Line 49 | public class PriorityBlockingQueueTest e
49       * Create a queue of given size containing consecutive
50       * Integers 0 ... n.
51       */
52 <    private PriorityBlockingQueue populatedQueue(int n) {
53 <        PriorityBlockingQueue q = new PriorityBlockingQueue(n);
52 >    private PriorityBlockingQueue<Integer> populatedQueue(int n) {
53 >        PriorityBlockingQueue<Integer> q =
54 >            new PriorityBlockingQueue<Integer>(n);
55          assertTrue(q.isEmpty());
56          for (int i = n-1; i >= 0; i-=2)
57              assertTrue(q.offer(new Integer(i)));
# Line 249 | Line 262 | public class PriorityBlockingQueueTest e
262              shouldThrow();
263          } catch (NullPointerException success) {}
264      }
265 +
266      /**
267       * addAll of a collection with any null elements throws NPE after
268       * possibly adding some elements
# Line 282 | Line 296 | public class PriorityBlockingQueueTest e
296      /**
297       * put(null) throws NPE
298       */
299 <     public void testPutNull() {
299 >    public void testPutNull() {
300          try {
301              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
302              q.put(null);
303              shouldThrow();
304          } catch (NullPointerException success) {}
305 <     }
305 >    }
306  
307      /**
308       * all elements successfully put are contained
309       */
310 <     public void testPut() {
311 <         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
312 <         for (int i = 0; i < SIZE; ++i) {
313 <             Integer I = new Integer(i);
314 <             q.put(I);
315 <             assertTrue(q.contains(I));
316 <         }
317 <         assertEquals(SIZE, q.size());
310 >    public void testPut() {
311 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
312 >        for (int i = 0; i < SIZE; ++i) {
313 >            Integer I = new Integer(i);
314 >            q.put(I);
315 >            assertTrue(q.contains(I));
316 >        }
317 >        assertEquals(SIZE, q.size());
318      }
319  
320      /**
# Line 309 | Line 323 | public class PriorityBlockingQueueTest e
323      public void testPutWithTake() throws InterruptedException {
324          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
325          final int size = 4;
326 <        Thread t = new Thread(new CheckedRunnable() {
326 >        Thread t = newStartedThread(new CheckedRunnable() {
327              public void realRun() {
328                  for (int i = 0; i < size; i++)
329                      q.put(new Integer(0));
330              }});
331  
332 <        t.start();
333 <        Thread.sleep(SHORT_DELAY_MS);
320 <        assertEquals(q.size(), size);
332 >        awaitTermination(t);
333 >        assertEquals(size, q.size());
334          q.take();
322        t.interrupt();
323        t.join();
335      }
336  
337      /**
# Line 328 | Line 339 | public class PriorityBlockingQueueTest e
339       */
340      public void testTimedOffer() throws InterruptedException {
341          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
342 <        Thread t = new Thread(new CheckedRunnable() {
342 >        Thread t = newStartedThread(new CheckedRunnable() {
343              public void realRun() {
344                  q.put(new Integer(0));
345                  q.put(new Integer(0));
346 <                threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
347 <                threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
346 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
347 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
348              }});
349  
350 <        t.start();
340 <        Thread.sleep(SMALL_DELAY_MS);
341 <        t.interrupt();
342 <        t.join();
350 >        awaitTermination(t);
351      }
352  
353      /**
# Line 348 | Line 356 | public class PriorityBlockingQueueTest e
356      public void testTake() throws InterruptedException {
357          PriorityBlockingQueue q = populatedQueue(SIZE);
358          for (int i = 0; i < SIZE; ++i) {
359 <            assertEquals(i, ((Integer)q.take()).intValue());
359 >            assertEquals(i, q.take());
360          }
361      }
362  
363      /**
356     * take blocks interruptibly when empty
357     */
358    public void testTakeFromEmpty() throws InterruptedException {
359        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
360        Thread t = new Thread(new CheckedInterruptedRunnable() {
361            public void realRun() throws InterruptedException {
362                q.take();
363            }});
364
365        t.start();
366        Thread.sleep(SHORT_DELAY_MS);
367        t.interrupt();
368        t.join();
369    }
370
371    /**
364       * Take removes existing elements until empty, then blocks interruptibly
365       */
366      public void testBlockingTake() throws InterruptedException {
367 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
367 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
368 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
369 >        Thread t = newStartedThread(new CheckedRunnable() {
370              public void realRun() throws InterruptedException {
377                PriorityBlockingQueue q = populatedQueue(SIZE);
371                  for (int i = 0; i < SIZE; ++i) {
372 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
372 >                    assertEquals(i, q.take());
373                  }
374 <                q.take();
374 >
375 >                Thread.currentThread().interrupt();
376 >                try {
377 >                    q.take();
378 >                    shouldThrow();
379 >                } catch (InterruptedException success) {}
380 >                assertFalse(Thread.interrupted());
381 >
382 >                pleaseInterrupt.countDown();
383 >                try {
384 >                    q.take();
385 >                    shouldThrow();
386 >                } catch (InterruptedException success) {}
387 >                assertFalse(Thread.interrupted());
388              }});
389  
390 <        t.start();
391 <        Thread.sleep(SHORT_DELAY_MS);
390 >        await(pleaseInterrupt);
391 >        assertThreadStaysAlive(t);
392          t.interrupt();
393 <        t.join();
393 >        awaitTermination(t);
394      }
395  
390
396      /**
397       * poll succeeds unless empty
398       */
399      public void testPoll() {
400          PriorityBlockingQueue q = populatedQueue(SIZE);
401          for (int i = 0; i < SIZE; ++i) {
402 <            assertEquals(i, ((Integer)q.poll()).intValue());
402 >            assertEquals(i, q.poll());
403          }
404          assertNull(q.poll());
405      }
406  
407      /**
408 <     * timed pool with zero timeout succeeds when non-empty, else times out
408 >     * timed poll with zero timeout succeeds when non-empty, else times out
409       */
410      public void testTimedPoll0() throws InterruptedException {
411          PriorityBlockingQueue q = populatedQueue(SIZE);
412          for (int i = 0; i < SIZE; ++i) {
413 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
413 >            assertEquals(i, q.poll(0, MILLISECONDS));
414          }
415          assertNull(q.poll(0, MILLISECONDS));
416      }
417  
418      /**
419 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
419 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
420       */
421      public void testTimedPoll() throws InterruptedException {
422 <        PriorityBlockingQueue q = populatedQueue(SIZE);
422 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
425 <        }
426 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
424 >            long startTime = System.nanoTime();
425 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
426 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
427 >        }
428 >        long startTime = System.nanoTime();
429 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
430 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
431 >        checkEmpty(q);
432      }
433  
434      /**
# Line 426 | Line 436 | public class PriorityBlockingQueueTest e
436       * returning timeout status
437       */
438      public void testInterruptedTimedPoll() throws InterruptedException {
439 <        Thread t = new Thread(new CheckedRunnable() {
439 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
440 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
441 >        Thread t = newStartedThread(new CheckedRunnable() {
442              public void realRun() throws InterruptedException {
431                PriorityBlockingQueue q = populatedQueue(SIZE);
443                  for (int i = 0; i < SIZE; ++i) {
444 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
444 >                    long t0 = System.nanoTime();
445 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
446 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
447                  }
448 <                try {
449 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
437 <                    shouldThrow();
438 <                } catch (InterruptedException success) {}
439 <            }});
440 <
441 <        t.start();
442 <        Thread.sleep(SHORT_DELAY_MS);
443 <        t.interrupt();
444 <        t.join();
445 <    }
446 <
447 <    /**
448 <     *  timed poll before a delayed offer fails; after offer succeeds;
449 <     *  on interruption throws
450 <     */
451 <    public void testTimedPollWithOffer() throws InterruptedException {
452 <        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
453 <        Thread t = new Thread(new CheckedRunnable() {
454 <            public void realRun() throws InterruptedException {
455 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
456 <                assertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
448 >                long t0 = System.nanoTime();
449 >                aboutToWait.countDown();
450                  try {
451                      q.poll(LONG_DELAY_MS, MILLISECONDS);
452 <                    threadShouldThrow();
453 <                } catch (InterruptedException success) {}
452 >                    shouldThrow();
453 >                } catch (InterruptedException success) {
454 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
455 >                }
456              }});
457  
458 <        t.start();
459 <        Thread.sleep(SMALL_DELAY_MS);
465 <        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
458 >        aboutToWait.await();
459 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
460          t.interrupt();
461 <        t.join();
461 >        awaitTermination(t, MEDIUM_DELAY_MS);
462      }
463  
470
464      /**
465       * peek returns next element, or null if empty
466       */
467      public void testPeek() {
468          PriorityBlockingQueue q = populatedQueue(SIZE);
469          for (int i = 0; i < SIZE; ++i) {
470 <            assertEquals(i, ((Integer)q.peek()).intValue());
471 <            q.poll();
470 >            assertEquals(i, q.peek());
471 >            assertEquals(i, q.poll());
472              assertTrue(q.peek() == null ||
473 <                       i != ((Integer)q.peek()).intValue());
473 >                       !q.peek().equals(i));
474          }
475          assertNull(q.peek());
476      }
# Line 488 | Line 481 | public class PriorityBlockingQueueTest e
481      public void testElement() {
482          PriorityBlockingQueue q = populatedQueue(SIZE);
483          for (int i = 0; i < SIZE; ++i) {
484 <            assertEquals(i, ((Integer)q.element()).intValue());
485 <            q.poll();
484 >            assertEquals(i, q.element());
485 >            assertEquals(i, q.poll());
486          }
487          try {
488              q.element();
# Line 503 | Line 496 | public class PriorityBlockingQueueTest e
496      public void testRemove() {
497          PriorityBlockingQueue q = populatedQueue(SIZE);
498          for (int i = 0; i < SIZE; ++i) {
499 <            assertEquals(i, ((Integer)q.remove()).intValue());
499 >            assertEquals(i, q.remove());
500          }
501          try {
502              q.remove();
# Line 517 | Line 510 | public class PriorityBlockingQueueTest e
510      public void testRemoveElement() {
511          PriorityBlockingQueue q = populatedQueue(SIZE);
512          for (int i = 1; i < SIZE; i+=2) {
513 <            assertTrue(q.remove(new Integer(i)));
513 >            assertTrue(q.contains(i));
514 >            assertTrue(q.remove(i));
515 >            assertFalse(q.contains(i));
516 >            assertTrue(q.contains(i-1));
517          }
518          for (int i = 0; i < SIZE; i+=2) {
519 <            assertTrue(q.remove(new Integer(i)));
520 <            assertFalse(q.remove(new Integer(i+1)));
519 >            assertTrue(q.contains(i));
520 >            assertTrue(q.remove(i));
521 >            assertFalse(q.contains(i));
522 >            assertFalse(q.remove(i+1));
523 >            assertFalse(q.contains(i+1));
524          }
525          assertTrue(q.isEmpty());
526      }
# Line 603 | Line 602 | public class PriorityBlockingQueueTest e
602      }
603  
604      /**
605 <     *  toArray contains all elements
605 >     * toArray contains all elements
606       */
607      public void testToArray() throws InterruptedException {
608          PriorityBlockingQueue q = populatedQueue(SIZE);
609          Object[] o = q.toArray();
610          Arrays.sort(o);
611          for (int i = 0; i < o.length; i++)
612 <            assertEquals(o[i], q.take());
612 >            assertSame(o[i], q.take());
613      }
614  
615      /**
616       * toArray(a) contains all elements
617       */
618      public void testToArray2() throws InterruptedException {
619 <        PriorityBlockingQueue q = populatedQueue(SIZE);
619 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
620          Integer[] ints = new Integer[SIZE];
621 <        ints = (Integer[])q.toArray(ints);
621 >        Integer[] array = q.toArray(ints);
622 >        assertSame(ints, array);
623          Arrays.sort(ints);
624          for (int i = 0; i < ints.length; i++)
625 <            assertEquals(ints[i], q.take());
625 >            assertSame(ints[i], q.take());
626      }
627  
628      /**
629 <     * toArray(null) throws NPE
629 >     * toArray(null) throws NullPointerException
630       */
631 <    public void testToArray_BadArg() {
631 >    public void testToArray_NullArg() {
632 >        PriorityBlockingQueue q = populatedQueue(SIZE);
633          try {
634 <            PriorityBlockingQueue q = populatedQueue(SIZE);
634 <            Object o[] = q.toArray(null);
634 >            q.toArray(null);
635              shouldThrow();
636          } catch (NullPointerException success) {}
637      }
638  
639      /**
640 <     * toArray with incompatible array type throws CCE
640 >     * toArray(incompatible array type) throws ArrayStoreException
641       */
642      public void testToArray1_BadArg() {
643 +        PriorityBlockingQueue q = populatedQueue(SIZE);
644          try {
645 <            PriorityBlockingQueue q = populatedQueue(SIZE);
645 <            Object o[] = q.toArray(new String[10] );
645 >            q.toArray(new String[10]);
646              shouldThrow();
647          } catch (ArrayStoreException success) {}
648      }
# Line 664 | Line 664 | public class PriorityBlockingQueueTest e
664      /**
665       * iterator.remove removes current element
666       */
667 <    public void testIteratorRemove () {
667 >    public void testIteratorRemove() {
668          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
669          q.add(new Integer(2));
670          q.add(new Integer(1));
# Line 680 | Line 680 | public class PriorityBlockingQueueTest e
680          assertFalse(it.hasNext());
681      }
682  
683
683      /**
684       * toString contains toStrings of elements
685       */
# Line 688 | Line 687 | public class PriorityBlockingQueueTest e
687          PriorityBlockingQueue q = populatedQueue(SIZE);
688          String s = q.toString();
689          for (int i = 0; i < SIZE; ++i) {
690 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
690 >            assertTrue(s.contains(String.valueOf(i)));
691          }
692      }
693  
694      /**
695 <     * offer transfers elements across Executor tasks
695 >     * timed poll transfers elements across Executor tasks
696       */
697      public void testPollInExecutor() {
698          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
699 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
700          ExecutorService executor = Executors.newFixedThreadPool(2);
701          executor.execute(new CheckedRunnable() {
702              public void realRun() throws InterruptedException {
703 <                threadAssertNull(q.poll());
704 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705 <                threadAssertTrue(q.isEmpty());
703 >                assertNull(q.poll());
704 >                threadsStarted.await();
705 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
706 >                checkEmpty(q);
707              }});
708  
709          executor.execute(new CheckedRunnable() {
710              public void realRun() throws InterruptedException {
711 <                Thread.sleep(SMALL_DELAY_MS);
712 <                q.put(new Integer(1));
711 >                threadsStarted.await();
712 >                q.put(one);
713              }});
714  
715          joinPool(executor);
# Line 821 | Line 822 | public class PriorityBlockingQueueTest e
822      }
823  
824      /**
825 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
825 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
826       */
827      public void testDrainToN() {
828          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
# Line 830 | Line 831 | public class PriorityBlockingQueueTest e
831                  assertTrue(q.offer(new Integer(j)));
832              ArrayList l = new ArrayList();
833              q.drainTo(l, i);
834 <            int k = (i < SIZE)? i : SIZE;
834 >            int k = (i < SIZE) ? i : SIZE;
835              assertEquals(l.size(), k);
836              assertEquals(q.size(), SIZE-k);
837              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines