--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/21 10:25:05 1.18 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/22 00:17:37 1.22 @@ -284,56 +284,51 @@ public class LinkedBlockingQueueTest ext * put blocks interruptibly if full */ public void testBlockingPut() throws InterruptedException { + final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * put blocks 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() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.put(i); try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -341,13 +336,16 @@ public class LinkedBlockingQueueTest ext */ public void testTimedOffer() throws InterruptedException { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -385,14 +383,17 @@ public class LinkedBlockingQueueTest ext * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.take()).intValue()); } - q.take(); - }}; + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -400,7 +401,6 @@ public class LinkedBlockingQueueTest ext t.join(); } - /** * poll succeeds unless empty */ @@ -439,14 +439,17 @@ public class LinkedBlockingQueueTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); } - q.poll(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -460,12 +463,15 @@ public class LinkedBlockingQueueTest ext */ public void testTimedPollWithOffer() throws InterruptedException { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}; + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS);