tests: close process on Errors (#11351)

This commit is contained in:
Nikolay Vitkov 2023-11-10 15:42:16 +01:00 committed by GitHub
parent 9d2b65912b
commit 12569b8f09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,8 +14,7 @@
* limitations under the License.
*/
/* eslint-disable @typescript-eslint/no-var-requires */
import {spawn, execSync} from 'child_process';
import path from 'path';
import expect from 'expect';
@ -35,7 +34,6 @@ describe('Fixtures', function () {
}
let dumpioData = '';
const {spawn} = await import('child_process');
const options = Object.assign({}, defaultBrowserOptions, {
pipe: true,
dumpio: true,
@ -57,7 +55,6 @@ describe('Fixtures', function () {
const {defaultBrowserOptions, puppeteerPath} = await getTestState();
let dumpioData = '';
const {spawn} = await import('child_process');
const options = Object.assign({}, defaultBrowserOptions, {dumpio: true});
const res = spawn('node', [
path.join(__dirname, '../fixtures', 'dumpio.js'),
@ -76,7 +73,6 @@ describe('Fixtures', function () {
const {defaultBrowserOptions, puppeteerPath, puppeteer} =
await getTestState();
const {spawn, execSync} = await import('child_process');
const options = Object.assign({}, defaultBrowserOptions, {
// Disable DUMPIO to cleanly read stdout.
dumpio: false,
@ -86,31 +82,43 @@ describe('Fixtures', function () {
puppeteerPath,
JSON.stringify(options),
]);
let wsEndPointCallback: (value: string) => void;
const wsEndPointPromise = new Promise<string>(x => {
wsEndPointCallback = x;
});
let output = '';
res.stdout.on('data', data => {
output += data;
if (output.indexOf('\n')) {
wsEndPointCallback(output.substring(0, output.indexOf('\n')));
let killed = false;
function killProcess() {
if (killed) {
return;
}
});
const browser = await puppeteer.connect({
browserWSEndpoint: await wsEndPointPromise,
});
const promises = [
waitEvent(browser, 'disconnected'),
new Promise(resolve => {
res.on('close', resolve);
}),
];
if (process.platform === 'win32') {
execSync(`taskkill /pid ${res.pid} /T /F`);
} else {
process.kill(res.pid!);
if (process.platform === 'win32') {
execSync(`taskkill /pid ${res.pid} /T /F`);
} else {
process.kill(res.pid!);
}
killed = true;
}
try {
let wsEndPointCallback: (value: string) => void;
const wsEndPointPromise = new Promise<string>(x => {
wsEndPointCallback = x;
});
let output = '';
res.stdout.on('data', data => {
output += data;
if (output.indexOf('\n')) {
wsEndPointCallback(output.substring(0, output.indexOf('\n')));
}
});
const browser = await puppeteer.connect({
browserWSEndpoint: await wsEndPointPromise,
});
const promises = [
waitEvent(browser, 'disconnected'),
new Promise(resolve => {
res.on('close', resolve);
}),
];
killProcess();
await Promise.all(promises);
} finally {
killProcess();
}
await Promise.all(promises);
});
});