mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
test: do kill testProcesses accordingly (#9818)
* test: do kill testProcesses accordingly * fix it
This commit is contained in:
parent
a8d276e223
commit
21c4435060
@ -21,7 +21,7 @@ import net from 'net';
|
|||||||
type TestChildParams = {
|
type TestChildParams = {
|
||||||
command: string[],
|
command: string[],
|
||||||
cwd?: string,
|
cwd?: string,
|
||||||
env?: { [key: string]: string | number | boolean | undefined },
|
env?: NodeJS.ProcessEnv,
|
||||||
shell?: boolean,
|
shell?: boolean,
|
||||||
onOutput?: () => void;
|
onOutput?: () => void;
|
||||||
};
|
};
|
||||||
@ -42,9 +42,13 @@ export class TestChildProcess {
|
|||||||
env: {
|
env: {
|
||||||
...process.env,
|
...process.env,
|
||||||
...params.env,
|
...params.env,
|
||||||
} as any,
|
},
|
||||||
cwd: params.cwd,
|
cwd: params.cwd,
|
||||||
shell: params.shell,
|
shell: params.shell,
|
||||||
|
// On non-windows platforms, `detached: true` makes child process a leader of a new
|
||||||
|
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
|
||||||
|
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
|
||||||
|
detached: process.platform !== 'win32',
|
||||||
});
|
});
|
||||||
if (process.env.PWTEST_DEBUG)
|
if (process.env.PWTEST_DEBUG)
|
||||||
process.stdout.write(`\n\nLaunching ${params.command.join(' ')}\n`);
|
process.stdout.write(`\n\nLaunching ${params.command.join(' ')}\n`);
|
||||||
@ -63,32 +67,34 @@ export class TestChildProcess {
|
|||||||
this.process.stderr.on('data', appendChunk);
|
this.process.stderr.on('data', appendChunk);
|
||||||
this.process.stdout.on('data', appendChunk);
|
this.process.stdout.on('data', appendChunk);
|
||||||
|
|
||||||
const onExit = () => {
|
const killProcessGroup = this._killProcessGroup.bind(this);
|
||||||
if (!this.process.pid || this.process.killed)
|
process.on('exit', killProcessGroup);
|
||||||
return;
|
|
||||||
try {
|
|
||||||
if (process.platform === 'win32')
|
|
||||||
execSync(`taskkill /pid ${this.process.pid} /T /F /FI "MEMUSAGE gt 0"`);
|
|
||||||
else
|
|
||||||
process.kill(-this.process.pid, 'SIGKILL');
|
|
||||||
} catch (e) {
|
|
||||||
// the process might have already stopped
|
|
||||||
}
|
|
||||||
};
|
|
||||||
process.on('exit', onExit);
|
|
||||||
this.exited = new Promise(f => {
|
this.exited = new Promise(f => {
|
||||||
this.process.on('exit', (exitCode, signal) => f({ exitCode, signal }));
|
this.process.on('exit', (exitCode, signal) => f({ exitCode, signal }));
|
||||||
process.off('exit', onExit);
|
process.off('exit', killProcessGroup);
|
||||||
});
|
});
|
||||||
this.exitCode = this.exited.then(r => r.exitCode);
|
this.exitCode = this.exited.then(r => r.exitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
if (!this.process.killed)
|
if (!this.process.killed)
|
||||||
this.process.kill();
|
this._killProcessGroup();
|
||||||
return this.exited;
|
return this.exited;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _killProcessGroup() {
|
||||||
|
if (!this.process.pid || this.process.killed)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
if (process.platform === 'win32')
|
||||||
|
execSync(`taskkill /pid ${this.process.pid} /T /F /FI "MEMUSAGE gt 0"`);
|
||||||
|
else
|
||||||
|
process.kill(-this.process.pid, 'SIGKILL');
|
||||||
|
} catch (e) {
|
||||||
|
// the process might have already stopped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async cleanExit() {
|
async cleanExit() {
|
||||||
const r = await this.exited;
|
const r = await this.exited;
|
||||||
if (r.exitCode)
|
if (r.exitCode)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user