aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar nharmata <nharmata@google.com>2018-08-09 09:31:02 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-08-09 09:32:51 -0700
commitb3b8639098c6b76f0485122160ba60182e950c56 (patch)
treed2f67c7b3067b4078093ae0d88ddf7bfa11ef51c
parent7777f4805086fc5998240772f7b3906a40bc3ec0 (diff)
Add test case showing off existing deadlock bug. There's no deterministic & perfect
way to expose the bug without e.g. adding hooks inside the implementation, but the test case I added seems to fail very consistently. RELNOTES: None PiperOrigin-RevId: 208060959
-rw-r--r--src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java b/src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java
index 3cd0ffc490..56729af005 100644
--- a/src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java
+++ b/src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java
@@ -18,6 +18,8 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
+import com.google.devtools.build.lib.testutil.TestThread;
+import com.google.devtools.build.lib.testutil.TestUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -28,7 +30,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -278,5 +282,68 @@ public class MultisetSemaphoreTest {
throw new InterruptedException();
}
}
+
+ @Test
+ @Ignore("Fails due to exposing actual deadlock bug (b/112412784)")
+ public void testSimpleDeadlock() throws Exception {
+ final MultisetSemaphore<String> multisetSemaphore = MultisetSemaphore.newBuilder()
+ .maxNumUniqueValues(2)
+ .build();
+
+ CountDownLatch thread1AcquiredLatch = new CountDownLatch(1);
+ CountDownLatch thread2AboutToAcquireLatch = new CountDownLatch(1);
+ CountDownLatch thread3AboutToAcquireLatch = new CountDownLatch(1);
+
+ TestThread thread1 =
+ new TestThread() {
+ @Override
+ public void runTest() throws InterruptedException {
+ multisetSemaphore.acquireAll(ImmutableSet.of("a", "b"));
+ thread1AcquiredLatch.countDown();
+ thread2AboutToAcquireLatch.await(
+ TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
+ thread3AboutToAcquireLatch.await(
+ TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
+ Thread.sleep(1000);
+ multisetSemaphore.releaseAll(ImmutableSet.of("a", "b"));
+ }
+ };
+ thread1.setName("Thread1");
+
+ TestThread thread2 =
+ new TestThread() {
+ @Override
+ public void runTest() throws InterruptedException {
+ thread1AcquiredLatch.await(
+ TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
+ thread2AboutToAcquireLatch.countDown();
+ multisetSemaphore.acquireAll(ImmutableSet.of("b", "c"));
+ multisetSemaphore.releaseAll(ImmutableSet.of("b", "c"));
+ }
+ };
+ thread2.setName("Thread2");
+
+ TestThread thread3 =
+ new TestThread() {
+ @Override
+ public void runTest() throws InterruptedException {
+ thread2AboutToAcquireLatch.await(
+ TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
+ Thread.sleep(1000);
+ thread3AboutToAcquireLatch.countDown();
+ multisetSemaphore.acquireAll(ImmutableSet.of("a", "d"));
+ multisetSemaphore.releaseAll(ImmutableSet.of("a", "d"));
+ }
+ };
+ thread3.setName("Thread3");
+
+ thread1.start();
+ thread2.start();
+ thread3.start();
+
+ thread1.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
+ thread2.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
+ thread3.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
+ }
}