--- jsr166/src/test/tck/ExchangerTest.java 2009/11/16 04:57:10 1.7 +++ jsr166/src/test/tck/ExchangerTest.java 2011/05/29 06:48:42 1.20 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -9,14 +9,15 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; public class ExchangerTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(ExchangerTest.class); + return new TestSuite(ExchangerTest.class); } /** @@ -24,38 +25,19 @@ public class ExchangerTest extends JSR16 */ public void testExchange() { final Exchanger e = new Exchanger(); - Thread t1 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(one); - threadAssertEquals(v, two); - Object w = e.exchange(v); - threadAssertEquals(w, one); - } catch (InterruptedException e){ - threadUnexpectedException(); - } - } - }); - Thread t2 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(two); - threadAssertEquals(v, one); - Object w = e.exchange(v); - threadAssertEquals(w, two); - } catch (InterruptedException e){ - threadUnexpectedException(); - } - } - }); - try { - t1.start(); - t2.start(); - t1.join(); - t2.join(); - } catch (InterruptedException ex) { - unexpectedException(); - } + Thread t1 = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertSame(one, e.exchange(two)); + assertSame(two, e.exchange(one)); + }}); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertSame(two, e.exchange(one)); + assertSame(one, e.exchange(two)); + }}); + + awaitTermination(t1); + awaitTermination(t2); } /** @@ -63,115 +45,71 @@ public class ExchangerTest extends JSR16 */ public void testTimedExchange() { final Exchanger e = new Exchanger(); - Thread t1 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertEquals(v, two); - Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertEquals(w, one); - } catch (InterruptedException e){ - threadUnexpectedException(); - } catch (TimeoutException toe) { - threadUnexpectedException(); - } - } - }); - Thread t2 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertEquals(v, one); - Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - threadAssertEquals(w, two); - } catch (InterruptedException e){ - threadUnexpectedException(); - } catch (TimeoutException toe) { - threadUnexpectedException(); - } - } - }); - try { - t1.start(); - t2.start(); - t1.join(); - t2.join(); - } catch (InterruptedException ex) { - unexpectedException(); - } + Thread t1 = newStartedThread(new CheckedRunnable() { + public void realRun() throws Exception { + assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); + assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); + }}); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() throws Exception { + assertSame(two, e.exchange(one, LONG_DELAY_MS, MILLISECONDS)); + assertSame(one, e.exchange(two, LONG_DELAY_MS, MILLISECONDS)); + }}); + + awaitTermination(t1); + awaitTermination(t2); } /** * interrupt during wait for exchange throws IE */ - public void testExchange_InterruptedException(){ + public void testExchange_InterruptedException() { final Exchanger e = new Exchanger(); - Thread t = new Thread(new Runnable() { - public void run(){ - try { - e.exchange(one); - threadShouldThrow(); - } catch (InterruptedException success){ - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (InterruptedException ex) { - unexpectedException(); - } + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + e.exchange(one); + }}); + + await(threadStarted); + t.interrupt(); + awaitTermination(t); } /** * interrupt during wait for timed exchange throws IE */ - public void testTimedExchange_InterruptedException(){ + public void testTimedExchange_InterruptedException() { final Exchanger e = new Exchanger(); - Thread t = new Thread(new Runnable() { - public void run(){ - try { - e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){ - } catch (Exception e2){ - threadFail("should throw IE"); - } - } - }); - try { - t.start(); - t.interrupt(); - t.join(); - } catch (InterruptedException ex){ - unexpectedException(); - } + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws Exception { + threadStarted.countDown(); + e.exchange(null, LONG_DELAY_MS, MILLISECONDS); + }}); + + await(threadStarted); + t.interrupt(); + awaitTermination(t); } /** - * timeout during wait for timed exchange throws TOE - */ - public void testExchange_TimeOutException(){ - final Exchanger e = new Exchanger(); - Thread t = new Thread(new Runnable() { - public void run(){ - try { - e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (TimeoutException success){ - } catch (InterruptedException e2){ - threadFail("should throw TOE"); - } - } - }); - try { - t.start(); - t.join(); - } catch (InterruptedException ex){ - unexpectedException(); - } + * timeout during wait for timed exchange throws TimeoutException + */ + public void testExchange_TimeoutException() { + final Exchanger e = new Exchanger(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws Exception { + long startTime = System.nanoTime(); + try { + e.exchange(null, timeoutMillis(), MILLISECONDS); + shouldThrow(); + } catch (TimeoutException success) {} + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + }}); + + awaitTermination(t); } /** @@ -179,54 +117,33 @@ public class ExchangerTest extends JSR16 */ public void testReplacementAfterExchange() { final Exchanger e = new Exchanger(); - Thread t1 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(one); - threadAssertEquals(v, two); - Object w = e.exchange(v); - threadShouldThrow(); - } catch (InterruptedException success){ - } - } - }); - Thread t2 = new Thread(new Runnable(){ - public void run(){ - try { - Object v = e.exchange(two); - threadAssertEquals(v, one); - Thread.sleep(SMALL_DELAY_MS); - Object w = e.exchange(v); - threadAssertEquals(w, three); - } catch (InterruptedException e){ - threadUnexpectedException(); - } - } - }); - Thread t3 = new Thread(new Runnable(){ - public void run(){ - try { - Thread.sleep(SMALL_DELAY_MS); - Object w = e.exchange(three); - threadAssertEquals(w, one); - } catch (InterruptedException e){ - threadUnexpectedException(); - } - } - }); - - try { - t1.start(); - t2.start(); - t3.start(); - Thread.sleep(SHORT_DELAY_MS); - t1.interrupt(); - t1.join(); - t2.join(); - t3.join(); - } catch (InterruptedException ex) { - unexpectedException(); - } + final CountDownLatch exchanged = new CountDownLatch(2); + final CountDownLatch interrupted = new CountDownLatch(1); + Thread t1 = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + assertSame(two, e.exchange(one)); + exchanged.countDown(); + e.exchange(two); + }}); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertSame(one, e.exchange(two)); + exchanged.countDown(); + interrupted.await(); + assertSame(three, e.exchange(one)); + }}); + Thread t3 = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + interrupted.await(); + assertSame(one, e.exchange(three)); + }}); + + await(exchanged); + t1.interrupt(); + awaitTermination(t1); + interrupted.countDown(); + awaitTermination(t2); + awaitTermination(t3); } }