--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/05/21 06:24:33 1.43 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/05/27 20:07:24 1.44 @@ -36,7 +36,6 @@ public class LinkedBlockingQueueTest ext new Bounded().testSuite()); } - /** * Create a queue of given size containing consecutive * Integers 0 ... n. @@ -303,49 +302,68 @@ public class LinkedBlockingQueueTest ext */ public void testBlockingPut() throws InterruptedException { final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) q.put(i); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - delay(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); } /** - * put blocks waiting for take when full + * put blocks interruptibly waiting for take when full */ public void testPutWithTake() throws InterruptedException { final int capacity = 2; final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < capacity + 1; i++) + for (int i = 0; i < capacity; i++) q.put(i); + pleaseTake.countDown(); + q.put(86); + + pleaseInterrupt.countDown(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - delay(SHORT_DELAY_MS); + await(pleaseTake); assertEquals(q.remainingCapacity(), 0); assertEquals(0, q.take()); - delay(SHORT_DELAY_MS); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); assertEquals(q.remainingCapacity(), 0); } @@ -370,6 +388,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); } @@ -388,22 +407,33 @@ public class LinkedBlockingQueueTest ext * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final LinkedBlockingQueue q = populatedQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.take()); } + + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - delay(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -432,11 +462,16 @@ public class LinkedBlockingQueueTest ext * timed poll with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() throws InterruptedException { - LinkedBlockingQueue q = populatedQueue(SIZE); + LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -668,7 +703,6 @@ public class LinkedBlockingQueueTest ext } catch (ArrayStoreException success) {} } - /** * iterator iterates through all elements */ @@ -699,7 +733,6 @@ public class LinkedBlockingQueueTest ext assertFalse(it.hasNext()); } - /** * iterator ordering is FIFO */ @@ -731,7 +764,6 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.size()); } - /** * toString contains toStrings of elements */ @@ -739,11 +771,10 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } - /** * offer transfers elements across Executor tasks */ @@ -752,16 +783,18 @@ public class LinkedBlockingQueueTest ext q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(q.offer(three)); - assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - delay(SMALL_DELAY_MS); + threadsStarted.await(); assertSame(one, q.take()); }}); @@ -769,21 +802,23 @@ public class LinkedBlockingQueueTest ext } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll()); - assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - delay(SMALL_DELAY_MS); + threadsStarted.await(); q.put(one); }});