--- jsr166/src/test/tck/SynchronousQueueTest.java 2009/11/16 05:30:08 1.12 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2009/11/19 01:15:42 1.13 @@ -9,6 +9,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; public class SynchronousQueueTest extends JSR166TestCase { @@ -51,7 +52,7 @@ public class SynchronousQueueTest extend SynchronousQueue q = new SynchronousQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -62,7 +63,7 @@ public class SynchronousQueueTest extend SynchronousQueue q = new SynchronousQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -82,8 +83,7 @@ public class SynchronousQueueTest extend assertEquals(0, q.remainingCapacity()); q.add(one); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -94,8 +94,7 @@ public class SynchronousQueueTest extend SynchronousQueue q = new SynchronousQueue(); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -106,8 +105,7 @@ public class SynchronousQueueTest extend SynchronousQueue q = new SynchronousQueue(); q.addAll(q); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -119,8 +117,7 @@ public class SynchronousQueueTest extend Integer[] ints = new Integer[1]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * addAll throws ISE if no active taker @@ -133,243 +130,181 @@ public class SynchronousQueueTest extend ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (IllegalStateException success) {} + } catch (IllegalStateException success) {} } /** * put(null) throws NPE */ - public void testPutNull() { + public void testPutNull() throws InterruptedException { try { SynchronousQueue q = new SynchronousQueue(); q.put(null); shouldThrow(); - } - catch (NullPointerException success) { - } - catch (InterruptedException ie) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** * put blocks interruptibly if no active taker */ - public void testBlockingPut() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - SynchronousQueue q = new SynchronousQueue(); - q.put(zero); - threadShouldThrow(); - } catch (InterruptedException ie) { - } - }}); + public void testBlockingPut() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(); + q.put(zero); + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * put blocks waiting for take */ - public void testPutWithTake() { + public void testPutWithTake() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException e) { - assertTrue(added >= 1); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + int added = 0; + 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) { + assertTrue(added >= 1); } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + q.take(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed offer times out if elements not taken */ - public void testTimedOffer() { + public void testTimedOffer() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + }}); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + t.start(); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); } /** * take blocks interruptibly when empty */ - public void testTakeFromEmpty() { + public void testTakeFromEmpty() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + q.take(); + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * put blocks interruptibly if no active taker */ - public void testFairBlockingPut() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - SynchronousQueue q = new SynchronousQueue(true); - q.put(zero); - threadShouldThrow(); - } catch (InterruptedException ie) { - } - }}); + public void testFairBlockingPut() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(true); + q.put(zero); + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * put blocks waiting for take */ - public void testFairPutWithTake() { + public void testFairPutWithTake() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException e) { - assertTrue(added >= 1); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + int added = 0; + 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) { + assertTrue(added >= 1); } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + q.take(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed offer times out if elements not taken */ - public void testFairTimedOffer() { + public void testFairTimedOffer() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new Runnable() { - public void run() { - try { - - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + }}); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + t.start(); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); } /** * take blocks interruptibly when empty */ - public void testFairTakeFromEmpty() { + public void testFairTakeFromEmpty() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + q.take(); + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** @@ -383,127 +318,97 @@ public class SynchronousQueueTest extend /** * timed pool with zero timeout times out if no active taker */ - public void testTimedPoll0() { - try { - SynchronousQueue q = new SynchronousQueue(); - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); - } + public void testTimedPoll0() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(); + assertNull(q.poll(0, MILLISECONDS)); } /** * timed pool with nonzero timeout times out if no active taker */ - public void testTimedPoll() { - try { - SynchronousQueue q = new SynchronousQueue(); - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); - } + public void testTimedPoll() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(); + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - SynchronousQueue q = new SynchronousQueue(); - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success) { - } - }}); + public void testInterruptedTimedPoll() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(); + q.poll(SMALL_DELAY_MS, MILLISECONDS); + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed poll before a delayed offer fails; after offer succeeds; * on interruption throws */ - public void testTimedPollWithOffer() { + public void testTimedPollWithOffer() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testFairInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - SynchronousQueue q = new SynchronousQueue(true); - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success) { - } - }}); + public void testFairInterruptedTimedPoll() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + SynchronousQueue q = new SynchronousQueue(true); + q.poll(SMALL_DELAY_MS, MILLISECONDS); + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed poll before a delayed offer fails; after offer succeeds; * on interruption throws */ - public void testFairTimedPollWithOffer() { + public void testFairTimedPollWithOffer() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); } @@ -523,8 +428,7 @@ public class SynchronousQueueTest extend try { q.element(); shouldThrow(); - } - catch (NoSuchElementException success) {} + } catch (NoSuchElementException success) {} } /** @@ -639,8 +543,7 @@ public class SynchronousQueueTest extend try { Object x = it.next(); shouldThrow(); - } - catch (NoSuchElementException success) {} + } catch (NoSuchElementException success) {} } /** @@ -652,8 +555,7 @@ public class SynchronousQueueTest extend try { it.remove(); shouldThrow(); - } - catch (IllegalStateException success) {} + } catch (IllegalStateException success) {} } /** @@ -674,30 +576,18 @@ public class SynchronousQueueTest extend ExecutorService executor = Executors.newFixedThreadPool(2); final Integer one = new Integer(1); - executor.execute(new Runnable() { - public void run() { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { threadAssertFalse(q.offer(one)); - try { - threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); + threadAssertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + threadAssertEquals(one, q.take()); + }}); joinPool(executor); @@ -709,30 +599,18 @@ public class SynchronousQueueTest extend public void testPollInExecutor() { final SynchronousQueue q = new SynchronousQueue(); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(new Integer(1)); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + threadAssertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + q.put(new Integer(1)); + }}); joinPool(executor); } @@ -740,24 +618,19 @@ public class SynchronousQueueTest extend /** * a deserialized serialized queue is usable */ - public void testSerialization() { + public void testSerialization() throws Exception { SynchronousQueue q = new SynchronousQueue(); - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - SynchronousQueue r = (SynchronousQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); - } + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(q); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + SynchronousQueue r = (SynchronousQueue)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.remove(), r.remove()); } /** @@ -768,8 +641,7 @@ public class SynchronousQueueTest extend try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -780,8 +652,7 @@ public class SynchronousQueueTest extend try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -798,30 +669,22 @@ public class SynchronousQueueTest extend /** * drainTo empties queue, unblocking a waiting put. */ - public void testDrainToWithActivePut() { + public void testDrainToWithActivePut() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Integer(1)); - } catch (InterruptedException ie) { - threadUnexpectedException(); - } - } - }); - try { - t.start(); - ArrayList l = new ArrayList(); - Thread.sleep(SHORT_DELAY_MS); - q.drainTo(l); - assertTrue(l.size() <= 1); - if (l.size() > 0) - assertEquals(l.get(0), new Integer(1)); - t.join(); - assertTrue(l.size() <= 1); - } catch (Exception e) { - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Integer(1)); + }}); + + t.start(); + ArrayList l = new ArrayList(); + Thread.sleep(SHORT_DELAY_MS); + q.drainTo(l); + assertTrue(l.size() <= 1); + if (l.size() > 0) + assertEquals(l.get(0), new Integer(1)); + t.join(); + assertTrue(l.size() <= 1); } /** @@ -832,8 +695,7 @@ public class SynchronousQueueTest extend try { q.drainTo(null, 0); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -851,44 +713,30 @@ public class SynchronousQueueTest extend /** * drainTo(c, n) empties up to n elements of queue into c */ - public void testDrainToN() { + public void testDrainToN() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t1 = new Thread(new Runnable() { - public void run() { - try { - q.put(one); - } catch (InterruptedException ie) { - threadUnexpectedException(); - } - } - }); - Thread t2 = new Thread(new Runnable() { - public void run() { - try { - q.put(two); - } catch (InterruptedException ie) { - threadUnexpectedException(); - } - } - }); + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(one); + }}); + + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(two); + }}); - try { - t1.start(); - t2.start(); - ArrayList l = new ArrayList(); - Thread.sleep(SHORT_DELAY_MS); - q.drainTo(l, 1); - assertTrue(l.size() == 1); - q.drainTo(l, 1); - assertTrue(l.size() == 2); - assertTrue(l.contains(one)); - assertTrue(l.contains(two)); - t1.join(); - t2.join(); - } catch (Exception e) { - unexpectedException(); - } + t1.start(); + t2.start(); + ArrayList l = new ArrayList(); + Thread.sleep(SHORT_DELAY_MS); + q.drainTo(l, 1); + assertTrue(l.size() == 1); + q.drainTo(l, 1); + assertTrue(l.size() == 2); + assertTrue(l.contains(one)); + assertTrue(l.contains(two)); + t1.join(); + t2.join(); } - }