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.46 by jsr166, Sat May 21 06:24:33 2011 UTC vs.
Revision 1.47 by jsr166, Fri May 27 20:07:24 2011 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
# 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 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 <        delay(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 <        delay(SHORT_DELAY_MS);
369 >        await(pleaseTake);
370          assertEquals(q.remainingCapacity(), 0);
371          assertEquals(0, q.take());
372 <        delay(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 381 | Line 398 | public class ArrayBlockingQueueTest exte
398              }});
399  
400          await(pleaseInterrupt);
401 +        assertThreadStaysAlive(t);
402          t.interrupt();
403          awaitTermination(t);
404      }
# Line 400 | 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 <        delay(SHORT_DELAY_MS);
443 >        await(pleaseInterrupt);
444 >        assertThreadStaysAlive(t);
445          t.interrupt();
446 <        t.join();
446 >        awaitTermination(t);
447      }
448  
420
449      /**
450       * poll succeeds unless empty
451       */
# Line 438 | 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 446 | 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 661 | Line 695 | public class ArrayBlockingQueueTest exte
695          } catch (ArrayStoreException success) {}
696      }
697  
664
698      /**
699       * iterator iterates through all elements
700       */
# Line 725 | Line 758 | public class ArrayBlockingQueueTest exte
758          assertEquals(0, q.size());
759      }
760  
728
761      /**
762       * toString contains toStrings of elements
763       */
# Line 733 | 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  
740
772      /**
773       * offer transfers elements across Executor tasks
774       */
# Line 746 | 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 <                delay(SMALL_DELAY_MS);
791 >                threadsStarted.await();
792 >                assertEquals(0, q.remainingCapacity());
793                  assertSame(one, q.take());
794              }});
795  
# Line 763 | 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 <                delay(SMALL_DELAY_MS);
816 >                threadsStarted.await();
817                  q.put(one);
818              }});
819  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines