Verify run preview descendant cleanup

This commit is contained in:
王鹏
2026-06-18 13:06:36 +08:00
parent 7e00b65159
commit 566dddfa1b

View File

@@ -11,6 +11,7 @@ import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@@ -53,27 +54,38 @@ public class RunPreviewProcessRunnerTest
} }
@Test @Test
public void startCommandRecordsManagedWindowsProcessPid() throws Exception public void stopManagedWindowsProcessTerminatesDescendantProcess() throws Exception
{ {
if (!isWindows()) if (!isWindows())
{ {
return; return;
} }
File projectDirectory = temporaryFolder.newFolder("managed-process"); File projectDirectory = temporaryFolder.newFolder("managed-process");
File childPidFile = new File(projectDirectory, "child.pid");
RunPreviewProcessRunner runner = new RunPreviewProcessRunner(); RunPreviewProcessRunner runner = new RunPreviewProcessRunner();
Process process = runner.startCommandForTesting( Process process = runner.startCommandForTesting(
"ping 127.0.0.1 -n 3 > nul", projectDirectory, new HashMap<String, String>()); "powershell.exe -NoProfile -NonInteractive -Command "
+ "\"$PID | Set-Content -NoNewline child.pid; Start-Sleep -Seconds 30\"",
projectDirectory, new HashMap<String, String>());
long childPid = -1L;
try try
{ {
assertTrue(process instanceof ManagedPreviewProcess); assertTrue(process instanceof ManagedPreviewProcess);
assertTrue(((ManagedPreviewProcess) process).waitForRootPid(2000L) > 0L); assertTrue(((ManagedPreviewProcess) process).waitForRootPid(2000L) > 0L);
assertTrue(waitForFile(childPidFile, 2000L));
childPid = Long.parseLong(new String(
Files.readAllBytes(childPidFile.toPath()), StandardCharsets.UTF_8).trim());
assertTrue(isWindowsProcessAlive(childPid));
} }
finally finally
{ {
new RunPreviewProcessKiller().stop(process, null); new RunPreviewProcessKiller().stop(process, null);
} }
assertFalse(process.isAlive());
assertFalse(isWindowsProcessAlive(childPid));
} }
private File viteProject() throws IOException private File viteProject() throws IOException
@@ -99,6 +111,37 @@ public class RunPreviewProcessRunnerTest
return nodeHome; return nodeHome;
} }
private boolean waitForFile(File file, long timeoutMillis) throws InterruptedException
{
long deadline = System.currentTimeMillis() + timeoutMillis;
while (System.currentTimeMillis() < deadline)
{
if (file.isFile() && file.length() > 0L)
{
return true;
}
Thread.sleep(25L);
}
return file.isFile() && file.length() > 0L;
}
private boolean isWindowsProcessAlive(long pid) throws Exception
{
if (pid <= 0L)
{
return false;
}
Process check = new ProcessBuilder(
"powershell.exe",
"-NoProfile",
"-NonInteractive",
"-Command",
"if (Get-Process -Id " + pid + " -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }")
.redirectErrorStream(true)
.start();
return check.waitFor() == 0;
}
private boolean isWindows() private boolean isWindows()
{ {
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");