From 78cae6dd66b4c7ce348039c8956a6b2b758e1307 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 24 Jan 2017 23:07:40 +0000 Subject: Global cleanup change. -- PiperOrigin-RevId: 145473478 MOS_MIGRATED_REVID=145473478 --- .../lib/concurrent/MultisetSemaphoreTest.java | 139 ++++++++++++--------- 1 file changed, 77 insertions(+), 62 deletions(-) (limited to 'src/test/java/com/google/devtools/build/lib/concurrent/MultisetSemaphoreTest.java') 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 4a1abe5a2c..3c87569007 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 @@ -27,6 +27,7 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; @@ -84,37 +85,43 @@ public class MultisetSemaphoreTest { for (int i = 0; i < m; i++) { final String val = "val" + i; // And we submit M Runnables, each of which - executorService.submit(wrapper.wrap(new Runnable() { - @Override - public void run() { - try { - // Has two rounds + @SuppressWarnings("unused") + Future possiblyIgnoredError = + executorService.submit( + wrapper.wrap( + new Runnable() { + @Override + public void run() { + try { + // Has two rounds - // Wherein the first round - // The Runnable acquire a permit for a unique value (among M values), - ImmutableSet valSet = ImmutableSet.of(val); - multisetSemaphore.acquireAll(valSet); - assertThat(numThreadsJustAfterAcquireInFirstRound.getAndIncrement()).isLessThan(n); - // And then sleeps, - Thread.sleep(napTimeMs); - numThreadsJustAfterAcquireInFirstRound.decrementAndGet(); - multisetSemaphore.releaseAll(valSet); + // Wherein the first round + // The Runnable acquire a permit for a unique value (among M values), + ImmutableSet valSet = ImmutableSet.of(val); + multisetSemaphore.acquireAll(valSet); + assertThat(numThreadsJustAfterAcquireInFirstRound.getAndIncrement()) + .isLessThan(n); + // And then sleeps, + Thread.sleep(napTimeMs); + numThreadsJustAfterAcquireInFirstRound.decrementAndGet(); + multisetSemaphore.releaseAll(valSet); - // And wherein the second round - // The Runnable again acquires a permit for its unique value, - multisetSemaphore.acquireAll(valSet); - assertThat(numThreadsJustAfterAcquireInSecondRound.getAndIncrement()).isLessThan(n); - // And then sleeps, - Thread.sleep(napTimeMs); - numThreadsJustAfterAcquireInSecondRound.decrementAndGet(); - // And notes that it has completed the second round, - secondRoundCompleted.incrementAndGet(); - multisetSemaphore.releaseAll(valSet); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } - } - })); + // And wherein the second round + // The Runnable again acquires a permit for its unique value, + multisetSemaphore.acquireAll(valSet); + assertThat(numThreadsJustAfterAcquireInSecondRound.getAndIncrement()) + .isLessThan(n); + // And then sleeps, + Thread.sleep(napTimeMs); + numThreadsJustAfterAcquireInSecondRound.decrementAndGet(); + // And notes that it has completed the second round, + secondRoundCompleted.incrementAndGet(); + multisetSemaphore.releaseAll(valSet); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + })); } // And we wait for all M Runnables to complete (that is, none of them were deadlocked), boolean interrupted = ExecutorUtil.interruptibleShutdown(executorService); @@ -160,25 +167,29 @@ public class MultisetSemaphoreTest { for (int i = 0; i < n; i++) { final String differentVal = "different-val" + i; // And we submit N Runnables, each of which - executorService.submit(wrapper.wrap(new Runnable() { - @Override - public void run() { - try { - Set vals = ImmutableSet.of(sameVal, differentVal); - // Tries to acquire a permit for a set of two values, one of which is the same for all - // the N Runnables and one of which is unique across all N Runnables. - multisetSemaphore.acquireAll(vals); - // And then sleeps - Thread.sleep(napTimeMs); - // And then releases its permits - multisetSemaphore.releaseAll(vals); - // And then counts down the done latch, - allDoneLatch.countDown(); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } - } - })); + @SuppressWarnings("unused") + Future possiblyIgnoredError = + executorService.submit( + wrapper.wrap( + new Runnable() { + @Override + public void run() { + try { + Set vals = ImmutableSet.of(sameVal, differentVal); + // Tries to acquire a permit for a set of two values, one of which is the same for all + // the N Runnables and one of which is unique across all N Runnables. + multisetSemaphore.acquireAll(vals); + // And then sleeps + Thread.sleep(napTimeMs); + // And then releases its permits + multisetSemaphore.releaseAll(vals); + // And then counts down the done latch, + allDoneLatch.countDown(); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + })); } // Then all of our Runnables completed (without deadlock!), as expected, boolean interrupted = ExecutorUtil.interruptibleShutdown(executorService); @@ -218,20 +229,24 @@ public class MultisetSemaphoreTest { for (List orderedVals : permutations) { final Set orderedSet = new LinkedHashSet<>(orderedVals); // And we submit N! Runnables, each of which - executorService.submit(wrapper.wrap(new Runnable() { - @Override - public void run() { - try { - // Tries to acquire a permit for the set of N values, with a unique iteration order - // (across all the N! different permutations) - multisetSemaphore.acquireAll(orderedSet); - // And then immediately releases the permit. - multisetSemaphore.releaseAll(orderedSet); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } - } - })); + @SuppressWarnings("unused") + Future possiblyIgnoredError = + executorService.submit( + wrapper.wrap( + new Runnable() { + @Override + public void run() { + try { + // Tries to acquire a permit for the set of N values, with a unique iteration order + // (across all the N! different permutations) + multisetSemaphore.acquireAll(orderedSet); + // And then immediately releases the permit. + multisetSemaphore.releaseAll(orderedSet); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + })); } // Then all of our Runnables completed (without deadlock!), as expected, boolean interrupted = ExecutorUtil.interruptibleShutdown(executorService); -- cgit v1.2.3