test: extract RemoteControlApp to spec-helpers (#24020)
This commit is contained in:
parent
d08cfce6cb
commit
71e2b7151c
4 changed files with 82 additions and 66 deletions
|
@ -3,27 +3,17 @@ import * as childProcess from 'child_process';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as Busboy from 'busboy';
|
import * as Busboy from 'busboy';
|
||||||
import * as path from 'path';
|
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 { app } from 'electron/main';
|
||||||
import { crashReporter } from 'electron/common';
|
import { crashReporter } from 'electron/common';
|
||||||
import { AddressInfo } from 'net';
|
import { AddressInfo } from 'net';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as v8 from 'v8';
|
|
||||||
import * as uuid from 'uuid';
|
import * as uuid from 'uuid';
|
||||||
|
|
||||||
const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
|
const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
|
||||||
const isLinuxOnArm = process.platform === 'linux' && process.arch.includes('arm');
|
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 = {
|
type CrashInfo = {
|
||||||
prod: string
|
prod: string
|
||||||
ver: 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 startServer = async () => {
|
||||||
const crashes: CrashInfo[] = [];
|
const crashes: CrashInfo[] = [];
|
||||||
function getCrashes () { return crashes; }
|
function getCrashes () { return crashes; }
|
||||||
|
@ -145,7 +92,7 @@ const startServer = async () => {
|
||||||
|
|
||||||
const port = (server.address() as AddressInfo).port;
|
const port = (server.address() as AddressInfo).port;
|
||||||
|
|
||||||
afterTest.push(() => { server.close(); });
|
defer(() => { server.close(); });
|
||||||
|
|
||||||
return { getCrashes, port, waitForCrash };
|
return { getCrashes, port, waitForCrash };
|
||||||
};
|
};
|
||||||
|
@ -188,8 +135,6 @@ function waitForNewFileInDir (dir: string): Promise<string[]> {
|
||||||
|
|
||||||
// TODO(nornagon): Fix tests on linux/arm.
|
// TODO(nornagon): Fix tests on linux/arm.
|
||||||
ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_TESTS)('crashReporter module', function () {
|
ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_TESTS)('crashReporter module', function () {
|
||||||
afterEach(cleanup);
|
|
||||||
|
|
||||||
describe('should send minidump', () => {
|
describe('should send minidump', () => {
|
||||||
it('when renderer crashes', async () => {
|
it('when renderer crashes', async () => {
|
||||||
const { port, waitForCrash } = await startServer();
|
const { port, waitForCrash } = await startServer();
|
||||||
|
|
|
@ -4,6 +4,7 @@ import * as http from 'http';
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
import { AddressInfo, Socket } from 'net';
|
import { AddressInfo, Socket } from 'net';
|
||||||
import { emittedOnce } from './events-helpers';
|
import { emittedOnce } from './events-helpers';
|
||||||
|
import { defer } from './spec-helpers';
|
||||||
|
|
||||||
const kOneKiloByte = 1024;
|
const kOneKiloByte = 1024;
|
||||||
const kOneMegaByte = kOneKiloByte * kOneKiloByte;
|
const kOneMegaByte = kOneKiloByte * kOneKiloByte;
|
||||||
|
@ -22,13 +23,6 @@ function randomString (length: number) {
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
const cleanupTasks: (() => void)[] = [];
|
|
||||||
|
|
||||||
function cleanUp () {
|
|
||||||
cleanupTasks.forEach(t => t());
|
|
||||||
cleanupTasks.length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getResponse (urlRequest: Electron.ClientRequest) {
|
async function getResponse (urlRequest: Electron.ClientRequest) {
|
||||||
return new Promise<Electron.IncomingMessage>((resolve, reject) => {
|
return new Promise<Electron.IncomingMessage>((resolve, reject) => {
|
||||||
urlRequest.on('error', reject);
|
urlRequest.on('error', reject);
|
||||||
|
@ -70,7 +64,7 @@ function respondNTimes (fn: http.RequestListener, n: number): Promise<string> {
|
||||||
});
|
});
|
||||||
const sockets: Socket[] = [];
|
const sockets: Socket[] = [];
|
||||||
server.on('connection', s => sockets.push(s));
|
server.on('connection', s => sockets.push(s));
|
||||||
cleanupTasks.push(() => {
|
defer(() => {
|
||||||
server.close();
|
server.close();
|
||||||
sockets.forEach(s => s.destroy());
|
sockets.forEach(s => s.destroy());
|
||||||
});
|
});
|
||||||
|
@ -118,7 +112,6 @@ describe('net module', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
routeFailure = false;
|
routeFailure = false;
|
||||||
});
|
});
|
||||||
afterEach(cleanUp);
|
|
||||||
afterEach(async function () {
|
afterEach(async function () {
|
||||||
await session.defaultSession.clearCache();
|
await session.defaultSession.clearCache();
|
||||||
if (routeFailure && this.test) {
|
if (routeFailure && this.test) {
|
||||||
|
|
|
@ -110,4 +110,8 @@ app.whenReady().then(async () => {
|
||||||
chai.use(require('dirty-chai'));
|
chai.use(require('dirty-chai'));
|
||||||
|
|
||||||
const runner = mocha.run(cb);
|
const runner = mocha.run(cb);
|
||||||
|
const { runCleanupFunctions } = require('./spec-helpers');
|
||||||
|
runner.on('test end', () => {
|
||||||
|
runCleanupFunctions();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,78 @@
|
||||||
|
import * as childProcess from 'child_process';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as http from 'http';
|
||||||
|
import * as v8 from 'v8';
|
||||||
|
|
||||||
export const ifit = (condition: boolean) => (condition ? it : it.skip);
|
export const ifit = (condition: boolean) => (condition ? it : it.skip);
|
||||||
export const ifdescribe = (condition: boolean) => (condition ? describe : describe.skip);
|
export const ifdescribe = (condition: boolean) => (condition ? describe : describe.skip);
|
||||||
|
|
||||||
export const delay = (time: number) => new Promise(resolve => setTimeout(resolve, time));
|
export const delay = (time: number) => new Promise(resolve => setTimeout(resolve, time));
|
||||||
|
|
||||||
|
type CleanupFunction = (() => void) | (() => Promise<void>)
|
||||||
|
const cleanupFunctions: CleanupFunction[] = [];
|
||||||
|
export async function runCleanupFunctions () {
|
||||||
|
for (const cleanup of cleanupFunctions) {
|
||||||
|
const r = cleanup();
|
||||||
|
if (r instanceof Promise) { await r; }
|
||||||
|
}
|
||||||
|
cleanupFunctions.length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defer (f: CleanupFunction) {
|
||||||
|
cleanupFunctions.push(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
class RemoteControlApp {
|
||||||
|
process: childProcess.ChildProcess;
|
||||||
|
port: number;
|
||||||
|
|
||||||
|
constructor (proc: childProcess.ChildProcess, port: number) {
|
||||||
|
this.process = proc;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteEval = (js: string): Promise<any> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = http.request({
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: this.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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
remotely = (script: Function, ...args: any[]): Promise<any> => {
|
||||||
|
return this.remoteEval(`(${script})(...${JSON.stringify(args)})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function startRemoteControlApp () {
|
||||||
|
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]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
defer(() => { appProcess.kill('SIGINT'); });
|
||||||
|
return new RemoteControlApp(appProcess, port);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue