test: extract RemoteControlApp to spec-helpers (#24020)

This commit is contained in:
Jeremy Rose 2020-06-09 11:42:53 -07:00 committed by GitHub
parent d08cfce6cb
commit 71e2b7151c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 66 deletions

View file

@ -3,27 +3,17 @@ import * as childProcess from 'child_process';
import * as http from 'http';
import * as Busboy from 'busboy';
import * as path from 'path';
import { ifdescribe, ifit } from './spec-helpers';
import { ifdescribe, ifit, defer, startRemoteControlApp } from './spec-helpers';
import { app } from 'electron/main';
import { crashReporter } from 'electron/common';
import { AddressInfo } from 'net';
import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as v8 from 'v8';
import * as uuid from 'uuid';
const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
const isLinuxOnArm = process.platform === 'linux' && process.arch.includes('arm');
const afterTest: ((() => void) | (() => Promise<void>))[] = [];
async function cleanup () {
for (const cleanup of afterTest) {
const r = cleanup();
if (r instanceof Promise) { await r; }
}
afterTest.length = 0;
}
type CrashInfo = {
prod: string
ver: string
@ -57,49 +47,6 @@ function checkCrash (expectedProcessType: string, fields: CrashInfo) {
}
}
const startRemoteControlApp = async () => {
const appPath = path.join(__dirname, 'fixtures', 'apps', 'remote-control');
const appProcess = childProcess.spawn(process.execPath, [appPath]);
appProcess.stderr.on('data', d => {
process.stderr.write(d);
});
const port = await new Promise<number>(resolve => {
appProcess.stdout.on('data', d => {
const m = /Listening: (\d+)/.exec(d.toString());
if (m && m[1] != null) {
resolve(Number(m[1]));
}
});
});
function remoteEval (js: string): any {
return new Promise((resolve, reject) => {
const req = http.request({
host: '127.0.0.1',
port,
method: 'POST'
}, res => {
const chunks = [] as Buffer[];
res.on('data', chunk => { chunks.push(chunk); });
res.on('end', () => {
const ret = v8.deserialize(Buffer.concat(chunks));
if (Object.prototype.hasOwnProperty.call(ret, 'error')) {
reject(new Error(`remote error: ${ret.error}\n\nTriggered at:`));
} else {
resolve(ret.result);
}
});
});
req.write(js);
req.end();
});
}
function remotely (script: Function, ...args: any[]): Promise<any> {
return remoteEval(`(${script})(...${JSON.stringify(args)})`);
}
afterTest.push(() => { appProcess.kill('SIGINT'); });
return { remoteEval, remotely };
};
const startServer = async () => {
const crashes: CrashInfo[] = [];
function getCrashes () { return crashes; }
@ -145,7 +92,7 @@ const startServer = async () => {
const port = (server.address() as AddressInfo).port;
afterTest.push(() => { server.close(); });
defer(() => { server.close(); });
return { getCrashes, port, waitForCrash };
};
@ -188,8 +135,6 @@ function waitForNewFileInDir (dir: string): Promise<string[]> {
// TODO(nornagon): Fix tests on linux/arm.
ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_TESTS)('crashReporter module', function () {
afterEach(cleanup);
describe('should send minidump', () => {
it('when renderer crashes', async () => {
const { port, waitForCrash } = await startServer();