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