Skip to content

Commit

Permalink
TFW: tighten up thread pool control logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-chacko committed Jul 27, 2023
1 parent e586743 commit 826c76e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions testify/src/main/java/testify/bus/SimpleBusImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static java.util.Arrays.stream;
import static java.util.Collections.synchronizedMap;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
Expand Down Expand Up @@ -165,11 +166,14 @@ public BiStream<String, String> biStream() {
@Override
public void easyClose() throws Exception {
threadPool.shutdown();
boolean terminated = threadPool.awaitTermination(5, SECONDS);
if (terminated) return;
if (threadPool.awaitTermination(5, SECONDS)) return;
List<?> list = threadPool.shutdownNow();
if (threadPool.awaitTermination(200, MILLISECONDS)) return;
String threadDump = dumpAllThreads();
// Check whether it terminated while dumping threads...
if (threadPool.isTerminated()) return;
throw new Error(String.format("Unable to shut down thread pool%nUnstarted work: %s%nThread dump follows:%n%s", list, dumpAllThreads()));
// No, it really wouldn't shut down, so the thread dump we collected earlier MUST have offending thread info
throw new Error(String.format("Unable to shut down thread pool%nUnstarted work: %s%nThread dump follows:%n%s", list, threadDump));
}

private static String dumpAllThreads() {
Expand Down

0 comments on commit 826c76e

Please sign in to comment.