Skip to content

Commit

Permalink
feat(testrunner): improve reporting of unhandled errors/rejections (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Apr 7, 2020
1 parent a7ae205 commit 39e06f0
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions utils/testrunner/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,32 @@ class TestRunner {
}, totalTimeout);
}

const terminations = [
createTermination.call(this, 'SIGINT', TestResult.Terminated, 'SIGINT received'),
createTermination.call(this, 'SIGHUP', TestResult.Terminated, 'SIGHUP received'),
createTermination.call(this, 'SIGTERM', TestResult.Terminated, 'SIGTERM received'),
createTermination.call(this, 'unhandledRejection', TestResult.Crashed, 'UNHANDLED PROMISE REJECTION'),
createTermination.call(this, 'uncaughtException', TestResult.Crashed, 'UNHANDLED ERROR'),
];
for (const termination of terminations)
process.on(termination.event, termination.handler);
const handleSIGINT = () => this._terminate(TestResult.Terminated, 'SIGINT received', false, null);
const handleSIGHUP = () => this._terminate(TestResult.Terminated, 'SIGHUP received', false, null);
const handleSIGTERM = () => this._terminate(TestResult.Terminated, 'SIGTERM received', true, null);
const handleRejection = error => {
let message = 'UNHANDLED PROMISE REJECTION';
if (!(error instanceof Error)) {
message += ': ' + error;
error = new Error();
error.stack = '';
}
this._terminate(TestResult.Crashed, message, false, error);
};
const handleException = error => {
let message = 'UNHANDLED ERROR';
if (!(error instanceof Error)) {
message += ': ' + error;
error = new Error();
error.stack = '';
}
this._terminate(TestResult.Crashed, message, false, error);
};
process.on('SIGINT', handleSIGINT);
process.on('SIGHUP', handleSIGHUP);
process.on('SIGTERM', handleSIGTERM);
process.on('unhandledRejection', handleRejection);
process.on('uncaughtException', handleException);

const workerCount = Math.min(parallel, testRuns.length);
const workerPromises = [];
Expand All @@ -401,23 +418,18 @@ class TestRunner {
}
await Promise.all(workerPromises);

for (const termination of terminations)
process.removeListener(termination.event, termination.handler);
process.removeListener('SIGINT', handleSIGINT);
process.removeListener('SIGHUP', handleSIGHUP);
process.removeListener('SIGTERM', handleSIGTERM);
process.removeListener('unhandledRejection', handleRejection);
process.removeListener('uncaughtException', handleException);

if (testRuns.some(run => run.isFailure()))
this._result.setResult(TestResult.Failed, '');

clearTimeout(timeoutId);
await this._delegate.onFinished(this._result);

function createTermination(event, result, message) {
return {
event,
message,
handler: error => this._terminate(result, message, event === 'SIGTERM', event.startsWith('SIG') ? null : error),
};
}

const result = this._result;
this._result = null;
this._workers = [];
Expand Down

0 comments on commit 39e06f0

Please sign in to comment.