From 1dffea2656a0d71c5d978cd9e79ef2544fb65738 Mon Sep 17 00:00:00 2001 From: Ao Li Date: Mon, 22 Jul 2024 12:08:50 -0700 Subject: [PATCH] Clean up threads for `CacheBuilderTest` and `FuturesTest`. Fixes #7320 RELNOTES=n/a PiperOrigin-RevId: 654848217 --- .../google/common/cache/CacheBuilderTest.java | 2 + .../common/util/concurrent/FuturesTest.java | 51 ++++++++++++++----- .../google/common/cache/CacheBuilderTest.java | 2 + .../common/util/concurrent/FuturesTest.java | 51 ++++++++++++++----- 4 files changed, 82 insertions(+), 24 deletions(-) diff --git a/android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java b/android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java index 3c39cf30857c..3170fbb0bd24 100644 --- a/android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java +++ b/android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java @@ -550,6 +550,8 @@ public void run() { // notification. assertEquals(expectedKeys, Sets.union(cache.asMap().keySet(), removalNotifications.keySet())); assertTrue(Sets.intersection(cache.asMap().keySet(), removalNotifications.keySet()).isEmpty()); + threadPool.shutdown(); + threadPool.awaitTermination(300, SECONDS); } /** diff --git a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index fcaf35d00826..62be7ae3be4c 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -441,8 +441,8 @@ public ListenableFuture apply(String s) throws Exception { } }; - ListenableFuture futureResult = - transformAsync(input, function, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture futureResult = transformAsync(input, function, service); input.set("value"); inFunction.await(); @@ -457,6 +457,8 @@ public ListenableFuture apply(String s) throws Exception { // https://github.com/google/guava/issues/1989 assertEquals(1, gotException.getCount()); // gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testTransformAsync_cancelPropagatesToAsyncOutput() throws Exception { @@ -1167,8 +1169,9 @@ public ListenableFuture apply(Throwable t) throws Exception { } }; + ExecutorService executor = newSingleThreadExecutor(); ListenableFuture futureResult = - catchingAsync(input, Exception.class, function, newSingleThreadExecutor()); + catchingAsync(input, Exception.class, function, executor); input.setException(new Exception()); inFunction.await(); @@ -1183,6 +1186,8 @@ public ListenableFuture apply(Throwable t) throws Exception { // https://github.com/google/guava/issues/1989 assertEquals(1, gotException.getCount()); // gotException.await(); + executor.shutdown(); + executor.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -1794,8 +1799,8 @@ public ListenableFuture apply(String input) throws Exception { } }; SettableFuture inputFuture = SettableFuture.create(); - ListenableFuture future = - transformAsync(inputFuture, function, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture future = transformAsync(inputFuture, function, service); inputFuture.set("value"); inFunction.await(); future.cancel(false); @@ -1810,6 +1815,8 @@ public ListenableFuture apply(String input) throws Exception { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -1904,7 +1911,8 @@ public ListenableFuture call() throws InterruptedException { } }; SettableFuture inputFuture = SettableFuture.create(); - ListenableFuture future = submitAsync(callable, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture future = submitAsync(callable, service); inputFuture.set("value"); inFunction.await(); future.cancel(false); @@ -1919,6 +1927,8 @@ public ListenableFuture call() throws InterruptedException { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2069,6 +2079,7 @@ public ListenableFuture call() { @J2ktIncompatible @GwtIncompatible // threads public void testScheduleAsync_asyncCallable_nullInsteadOfFuture() throws Exception { + ExecutorService service = newSingleThreadScheduledExecutor(); ListenableFuture chainedFuture = scheduleAsync( constantAsyncCallable(null), 1, NANOSECONDS, newSingleThreadScheduledExecutor()); @@ -2083,6 +2094,8 @@ public void testScheduleAsync_asyncCallable_nullInsteadOfFuture() throws Excepti "AsyncCallable.call returned null instead of a Future. " + "Did you mean to return immediateFuture(null)?"); } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2101,8 +2114,8 @@ public ListenableFuture call() throws InterruptedException { return resultFuture; } }; - ListenableFuture future = - scheduleAsync(callable, 1, NANOSECONDS, newSingleThreadScheduledExecutor()); + ScheduledExecutorService service = newSingleThreadScheduledExecutor(); + ListenableFuture future = scheduleAsync(callable, 1, NANOSECONDS, service); inFunction.await(); future.cancel(false); callableDone.countDown(); @@ -2116,6 +2129,8 @@ public ListenableFuture call() throws InterruptedException { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2716,8 +2731,9 @@ public ListenableFuture call() throws Exception { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2735,6 +2751,8 @@ public ListenableFuture call() throws Exception { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2759,8 +2777,9 @@ public ListenableFuture call() throws Exception { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2772,6 +2791,8 @@ public ListenableFuture call() throws Exception { } catch (CancellationException expected) { } gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testWhenAllComplete_runnableResult() throws Exception { @@ -2855,8 +2876,9 @@ public void run() { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).run(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).run(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2869,6 +2891,8 @@ public void run() { } catch (CancellationException expected) { } combinerCompletedWithoutInterrupt.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2894,8 +2918,9 @@ public void run() { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).run(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).run(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2907,6 +2932,8 @@ public void run() { } catch (CancellationException expected) { } gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testWhenAllSucceed() throws Exception { diff --git a/guava-tests/test/com/google/common/cache/CacheBuilderTest.java b/guava-tests/test/com/google/common/cache/CacheBuilderTest.java index 691b46bdd96f..638e33745447 100644 --- a/guava-tests/test/com/google/common/cache/CacheBuilderTest.java +++ b/guava-tests/test/com/google/common/cache/CacheBuilderTest.java @@ -541,6 +541,8 @@ public void run() { // notification. assertEquals(expectedKeys, Sets.union(cache.asMap().keySet(), removalNotifications.keySet())); assertTrue(Sets.intersection(cache.asMap().keySet(), removalNotifications.keySet()).isEmpty()); + threadPool.shutdown(); + threadPool.awaitTermination(300, SECONDS); } /** diff --git a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java index fcaf35d00826..62be7ae3be4c 100644 --- a/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/FuturesTest.java @@ -441,8 +441,8 @@ public ListenableFuture apply(String s) throws Exception { } }; - ListenableFuture futureResult = - transformAsync(input, function, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture futureResult = transformAsync(input, function, service); input.set("value"); inFunction.await(); @@ -457,6 +457,8 @@ public ListenableFuture apply(String s) throws Exception { // https://github.com/google/guava/issues/1989 assertEquals(1, gotException.getCount()); // gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testTransformAsync_cancelPropagatesToAsyncOutput() throws Exception { @@ -1167,8 +1169,9 @@ public ListenableFuture apply(Throwable t) throws Exception { } }; + ExecutorService executor = newSingleThreadExecutor(); ListenableFuture futureResult = - catchingAsync(input, Exception.class, function, newSingleThreadExecutor()); + catchingAsync(input, Exception.class, function, executor); input.setException(new Exception()); inFunction.await(); @@ -1183,6 +1186,8 @@ public ListenableFuture apply(Throwable t) throws Exception { // https://github.com/google/guava/issues/1989 assertEquals(1, gotException.getCount()); // gotException.await(); + executor.shutdown(); + executor.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -1794,8 +1799,8 @@ public ListenableFuture apply(String input) throws Exception { } }; SettableFuture inputFuture = SettableFuture.create(); - ListenableFuture future = - transformAsync(inputFuture, function, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture future = transformAsync(inputFuture, function, service); inputFuture.set("value"); inFunction.await(); future.cancel(false); @@ -1810,6 +1815,8 @@ public ListenableFuture apply(String input) throws Exception { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -1904,7 +1911,8 @@ public ListenableFuture call() throws InterruptedException { } }; SettableFuture inputFuture = SettableFuture.create(); - ListenableFuture future = submitAsync(callable, newSingleThreadExecutor()); + ExecutorService service = newSingleThreadExecutor(); + ListenableFuture future = submitAsync(callable, service); inputFuture.set("value"); inFunction.await(); future.cancel(false); @@ -1919,6 +1927,8 @@ public ListenableFuture call() throws InterruptedException { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2069,6 +2079,7 @@ public ListenableFuture call() { @J2ktIncompatible @GwtIncompatible // threads public void testScheduleAsync_asyncCallable_nullInsteadOfFuture() throws Exception { + ExecutorService service = newSingleThreadScheduledExecutor(); ListenableFuture chainedFuture = scheduleAsync( constantAsyncCallable(null), 1, NANOSECONDS, newSingleThreadScheduledExecutor()); @@ -2083,6 +2094,8 @@ public void testScheduleAsync_asyncCallable_nullInsteadOfFuture() throws Excepti "AsyncCallable.call returned null instead of a Future. " + "Did you mean to return immediateFuture(null)?"); } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2101,8 +2114,8 @@ public ListenableFuture call() throws InterruptedException { return resultFuture; } }; - ListenableFuture future = - scheduleAsync(callable, 1, NANOSECONDS, newSingleThreadScheduledExecutor()); + ScheduledExecutorService service = newSingleThreadScheduledExecutor(); + ListenableFuture future = scheduleAsync(callable, 1, NANOSECONDS, service); inFunction.await(); future.cancel(false); callableDone.countDown(); @@ -2116,6 +2129,8 @@ public ListenableFuture call() throws InterruptedException { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2716,8 +2731,9 @@ public ListenableFuture call() throws Exception { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2735,6 +2751,8 @@ public ListenableFuture call() throws Exception { fail(); } catch (CancellationException expected) { } + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2759,8 +2777,9 @@ public ListenableFuture call() throws Exception { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2772,6 +2791,8 @@ public ListenableFuture call() throws Exception { } catch (CancellationException expected) { } gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testWhenAllComplete_runnableResult() throws Exception { @@ -2855,8 +2876,9 @@ public void run() { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).run(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).run(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2869,6 +2891,8 @@ public void run() { } catch (CancellationException expected) { } combinerCompletedWithoutInterrupt.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } @J2ktIncompatible @@ -2894,8 +2918,9 @@ public void run() { } }; + ExecutorService service = newSingleThreadExecutor(); ListenableFuture futureResult = - whenAllComplete(stringFuture, booleanFuture).run(combiner, newSingleThreadExecutor()); + whenAllComplete(stringFuture, booleanFuture).run(combiner, service); stringFuture.set("value"); booleanFuture.set(true); @@ -2907,6 +2932,8 @@ public void run() { } catch (CancellationException expected) { } gotException.await(); + service.shutdown(); + service.awaitTermination(30, SECONDS); } public void testWhenAllSucceed() throws Exception {