--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2016/10/16 20:44:18 1.64 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2017/05/14 04:02:06 1.74 @@ -41,16 +41,24 @@ public class LinkedBlockingQueueTest ext } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return LinkedBlockingQueue.class; } + public Collection emptyCollection() { return new LinkedBlockingQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(LinkedBlockingQueueTest.class, new Unbounded().testSuite(), - new Bounded().testSuite()); + new Bounded().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** * Returns a new queue of given size containing consecutive * Integers 0 ... n - 1. */ - private LinkedBlockingQueue populatedQueue(int n) { + private static LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); @@ -284,7 +292,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(SIZE, q.size()); @@ -306,6 +314,13 @@ public class LinkedBlockingQueueTest ext pleaseTake.countDown(); q.put(86); + Thread.currentThread().interrupt(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { q.put(99); @@ -319,7 +334,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.take()); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -343,10 +358,11 @@ public class LinkedBlockingQueueTest ext q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -369,9 +385,7 @@ public class LinkedBlockingQueueTest ext 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()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); Thread.currentThread().interrupt(); try { @@ -389,7 +403,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -438,24 +452,32 @@ public class LinkedBlockingQueueTest ext */ public void testInterruptedTimedPoll() throws InterruptedException { final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { long startTime = System.nanoTime(); - for (int i = 0; i < SIZE; ++i) { + for (int i = 0; i < SIZE; i++) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - } - aboutToWait.countDown(); + + Thread.currentThread().interrupt(); try { q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - await(aboutToWait); - waitForThreadToEnterWaitState(t); + await(pleaseInterrupt); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q);