Skip to content

Commit

Permalink
Clean up threads for CacheBuilderTest and FuturesTest.
Browse files Browse the repository at this point in the history
Fixes #7320

RELNOTES=n/a
PiperOrigin-RevId: 654848217
  • Loading branch information
aoli-al authored and Google Java Core Libraries committed Jul 22, 2024
1 parent 5ccc169 commit 1dffea2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ public ListenableFuture<String> apply(String s) throws Exception {
}
};

ListenableFuture<String> futureResult =
transformAsync(input, function, newSingleThreadExecutor());
ExecutorService service = newSingleThreadExecutor();
ListenableFuture<String> futureResult = transformAsync(input, function, service);

input.set("value");
inFunction.await();
Expand All @@ -457,6 +457,8 @@ public ListenableFuture<String> 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 {
Expand Down Expand Up @@ -1167,8 +1169,9 @@ public ListenableFuture<String> apply(Throwable t) throws Exception {
}
};

ExecutorService executor = newSingleThreadExecutor();
ListenableFuture<String> futureResult =
catchingAsync(input, Exception.class, function, newSingleThreadExecutor());
catchingAsync(input, Exception.class, function, executor);

input.setException(new Exception());
inFunction.await();
Expand All @@ -1183,6 +1186,8 @@ public ListenableFuture<String> 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
Expand Down Expand Up @@ -1794,8 +1799,8 @@ public ListenableFuture<Integer> apply(String input) throws Exception {
}
};
SettableFuture<String> inputFuture = SettableFuture.create();
ListenableFuture<Integer> future =
transformAsync(inputFuture, function, newSingleThreadExecutor());
ExecutorService service = newSingleThreadExecutor();
ListenableFuture<Integer> future = transformAsync(inputFuture, function, service);
inputFuture.set("value");
inFunction.await();
future.cancel(false);
Expand All @@ -1810,6 +1815,8 @@ public ListenableFuture<Integer> apply(String input) throws Exception {
fail();
} catch (CancellationException expected) {
}
service.shutdown();
service.awaitTermination(30, SECONDS);
}

@J2ktIncompatible
Expand Down Expand Up @@ -1904,7 +1911,8 @@ public ListenableFuture<Integer> call() throws InterruptedException {
}
};
SettableFuture<String> inputFuture = SettableFuture.create();
ListenableFuture<Integer> future = submitAsync(callable, newSingleThreadExecutor());
ExecutorService service = newSingleThreadExecutor();
ListenableFuture<Integer> future = submitAsync(callable, service);
inputFuture.set("value");
inFunction.await();
future.cancel(false);
Expand All @@ -1919,6 +1927,8 @@ public ListenableFuture<Integer> call() throws InterruptedException {
fail();
} catch (CancellationException expected) {
}
service.shutdown();
service.awaitTermination(30, SECONDS);
}

@J2ktIncompatible
Expand Down Expand Up @@ -2069,6 +2079,7 @@ public ListenableFuture<Integer> call() {
@J2ktIncompatible
@GwtIncompatible // threads
public void testScheduleAsync_asyncCallable_nullInsteadOfFuture() throws Exception {
ExecutorService service = newSingleThreadScheduledExecutor();
ListenableFuture<?> chainedFuture =
scheduleAsync(
constantAsyncCallable(null), 1, NANOSECONDS, newSingleThreadScheduledExecutor());
Expand All @@ -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
Expand All @@ -2101,8 +2114,8 @@ public ListenableFuture<Integer> call() throws InterruptedException {
return resultFuture;
}
};
ListenableFuture<Integer> future =
scheduleAsync(callable, 1, NANOSECONDS, newSingleThreadScheduledExecutor());
ScheduledExecutorService service = newSingleThreadScheduledExecutor();
ListenableFuture<Integer> future = scheduleAsync(callable, 1, NANOSECONDS, service);
inFunction.await();
future.cancel(false);
callableDone.countDown();
Expand All @@ -2116,6 +2129,8 @@ public ListenableFuture<Integer> call() throws InterruptedException {
fail();
} catch (CancellationException expected) {
}
service.shutdown();
service.awaitTermination(30, SECONDS);
}

@J2ktIncompatible
Expand Down Expand Up @@ -2716,8 +2731,9 @@ public ListenableFuture<String> call() throws Exception {
}
};

ExecutorService service = newSingleThreadExecutor();
ListenableFuture<String> futureResult =
whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor());
whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service);

stringFuture.set("value");
booleanFuture.set(true);
Expand All @@ -2735,6 +2751,8 @@ public ListenableFuture<String> call() throws Exception {
fail();
} catch (CancellationException expected) {
}
service.shutdown();
service.awaitTermination(30, SECONDS);
}

@J2ktIncompatible
Expand All @@ -2759,8 +2777,9 @@ public ListenableFuture<String> call() throws Exception {
}
};

ExecutorService service = newSingleThreadExecutor();
ListenableFuture<String> futureResult =
whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor());
whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, service);

stringFuture.set("value");
booleanFuture.set(true);
Expand All @@ -2772,6 +2791,8 @@ public ListenableFuture<String> call() throws Exception {
} catch (CancellationException expected) {
}
gotException.await();
service.shutdown();
service.awaitTermination(30, SECONDS);
}

public void testWhenAllComplete_runnableResult() throws Exception {
Expand Down Expand Up @@ -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);
Expand All @@ -2869,6 +2891,8 @@ public void run() {
} catch (CancellationException expected) {
}
combinerCompletedWithoutInterrupt.await();
service.shutdown();
service.awaitTermination(30, SECONDS);
}

@J2ktIncompatible
Expand All @@ -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);
Expand All @@ -2907,6 +2932,8 @@ public void run() {
} catch (CancellationException expected) {
}
gotException.await();
service.shutdown();
service.awaitTermination(30, SECONDS);
}

public void testWhenAllSucceed() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading

0 comments on commit 1dffea2

Please sign in to comment.