--- jsr166/src/test/tck/CyclicBarrierTest.java 2004/01/09 14:45:58 1.7 +++ jsr166/src/test/tck/CyclicBarrierTest.java 2005/04/25 15:04:50 1.8 @@ -9,6 +9,8 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import java.util.concurrent.locks.*; +import java.util.concurrent.atomic.*; public class CyclicBarrierTest extends JSR166TestCase{ public static void main(String[] args) { @@ -162,7 +164,7 @@ public class CyclicBarrierTest extends J * throw BrokenBarrierException */ public void testAwait2_Interrupted_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -227,7 +229,7 @@ public class CyclicBarrierTest extends J * throw BrokenBarrierException */ public void testAwait4_Timeout_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -265,7 +267,7 @@ public class CyclicBarrierTest extends J * throw BrokenBarrierException */ public void testAwait5_Timeout_BrokenBarrier() { - final CyclicBarrier c = new CyclicBarrier(3); + final CyclicBarrier c = new CyclicBarrier(3); Thread t1 = new Thread(new Runnable() { public void run() { try { @@ -374,4 +376,249 @@ public class CyclicBarrierTest extends J } } + /** + * All threads block while a barrier is broken. + */ + public void testReset_Leakage() { + try { + final CyclicBarrier c = new CyclicBarrier(2); + final AtomicBoolean done = new AtomicBoolean(); + Thread t = new Thread() { + public void run() { + while (!done.get()) { + try { + while (c.isBroken()) + c.reset(); + + c.await(); + threadFail("await should not return"); + } + catch (BrokenBarrierException e) { + } + catch (InterruptedException ie) { + } + } + } + }; + + t.start(); + for( int i = 0; i < 4; i++) { + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + } + done.set(true); + t.interrupt(); + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a non-broken barrier does not break barrier + */ + public void testResetWithoutBreakage() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 3; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch (Throwable thrown) { + unexpectedException(); + }}}); + + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + barrier.await(); + t1.join(); + t2.join(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + if (i == 1) barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a barrier after interruption reinitializes it. + */ + public void testResetAfterInterrupt() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(InterruptedException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + t1.interrupt(); + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + /** + * Reset of a barrier after timeout reinitializes it. + */ + public void testResetAfterTimeout() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = new CyclicBarrier(3); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } + catch(TimeoutException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } + + + /** + * Reset of a barrier after a failed command reinitializes it. + */ + public void testResetAfterCommandException() { + try { + final CyclicBarrier start = new CyclicBarrier(3); + final CyclicBarrier barrier = + new CyclicBarrier(3, new Runnable() { + public void run() { + throw new NullPointerException(); }}); + for (int i = 0; i < 2; i++) { + Thread t1 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + Thread t2 = new Thread(new Runnable() { + public void run() { + try { start.await(); } + catch (Exception ie) { + threadFail("start barrier"); + } + try { barrier.await(); } + catch(BrokenBarrierException ok) {} + catch (Throwable thrown) { + unexpectedException(); + }}}); + + t1.start(); + t2.start(); + try { start.await(); } + catch (Exception ie) { threadFail("start barrier"); } + while (barrier.getNumberWaiting() < 2) { Thread.yield(); } + try { barrier.await(); } + catch (Exception ok) { } + t1.join(); + t2.join(); + assertTrue(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + barrier.reset(); + assertFalse(barrier.isBroken()); + assertEquals(0, barrier.getNumberWaiting()); + } + } + catch (Exception ex) { + unexpectedException(); + } + } }