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.39 by jsr166, Wed Nov 3 16:46:34 2010 UTC vs.
Revision 1.47 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   */
8  
9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
# Line 41 | Line 40 | public class ArrayBlockingQueueTest exte
40       * Create a queue of given size containing consecutive
41       * Integers 0 ... n.
42       */
43 <    private ArrayBlockingQueue populatedQueue(int n) {
44 <        ArrayBlockingQueue q = new ArrayBlockingQueue(n);
43 >    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
44 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
45          assertTrue(q.isEmpty());
46          for (int i = 0; i < n; i++)
47              assertTrue(q.offer(new Integer(i)));
# Line 228 | Line 227 | public class ArrayBlockingQueueTest exte
227          } catch (IllegalArgumentException success) {}
228      }
229  
231
230      /**
231       * addAll of a collection with null elements throws NPE
232       */
# Line 294 | Line 292 | public class ArrayBlockingQueueTest exte
292              q.put(null);
293              shouldThrow();
294          } catch (NullPointerException success) {}
295 <     }
295 >    }
296  
297      /**
298       * all elements successfully put are contained
# Line 314 | Line 312 | public class ArrayBlockingQueueTest exte
312       */
313      public void testBlockingPut() throws InterruptedException {
314          final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
315 <        Thread t = new Thread(new CheckedRunnable() {
315 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
316 >        Thread t = newStartedThread(new CheckedRunnable() {
317              public void realRun() throws InterruptedException {
318                  for (int i = 0; i < SIZE; ++i)
319                      q.put(i);
320                  assertEquals(SIZE, q.size());
321                  assertEquals(0, q.remainingCapacity());
322 +
323 +                Thread.currentThread().interrupt();
324                  try {
325                      q.put(99);
326                      shouldThrow();
327                  } catch (InterruptedException success) {}
328 +                assertFalse(Thread.interrupted());
329 +
330 +                pleaseInterrupt.countDown();
331 +                try {
332 +                    q.put(99);
333 +                    shouldThrow();
334 +                } catch (InterruptedException success) {}
335 +                assertFalse(Thread.interrupted());
336              }});
337  
338 <        t.start();
339 <        Thread.sleep(SHORT_DELAY_MS);
338 >        await(pleaseInterrupt);
339 >        assertThreadStaysAlive(t);
340          t.interrupt();
341 <        t.join();
341 >        awaitTermination(t);
342          assertEquals(SIZE, q.size());
343          assertEquals(0, q.remainingCapacity());
344      }
345  
346      /**
347 <     * put blocks waiting for take when full
347 >     * put blocks interruptibly waiting for take when full
348       */
349      public void testPutWithTake() throws InterruptedException {
350          final int capacity = 2;
351          final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity);
352 <        Thread t = new Thread(new CheckedRunnable() {
352 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
353 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
354 >        Thread t = newStartedThread(new CheckedRunnable() {
355              public void realRun() throws InterruptedException {
356 <                for (int i = 0; i < capacity + 1; i++)
356 >                for (int i = 0; i < capacity; i++)
357                      q.put(i);
358 +                pleaseTake.countDown();
359 +                q.put(86);
360 +
361 +                pleaseInterrupt.countDown();
362                  try {
363                      q.put(99);
364                      shouldThrow();
365                  } catch (InterruptedException success) {}
366 +                assertFalse(Thread.interrupted());
367              }});
368  
369 <        t.start();
354 <        Thread.sleep(SHORT_DELAY_MS);
369 >        await(pleaseTake);
370          assertEquals(q.remainingCapacity(), 0);
371          assertEquals(0, q.take());
372 <        Thread.sleep(SHORT_DELAY_MS);
372 >
373 >        await(pleaseInterrupt);
374 >        assertThreadStaysAlive(t);
375          t.interrupt();
376 <        t.join();
376 >        awaitTermination(t);
377          assertEquals(q.remainingCapacity(), 0);
378      }
379  
# Line 365 | Line 382 | public class ArrayBlockingQueueTest exte
382       */
383      public void testTimedOffer() throws InterruptedException {
384          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
385 <        Thread t = new Thread(new CheckedRunnable() {
385 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
386 >        Thread t = newStartedThread(new CheckedRunnable() {
387              public void realRun() throws InterruptedException {
388                  q.put(new Object());
389                  q.put(new Object());
390 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS));
390 >                long startTime = System.nanoTime();
391 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
392 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
393 >                pleaseInterrupt.countDown();
394                  try {
395 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
395 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
396                      shouldThrow();
397                  } catch (InterruptedException success) {}
398              }});
399  
400 <        t.start();
401 <        Thread.sleep(SHORT_DELAY_MS);
400 >        await(pleaseInterrupt);
401 >        assertThreadStaysAlive(t);
402          t.interrupt();
403 <        t.join();
403 >        awaitTermination(t);
404      }
405  
406      /**
# Line 397 | Line 418 | public class ArrayBlockingQueueTest exte
418       */
419      public void testBlockingTake() throws InterruptedException {
420          final ArrayBlockingQueue q = populatedQueue(SIZE);
421 <        Thread t = new Thread(new CheckedRunnable() {
421 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
422 >        Thread t = newStartedThread(new CheckedRunnable() {
423              public void realRun() throws InterruptedException {
424                  for (int i = 0; i < SIZE; ++i) {
425                      assertEquals(i, q.take());
426                  }
427 +
428 +                Thread.currentThread().interrupt();
429 +                try {
430 +                    q.take();
431 +                    shouldThrow();
432 +                } catch (InterruptedException success) {}
433 +                assertFalse(Thread.interrupted());
434 +
435 +                pleaseInterrupt.countDown();
436                  try {
437                      q.take();
438                      shouldThrow();
439                  } catch (InterruptedException success) {}
440 +                assertFalse(Thread.interrupted());
441              }});
442  
443 <        t.start();
444 <        Thread.sleep(SHORT_DELAY_MS);
443 >        await(pleaseInterrupt);
444 >        assertThreadStaysAlive(t);
445          t.interrupt();
446 <        t.join();
446 >        awaitTermination(t);
447      }
448  
417
449      /**
450       * poll succeeds unless empty
451       */
# Line 435 | Line 466 | public class ArrayBlockingQueueTest exte
466              assertEquals(i, q.poll(0, MILLISECONDS));
467          }
468          assertNull(q.poll(0, MILLISECONDS));
469 +        checkEmpty(q);
470      }
471  
472      /**
# Line 443 | Line 475 | public class ArrayBlockingQueueTest exte
475      public void testTimedPoll() throws InterruptedException {
476          ArrayBlockingQueue q = populatedQueue(SIZE);
477          for (int i = 0; i < SIZE; ++i) {
478 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
479 <        }
480 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
478 >            long startTime = System.nanoTime();
479 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
480 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
481 >        }
482 >        long startTime = System.nanoTime();
483 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
484 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
485 >        checkEmpty(q);
486      }
487  
488      /**
# Line 453 | Line 490 | public class ArrayBlockingQueueTest exte
490       * returning timeout status
491       */
492      public void testInterruptedTimedPoll() throws InterruptedException {
493 <        Thread t = new Thread(new CheckedRunnable() {
493 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
494 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
495 >        Thread t = newStartedThread(new CheckedRunnable() {
496              public void realRun() throws InterruptedException {
458                ArrayBlockingQueue q = populatedQueue(SIZE);
497                  for (int i = 0; i < SIZE; ++i) {
498 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));;
498 >                    long t0 = System.nanoTime();
499 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
500 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
501                  }
502 +                long t0 = System.nanoTime();
503 +                aboutToWait.countDown();
504                  try {
505 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
505 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
506                      shouldThrow();
507 <                } catch (InterruptedException success) {}
507 >                } catch (InterruptedException success) {
508 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
509 >                }
510              }});
511  
512 <        t.start();
513 <        Thread.sleep(SHORT_DELAY_MS);
512 >        aboutToWait.await();
513 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
514          t.interrupt();
515 <        t.join();
515 >        awaitTermination(t, MEDIUM_DELAY_MS);
516 >        checkEmpty(q);
517      }
518  
519      /**
# Line 607 | Line 652 | public class ArrayBlockingQueueTest exte
652      }
653  
654      /**
655 <     * toArray contains all elements
655 >     * toArray contains all elements in FIFO order
656       */
657 <    public void testToArray() throws InterruptedException {
657 >    public void testToArray() {
658          ArrayBlockingQueue q = populatedQueue(SIZE);
659          Object[] o = q.toArray();
660          for (int i = 0; i < o.length; i++)
661 <            assertEquals(o[i], q.take());
661 >            assertSame(o[i], q.poll());
662      }
663  
664      /**
665 <     * toArray(a) contains all elements
665 >     * toArray(a) contains all elements in FIFO order
666       */
667 <    public void testToArray2() throws InterruptedException {
668 <        ArrayBlockingQueue q = populatedQueue(SIZE);
667 >    public void testToArray2() {
668 >        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
669          Integer[] ints = new Integer[SIZE];
670 <        ints = (Integer[])q.toArray(ints);
670 >        Integer[] array = q.toArray(ints);
671 >        assertSame(ints, array);
672          for (int i = 0; i < ints.length; i++)
673 <            assertEquals(ints[i], q.take());
673 >            assertSame(ints[i], q.poll());
674      }
675  
676      /**
# Line 649 | Line 695 | public class ArrayBlockingQueueTest exte
695          } catch (ArrayStoreException success) {}
696      }
697  
652
698      /**
699       * iterator iterates through all elements
700       */
# Line 713 | Line 758 | public class ArrayBlockingQueueTest exte
758          assertEquals(0, q.size());
759      }
760  
716
761      /**
762       * toString contains toStrings of elements
763       */
# Line 721 | Line 765 | public class ArrayBlockingQueueTest exte
765          ArrayBlockingQueue q = populatedQueue(SIZE);
766          String s = q.toString();
767          for (int i = 0; i < SIZE; ++i) {
768 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
768 >            assertTrue(s.contains(String.valueOf(i)));
769          }
770      }
771  
728
772      /**
773       * offer transfers elements across Executor tasks
774       */
# Line 734 | Line 777 | public class ArrayBlockingQueueTest exte
777          q.add(one);
778          q.add(two);
779          ExecutorService executor = Executors.newFixedThreadPool(2);
780 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
781          executor.execute(new CheckedRunnable() {
782              public void realRun() throws InterruptedException {
783                  assertFalse(q.offer(three));
784 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
784 >                threadsStarted.await();
785 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
786                  assertEquals(0, q.remainingCapacity());
787              }});
788  
789          executor.execute(new CheckedRunnable() {
790              public void realRun() throws InterruptedException {
791 <                Thread.sleep(SMALL_DELAY_MS);
791 >                threadsStarted.await();
792 >                assertEquals(0, q.remainingCapacity());
793                  assertSame(one, q.take());
794              }});
795  
# Line 751 | Line 797 | public class ArrayBlockingQueueTest exte
797      }
798  
799      /**
800 <     * poll retrieves elements across Executor threads
800 >     * timed poll retrieves elements across Executor threads
801       */
802      public void testPollInExecutor() {
803          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
804 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
805          ExecutorService executor = Executors.newFixedThreadPool(2);
806          executor.execute(new CheckedRunnable() {
807              public void realRun() throws InterruptedException {
808                  assertNull(q.poll());
809 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
810 <                assertTrue(q.isEmpty());
809 >                threadsStarted.await();
810 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
811 >                checkEmpty(q);
812              }});
813  
814          executor.execute(new CheckedRunnable() {
815              public void realRun() throws InterruptedException {
816 <                Thread.sleep(SMALL_DELAY_MS);
816 >                threadsStarted.await();
817                  q.put(one);
818              }});
819  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines