< prev index next >
test/jdk/java/util/concurrent/tck/CountDownLatchTest.java
Print this page
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
Reviewed-by: martin, alanb
*** 34,43 ****
--- 34,44 ----
*/
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.concurrent.CountDownLatch;
+ import java.util.concurrent.ThreadLocalRandom;
import junit.framework.Test;
import junit.framework.TestSuite;
public class CountDownLatchTest extends JSR166TestCase {
*** 97,107 ****
await(pleaseCountDown);
assertEquals(2, l.getCount());
l.countDown();
assertEquals(1, l.getCount());
! assertThreadBlocks(t, Thread.State.WAITING);
l.countDown();
assertEquals(0, l.getCount());
awaitTermination(t);
}
--- 98,108 ----
await(pleaseCountDown);
assertEquals(2, l.getCount());
l.countDown();
assertEquals(1, l.getCount());
! if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
l.countDown();
assertEquals(0, l.getCount());
awaitTermination(t);
}
*** 122,132 ****
await(pleaseCountDown);
assertEquals(2, l.getCount());
l.countDown();
assertEquals(1, l.getCount());
! assertThreadBlocks(t, Thread.State.TIMED_WAITING);
l.countDown();
assertEquals(0, l.getCount());
awaitTermination(t);
}
--- 123,133 ----
await(pleaseCountDown);
assertEquals(2, l.getCount());
l.countDown();
assertEquals(1, l.getCount());
! if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
l.countDown();
assertEquals(0, l.getCount());
awaitTermination(t);
}
*** 154,195 ****
assertEquals(1, l.getCount());
}});
await(pleaseInterrupt);
! assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt();
awaitTermination(t);
}
/**
* timed await throws InterruptedException if interrupted before counted down
*/
public void testTimedAwait_Interruptible() {
! final CountDownLatch l = new CountDownLatch(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
Thread.currentThread().interrupt();
try {
! l.await(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
! l.await(LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
! assertEquals(1, l.getCount());
}});
await(pleaseInterrupt);
! assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt();
awaitTermination(t);
}
/**
--- 155,197 ----
assertEquals(1, l.getCount());
}});
await(pleaseInterrupt);
! if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt();
awaitTermination(t);
}
/**
* timed await throws InterruptedException if interrupted before counted down
*/
public void testTimedAwait_Interruptible() {
! final int initialCount = ThreadLocalRandom.current().nextInt(1, 3);
! final CountDownLatch l = new CountDownLatch(initialCount);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
Thread.currentThread().interrupt();
try {
! l.await(randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
pleaseInterrupt.countDown();
try {
! l.await(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (InterruptedException success) {}
assertFalse(Thread.interrupted());
! assertEquals(initialCount, l.getCount());
}});
await(pleaseInterrupt);
! if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt();
awaitTermination(t);
}
/**
*** 198,208 ****
--- 200,214 ----
public void testAwaitTimeout() throws InterruptedException {
final CountDownLatch l = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertEquals(1, l.getCount());
+
+ long startTime = System.nanoTime();
assertFalse(l.await(timeoutMillis(), MILLISECONDS));
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
+
assertEquals(1, l.getCount());
}});
awaitTermination(t);
assertEquals(1, l.getCount());
< prev index next >