Skip to content

Commit 4570a43

Browse files
QiuYucheng2003Google Java Core Libraries
authored andcommitted
As discussed in Issue #8152, the ExecutorService in FakeTickerTest was created but never shut down, leading to potential thread leaks.
- Wrapped the test execution logic in a `try-with-resources` block. Closes #8154. Fixes #8152. RELNOTES=n/a PiperOrigin-RevId: 855241499
1 parent ac495c5 commit 4570a43

2 files changed

Lines changed: 42 additions & 34 deletions

File tree

android/guava-testlib/test/com/google/common/testing/FakeTickerTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,27 @@ public void testConcurrentAutoIncrementStep() throws Exception {
167167
private void runConcurrentTest(int numberOfThreads, Callable<@Nullable Void> callable)
168168
throws Exception {
169169
ExecutorService executorService = newFixedThreadPool(numberOfThreads);
170-
CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
171-
CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
172-
for (int i = numberOfThreads; i > 0; i--) {
173-
@SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored
174-
Future<?> possiblyIgnoredError =
175-
executorService.submit(
176-
new Callable<@Nullable Void>() {
177-
@Override
178-
public @Nullable Void call() throws Exception {
179-
startLatch.countDown();
180-
startLatch.await();
181-
callable.call();
182-
doneLatch.countDown();
183-
return null;
184-
}
185-
});
170+
try {
171+
CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
172+
CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
173+
for (int i = numberOfThreads; i > 0; i--) {
174+
@SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored
175+
Future<?> possiblyIgnoredError =
176+
executorService.submit(
177+
new Callable<@Nullable Void>() {
178+
@Override
179+
public @Nullable Void call() throws Exception {
180+
startLatch.countDown();
181+
startLatch.await();
182+
callable.call();
183+
doneLatch.countDown();
184+
return null;
185+
}
186+
});
187+
}
188+
doneLatch.await();
189+
} finally {
190+
executorService.shutdown();
186191
}
187-
doneLatch.await();
188192
}
189193
}

guava-testlib/test/com/google/common/testing/FakeTickerTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,27 @@ public void testConcurrentAutoIncrementStep() throws Exception {
165165
private void runConcurrentTest(int numberOfThreads, Callable<@Nullable Void> callable)
166166
throws Exception {
167167
ExecutorService executorService = newFixedThreadPool(numberOfThreads);
168-
CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
169-
CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
170-
for (int i = numberOfThreads; i > 0; i--) {
171-
@SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored
172-
Future<?> possiblyIgnoredError =
173-
executorService.submit(
174-
new Callable<@Nullable Void>() {
175-
@Override
176-
public @Nullable Void call() throws Exception {
177-
startLatch.countDown();
178-
startLatch.await();
179-
callable.call();
180-
doneLatch.countDown();
181-
return null;
182-
}
183-
});
168+
try {
169+
CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
170+
CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
171+
for (int i = numberOfThreads; i > 0; i--) {
172+
@SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored
173+
Future<?> possiblyIgnoredError =
174+
executorService.submit(
175+
new Callable<@Nullable Void>() {
176+
@Override
177+
public @Nullable Void call() throws Exception {
178+
startLatch.countDown();
179+
startLatch.await();
180+
callable.call();
181+
doneLatch.countDown();
182+
return null;
183+
}
184+
});
185+
}
186+
doneLatch.await();
187+
} finally {
188+
executorService.shutdown();
184189
}
185-
doneLatch.await();
186190
}
187191
}

0 commit comments

Comments
 (0)