Skip to content

Commit

Permalink
add parallel_process_delay in worker threads
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityvi committed Jan 25, 2024
1 parent 37615b2 commit 6e04ff2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
38 changes: 22 additions & 16 deletions lib/runner/concurrency/worker-task.js
Expand Up @@ -20,6 +20,7 @@ class WorkerTask extends EventEmitter {
super();

this.task_output = task_ouput || [];
this.startDelay = settings.parallel_process_delay;
this.piscina = piscina;
this.label = label;
this.settings = settings;
Expand Down Expand Up @@ -93,22 +94,27 @@ 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
console.log(`${failures ? symbols.fail : symbols.ok} ${this.task_label}\n`, this.task_output.join('\n'), '\n');
} else {
// 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();
}
});
return new Promise((resolve, reject) => {
setTimeout(() => {
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
console.log(`${failures ? symbols.fail : symbols.ok} ${this.task_label}\n`, this.task_output.join('\n'), '\n');
} else {
// 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) {
return reject(new Error());
}
resolve();
});
}, this.index * this.startDelay);
});
}
}

Expand Down
50 changes: 49 additions & 1 deletion test/src/runner/cli/testCliRunnerParallel.js
Expand Up @@ -8,10 +8,11 @@ const {settings} = common;
describe('Test CLI Runner in Parallel', function () {
const ChildProcess = common.require('runner/concurrency/child-process.js');
const WorkerProcess = common.require('runner/concurrency/worker-process.js');
const WorkerTask = common.require('runner/concurrency/worker-task.js');
const RunnerBase = common.require('runner/runner.js');

const filtered = Object.keys(require.cache).filter(item => (
item.endsWith('runner/runner.js') || item.endsWith('runner/concurrency/child-process.js') || item.endsWith('runner/concurrency/worker-process.js')
item.endsWith('runner/runner.js') || item.endsWith('runner/concurrency/child-process.js') || item.endsWith('runner/concurrency/worker-process.js') || item.endsWith('runner/concurrency/worker-task.js')
));

if (filtered && filtered.length > 0) {
Expand Down Expand Up @@ -326,5 +327,52 @@ describe('Test CLI Runner in Parallel', function () {
assert.strictEqual(runner.isTestWorkersEnabled(), false);
assert.strictEqual(runner.parallelMode(), true);
});


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

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

class WorkerTaskMock extends WorkerTask {
async runWorkerTask(colors, type) {
startDelay = this.startDelay;

return new Promise((resolve, reject) => {
try {
assert.strictEqual(this.startDelay, 10);
resolve();
} catch (err) {
reject(err);
}
}, 10);
}
}
mockery.registerMock('./worker-task.js', WorkerTaskMock);
mockery.registerMock('../runner.js', RunnerBaseMock);

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

0 comments on commit 6e04ff2

Please sign in to comment.