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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.34 by jsr166, Tue Oct 19 00:41:14 2010 UTC vs.
Revision 1.45 by dl, Fri May 6 11:22:07 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 41 | Line 41 | public class ArrayBlockingQueueTest exte
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private ArrayBlockingQueue populatedQueue(int n) {
45 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
44 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
45 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
46          assertTrue(q.isEmpty());
47          for (int i = 0; i < n; i++)
48              assertTrue(q.offer(new Integer(i)));
# Line 294 | Line 294 | public class ArrayBlockingQueueTest exte
294              q.put(null);
295              shouldThrow();
296          } catch (NullPointerException success) {}
297 <     }
297 >    }
298  
299      /**
300       * all elements successfully put are contained
# Line 327 | Line 327 | public class ArrayBlockingQueueTest exte
327              }});
328  
329          t.start();
330 <        Thread.sleep(SHORT_DELAY_MS);
330 >        delay(SHORT_DELAY_MS);
331          t.interrupt();
332          t.join();
333          assertEquals(SIZE, q.size());
# Line 351 | Line 351 | public class ArrayBlockingQueueTest exte
351              }});
352  
353          t.start();
354 <        Thread.sleep(SHORT_DELAY_MS);
354 >        delay(SHORT_DELAY_MS);
355          assertEquals(q.remainingCapacity(), 0);
356          assertEquals(0, q.take());
357 <        Thread.sleep(SHORT_DELAY_MS);
357 >        delay(SHORT_DELAY_MS);
358          t.interrupt();
359          t.join();
360          assertEquals(q.remainingCapacity(), 0);
# Line 377 | Line 377 | public class ArrayBlockingQueueTest exte
377              }});
378  
379          t.start();
380 <        Thread.sleep(SHORT_DELAY_MS);
380 >        delay(SHORT_DELAY_MS);
381          t.interrupt();
382          t.join();
383      }
# Line 393 | Line 393 | public class ArrayBlockingQueueTest exte
393      }
394  
395      /**
396     * take blocks interruptibly when empty
397     */
398    public void testTakeFromEmpty() throws InterruptedException {
399        final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
400        Thread t = new ThreadShouldThrow(InterruptedException.class) {
401            public void realRun() throws InterruptedException {
402                q.take();
403            }};
404
405        t.start();
406        Thread.sleep(SHORT_DELAY_MS);
407        t.interrupt();
408        t.join();
409    }
410
411    /**
396       * Take removes existing elements until empty, then blocks interruptibly
397       */
398      public void testBlockingTake() throws InterruptedException {
# Line 425 | Line 409 | public class ArrayBlockingQueueTest exte
409              }});
410  
411          t.start();
412 <        Thread.sleep(SHORT_DELAY_MS);
412 >        delay(SHORT_DELAY_MS);
413          t.interrupt();
414          t.join();
415      }
# Line 443 | Line 427 | public class ArrayBlockingQueueTest exte
427      }
428  
429      /**
430 <     * timed pool with zero timeout succeeds when non-empty, else times out
430 >     * timed poll with zero timeout succeeds when non-empty, else times out
431       */
432      public void testTimedPoll0() throws InterruptedException {
433          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 454 | Line 438 | public class ArrayBlockingQueueTest exte
438      }
439  
440      /**
441 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
441 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
442       */
443      public void testTimedPoll() throws InterruptedException {
444          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 469 | Line 453 | public class ArrayBlockingQueueTest exte
453       * returning timeout status
454       */
455      public void testInterruptedTimedPoll() throws InterruptedException {
456 <        Thread t = new Thread(new CheckedRunnable() {
456 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
457 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
458 >        Thread t = newStartedThread(new CheckedRunnable() {
459              public void realRun() throws InterruptedException {
474                ArrayBlockingQueue q = populatedQueue(SIZE);
460                  for (int i = 0; i < SIZE; ++i) {
461 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
461 >                    long t0 = System.nanoTime();
462 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
463 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
464                  }
465 +                long t0 = System.nanoTime();
466 +                aboutToWait.countDown();
467                  try {
468 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
468 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
469                      shouldThrow();
470 <                } catch (InterruptedException success) {}
470 >                } catch (InterruptedException success) {
471 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
472 >                }
473              }});
474  
475 <        t.start();
476 <        Thread.sleep(SHORT_DELAY_MS);
475 >        aboutToWait.await();
476 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
477          t.interrupt();
478 <        t.join();
478 >        awaitTermination(t, MEDIUM_DELAY_MS);
479 >        checkEmpty(q);
480      }
481  
482      /**
# Line 623 | Line 615 | public class ArrayBlockingQueueTest exte
615      }
616  
617      /**
618 <     * toArray contains all elements
618 >     * toArray contains all elements in FIFO order
619       */
620 <    public void testToArray() throws InterruptedException {
620 >    public void testToArray() {
621          ArrayBlockingQueue q = populatedQueue(SIZE);
622          Object[] o = q.toArray();
623          for (int i = 0; i < o.length; i++)
624 <            assertEquals(o[i], q.take());
624 >            assertSame(o[i], q.poll());
625      }
626  
627      /**
628 <     * toArray(a) contains all elements
628 >     * toArray(a) contains all elements in FIFO order
629       */
630 <    public void testToArray2() throws InterruptedException {
631 <        ArrayBlockingQueue q = populatedQueue(SIZE);
630 >    public void testToArray2() {
631 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
632          Integer[] ints = new Integer[SIZE];
633 <        ints = (Integer[])q.toArray(ints);
633 >        Integer[] array = q.toArray(ints);
634 >        assertSame(ints, array);
635          for (int i = 0; i < ints.length; i++)
636 <            assertEquals(ints[i], q.take());
636 >            assertSame(ints[i], q.poll());
637      }
638  
639      /**
640 <     * toArray(null) throws NPE
640 >     * toArray(null) throws NullPointerException
641       */
642 <    public void testToArray_BadArg() {
642 >    public void testToArray_NullArg() {
643          ArrayBlockingQueue q = populatedQueue(SIZE);
644          try {
645 <            Object o[] = q.toArray(null);
645 >            q.toArray(null);
646              shouldThrow();
647          } catch (NullPointerException success) {}
648      }
649  
650      /**
651 <     * toArray with incompatible array type throws CCE
651 >     * toArray(incompatible array type) throws ArrayStoreException
652       */
653      public void testToArray1_BadArg() {
654          ArrayBlockingQueue q = populatedQueue(SIZE);
655          try {
656 <            Object o[] = q.toArray(new String[10]);
656 >            q.toArray(new String[10]);
657              shouldThrow();
658          } catch (ArrayStoreException success) {}
659      }
# Line 759 | Line 752 | public class ArrayBlockingQueueTest exte
752  
753          executor.execute(new CheckedRunnable() {
754              public void realRun() throws InterruptedException {
755 <                Thread.sleep(SMALL_DELAY_MS);
755 >                delay(SMALL_DELAY_MS);
756                  assertSame(one, q.take());
757              }});
758  
# Line 781 | Line 774 | public class ArrayBlockingQueueTest exte
774  
775          executor.execute(new CheckedRunnable() {
776              public void realRun() throws InterruptedException {
777 <                Thread.sleep(SMALL_DELAY_MS);
777 >                delay(SMALL_DELAY_MS);
778                  q.put(one);
779              }});
780  
# Line 905 | Line 898 | public class ArrayBlockingQueueTest exte
898                  assertTrue(q.offer(new Integer(j)));
899              ArrayList l = new ArrayList();
900              q.drainTo(l, i);
901 <            int k = (i < SIZE)? i : SIZE;
901 >            int k = (i < SIZE) ? i : SIZE;
902              assertEquals(l.size(), k);
903              assertEquals(q.size(), SIZE-k);
904              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines