Skip to content

Commit

Permalink
add error handling for worker threads (#3828)
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityvi authored Jul 26, 2023
1 parent d3de801 commit fcb9dd8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/runner/concurrency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Concurrency extends EventEmitter {
if (this.isSafariEnvPresent) {
extraArgs.push('--serial');
}
let childProcess = this.createChildProcess(environment, args, extraArgs, index);
const childProcess = this.createChildProcess(environment, args, extraArgs, index);

return this.runChildProcess(childProcess, environment + ' environment', availColors, 'envs');
}));
Expand Down Expand Up @@ -216,8 +216,14 @@ class Concurrency extends EventEmitter {
});
});

return Promise.all(workerPool.tasks);
return new Promise((resolve, reject) => {
Promise.allSettled(workerPool.tasks)
.then(values => {
values.some(({status}) => status === 'rejected') ? reject() : resolve();
});
});
}

/**
*
* @param {Array} modules
Expand Down Expand Up @@ -296,7 +302,12 @@ class Concurrency extends EventEmitter {
});


return Promise.all(workerPool.tasks);
return new Promise((resolve, reject) => {
Promise.allSettled(workerPool.tasks)
.then(values => {
values.some(({status}) => status === 'rejected') ? reject() : resolve();
});
});
}


Expand Down
6 changes: 6 additions & 0 deletions lib/runner/concurrency/worker-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class WorkerTask extends EventEmitter {
port2.unref();

return this.piscina.run({argv: this.argv, port1}, {transferList: [port1]})
.catch(err => err)
.then(failures => {
if (this.settings.disable_output_boxes){
// eslint-disable-next-line no-console
Expand All @@ -102,6 +103,11 @@ class WorkerTask extends EventEmitter {
// eslint-disable-next-line no-console
console.log(boxen(this.task_output.join('\n'), {title: `────────────────── ${failures ? symbols.fail : symbols.ok} ${this.task_label}`, padding: 1, borderColor: 'cyan'}));
}

//throw error to mark exit-code of the process
if (failures) {
throw new Error();
}
});
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/src/runner/cli/testCliRunnerParallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const mockery = require('mockery');
const common = require('../../../common.js');
const NightwatchClient = common.require('index.js');
const {settings} = common;

describe('Test CLI Runner in Parallel', function () {
const ChildProcess = common.require('runner/concurrency/child-process.js');
Expand Down Expand Up @@ -105,6 +106,51 @@ describe('Test CLI Runner in Parallel', function () {
});
});

it('run error test file with concurrency - worker threads', function() {
let numberOfTasks = 0;
class RunnerBaseMock extends RunnerBase {
static readTestSource(settings, argv) {
assert.strictEqual(settings.testWorkersEnabled, true);

return [
'test_file_1.js',
'test_file_2.js'
];
}
}

class WorkerProcessMock extends WorkerProcess {
addTask({colors}) {

this.__tasks.push(new Promise((resolve, reject) => {
setTimeout(()=>{
numberOfTasks++;
reject(new Error('Nigtwatch custom error'));
}, 10*(numberOfTasks+1));
}));

return Promise.resolve(0);
}
}
mockery.registerMock('./worker-process.js', WorkerProcessMock);
mockery.registerMock('../runner.js', RunnerBaseMock);

return NightwatchClient.runTests({
env: 'default',
config: path.join(__dirname, '../../../extra/withgeckodriver-concurrent.json')
}, settings({
globals: {
reporter() {
assert.strictEqual(numberOfTasks, 2);
}
},
use_child_process: false,
silent: false,
output: false,
output_folder: false
}));
});

it('start single test run with geckodriver and test workers enabled', function () {
const testPath = path.join(__dirname, '../../../sampletests/async/test/sample.js');
const runner = NightwatchClient.CliRunner({
Expand Down

0 comments on commit fcb9dd8

Please sign in to comment.