--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2017/05/13 22:49:01 1.70 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2018/05/28 21:19:50 1.78 @@ -59,8 +59,7 @@ public class LinkedBlockingQueueTest ext * Integers 0 ... n - 1. */ private static LinkedBlockingQueue populatedQueue(int n) { - LinkedBlockingQueue q = - new LinkedBlockingQueue(n); + LinkedBlockingQueue q = new LinkedBlockingQueue<>(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -314,6 +313,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); @@ -343,14 +349,24 @@ public class LinkedBlockingQueueTest ext public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); + long startTime = System.nanoTime(); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); @@ -444,23 +460,31 @@ 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) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - await(aboutToWait); + await(pleaseInterrupt); assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); @@ -605,9 +629,11 @@ public class LinkedBlockingQueueTest ext */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.poll()); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + for (Object o : a) + assertSame(o, q.poll()); + assertTrue(q.isEmpty()); } /** @@ -618,8 +644,9 @@ public class LinkedBlockingQueueTest ext Integer[] ints = new Integer[SIZE]; Integer[] array = q.toArray(ints); assertSame(ints, array); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.poll()); + for (Integer o : ints) + assertSame(o, q.poll()); + assertTrue(q.isEmpty()); } /** @@ -771,7 +798,7 @@ public class LinkedBlockingQueueTest ext } /** - * A deserialized serialized queue has same elements in same order + * A deserialized/reserialized queue has same elements in same order */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE);