Skip to content

Commit

Permalink
workerForceExit e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Mar 25, 2019
1 parent e58fd9a commit 04b13a7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
15 changes: 15 additions & 0 deletions e2e/Utils.ts
Expand Up @@ -89,6 +89,21 @@ export const writeFiles = (
});
};

const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;
/**
* Forces Jest to use workers by generating many test files to run.
* Slow and modifies the test output. Use sparingly.
*/
export const generateTestFilesToForceUsingWorkers = () => {
const testFiles = {};
for (let i = 0; i <= NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS; i++) {
testFiles[`__tests__/test${i}.test.js`] = `
test('test ${i}', () => {});
`;
}
return testFiles;
};

export const copyDir = (src: string, dest: string) => {
const srcStat = fs.lstatSync(src);
if (srcStat.isDirectory()) {
Expand Down
15 changes: 6 additions & 9 deletions e2e/__tests__/fatalWorkerError.test.ts
Expand Up @@ -7,31 +7,28 @@

import path from 'path';
import os from 'os';
import {cleanup, writeFiles} from '../Utils';
import {
cleanup,
generateTestFilesToForceUsingWorkers,
writeFiles,
} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(os.tmpdir(), 'fatal-worker-error');

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;

test('fails a test that terminates the worker with a fatal error', () => {
const testFiles = {
...generateTestFilesToForceUsingWorkers(),
'__tests__/fatalWorkerError.test.js': `
test('fatal worker error', () => {
process.exit(134);
});
`,
};

for (let i = 0; i <= NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS; i++) {
testFiles[`__tests__/test${i}.test.js`] = `
test('test ${i}', () => {});
`;
}

writeFiles(DIR, {
...testFiles,
'package.json': '{}',
Expand Down
62 changes: 62 additions & 0 deletions e2e/__tests__/workerForceExit.test.ts
@@ -0,0 +1,62 @@
import {tmpdir} from 'os';
import {resolve} from 'path';
import findProcess from 'find-process';

import {
cleanup,
generateTestFilesToForceUsingWorkers,
writeFiles,
} from '../Utils';
import runJest from '../runJest';

const DIR = resolve(tmpdir(), 'worker-force-exit');

beforeEach(() => cleanup(DIR));
const testFiles = {
...generateTestFilesToForceUsingWorkers(),
'package.json': `{
"testEnvironment": "node"
}`,
};
afterEach(() => cleanup(DIR));

const verifyNumPassed = stderr => {
const numberOfTestsPassed = (stderr.match(/\bPASS\b/g) || []).length;
// assuming -1 because of package.json, but +1 because of the individual test file
expect(numberOfTestsPassed).toBe(Object.keys(testFiles).length);
};

test('prints a warning if a worker is force exited', () => {
writeFiles(DIR, {
...testFiles,
'__tests__/simple.test.js': `
test('t', () => {
require('http').createServer().listen(0);
});
`,
});
const {status, stderr, stdout} = runJest(DIR, ['--maxWorkers=2']);

expect(status).toBe(0);
verifyNumPassed(stderr);
expect(stdout).toContain('A worker process has failed to exit gracefully');
});

test('force exits a worker that fails to exit gracefully', async () => {
writeFiles(DIR, {
...testFiles,
'__tests__/timeoutKilled.test.js': `
test('t', () => {
require('http').createServer().listen(0);
console.error('pid: ' + process.pid);
});
`,
});
const {status, stderr} = runJest(DIR, ['--maxWorkers=2']);

expect(status).toBe(0);
verifyNumPassed(stderr);

const [pid] = /pid: \d+/.exec(stderr);
expect(await findProcess('pid', pid)).toHaveLength(0);
});
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -39,6 +39,7 @@
"eslint-plugin-react": "^7.1.0",
"eslint-plugin-relay": "~0.0.19",
"execa": "^1.0.0",
"find-process": "^1.4.1",
"glob": "^7.1.1",
"graceful-fs": "^4.1.15",
"isbinaryfile": "^4.0.0",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Expand Up @@ -5908,6 +5908,15 @@ find-cache-dir@^2.0.0:
make-dir "^1.0.0"
pkg-dir "^3.0.0"

find-process@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/find-process/-/find-process-1.4.1.tgz#628c576a494d1525a27673fb26c77af90db5db02"
integrity sha512-RkYWDeukxEoDKUyocqMGKAYuwhSwq77zL99gCqhX9czWon3otdlzihJ0MSZ6YWNKHyvS/MN2YR4+RGYOuIEANg==
dependencies:
chalk "^2.0.1"
commander "^2.11.0"
debug "^2.6.8"

find-up@^1.0.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f"
Expand Down

0 comments on commit 04b13a7

Please sign in to comment.