|
35 | 35 | import java.nio.file.attribute.PosixFilePermission; |
36 | 36 | import java.util.EnumSet; |
37 | 37 | import java.util.Locale; |
| 38 | +import java.util.concurrent.FutureTask; |
| 39 | +import java.util.concurrent.TimeUnit; |
38 | 40 | import org.junit.jupiter.api.Test; |
39 | 41 | import org.junit.jupiter.api.io.TempDir; |
40 | 42 | import org.junit.jupiter.api.parallel.Execution; |
@@ -91,6 +93,29 @@ public void version() throws UsageException { |
91 | 93 | assertThat(err.toString()).contains("open-java-format: Version "); |
92 | 94 | } |
93 | 95 |
|
| 96 | + // Main used to leave its thread pool running after format returned. The command line does not notice, because it |
| 97 | + // exits, but anything that runs Main in-process kept the idle threads (#40, from google/google-java-format#384). |
| 98 | + @Test |
| 99 | + public void formatLeavesNoPoolThreadRunning() throws Exception { |
| 100 | + Path path = Files.writeString(testFolder.resolve("A.java"), "class A {}\n"); |
| 101 | + Main main = new Main( |
| 102 | + new PrintWriter(new StringWriter(), true), new PrintWriter(new StringWriter(), true), System.in); |
| 103 | + // The pool's threads join the thread group of the thread that creates the pool. |
| 104 | + ThreadGroup group = new ThreadGroup("formatLeavesNoPoolThreadRunning"); |
| 105 | + FutureTask<Integer> format = new FutureTask<>(() -> main.format(path.toString())); |
| 106 | + new Thread(group, format).start(); |
| 107 | + assertThat(format.get()).isEqualTo(0); |
| 108 | + |
| 109 | + Thread[] threads = new Thread[group.activeCount() + 16]; |
| 110 | + int count = group.enumerate(threads); |
| 111 | + for (int i = 0; i < count; i++) { |
| 112 | + threads[i].join(TimeUnit.SECONDS.toMillis(10)); |
| 113 | + assertWithMessage(threads[i].getName() + " is still running") |
| 114 | + .that(threads[i].isAlive()) |
| 115 | + .isFalse(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
94 | 119 | @Test |
95 | 120 | public void preserveOriginalFile() throws Exception { |
96 | 121 | Path path = Files.createFile(testFolder.resolve("Test.java")); |
|
0 commit comments