Feat: Add public API for workers#2115
Conversation
|
@DavertMik This is just a draft. Kindly review this and we can build over this Simple Worker const Workers = require('codeceptjs/lib/workers');
const workers = new Workers(2, false);
workers.run();
//console.log(worker.workers);
workers.on('FAILED', (failedTests, err, stats) => {
console.log('Failed');
//console.log('FailedTests :', failedTest);
console.log('Errors :', err);
console.log('Stats :', stats);
for (const failedTest of failedTests) {
console.log('Title : ', failedTest.title);
console.log('Message : ', failedTest.err.message)
}
});
workers.on('PASSED', (stats) => {
console.log('Success', stats);
});Complex: const Workers = require('codeceptjs/lib/workers');
const workers = new Workers();
const testGroups = workers.createGroupsOfTests(2);
const worker1 = workers.spawn();
worker1.addConfig({
helpers: {
Puppeteer: {
url: 'http://localhost',
show: false
}
}
});
worker1.addTests(testGroups[0]);
const worker2 = workers.spawn();
worker2.addConfig({
helpers: {
Puppeteer: {
url: 'http://localhost',
show: true
}
}
});
worker2.addTests(testGroups[1]);
workers.run();
workers.on('FAILED', (failedTests, err, stats) => {
console.log('Failed');
//console.log('FailedTests :', failedTest);
console.log('Errors :', err);
console.log('Stats :', stats);
for (const failedTest of failedTests) {
console.log('Title : ', failedTest.title);
console.log('Message : ', failedTest.err.message)
}
});
workers.on('PASSED', (stats) => {
console.log('Success', stats);
}); |
|
My suggestions to usage examples: const { event } = require('codeceptjs');
const Workers = require('codeceptjs/lib/workers');
const workers = new Workers(2, { by: 'tests' }); // can also be by suite, function etc
workers.run();
//console.log(worker.workers);
workers.on(event.test.failed, (failedTest, err) => { // use standard constants
console.log('Failed', failedTest);
}); |
DavertMik
left a comment
There was a problem hiding this comment.
I like it. For shared memory let's make another PR after another round of thinking how to make it right.
|
Next: sharing data between master process and workers. Just like we use Usage share({ userid: 1});
const { userid } = inject();Usage types:
In each case, data is appended to a container. Implementation:
const workers = new Workers(2, { by: 'tests' });
workers.share({ user_id: 2 }) |
|
Should check for Worker API too. #2141 |
There was a problem hiding this comment.
Ok, looks like this design breaks the separation of concerns principle. Container knows too much about workers right now. I don't think we need to store workers inside a container, they need their own container for that :) Maybe we should create a singleton class WorkersStorage and delegate all workers control to it.
So I suggest the following:
1.Create WorkerStorage singleton class in a similar manner as Container - with static methods and top-level variables..
- Workers class should use
WorkerStoragewhen it adds a new worker or worker has finished - Add
sharetoWorkerStorageas static method Container.shareshould callWorkerStorage.share
902ce73 to
8b35280
Compare
8b35280 to
41e3b34
Compare
|
Complexity increasing per file
==============================
- test/data/sandbox/custom-worker/share_test.worker.js 1
- lib/workerStorage.js 2
- test/data/sandbox/custom-worker/custom_test.worker.js 1
- test/data/sandbox/codecept.customworker.js 1
- lib/workers.js 7
- test/data/sandbox/custom_worker_helper.js 1
- test/unit/worker_test.js 3
- test/data/sandbox/custom-worker/base_test.worker.js 1
Clones added
============
- lib/command/run-workers.js 5
- test/data/sandbox/codecept.customworker.js 1
- lib/workers.js 7
- test/unit/worker_test.js 28
See the complete overview on Codacy |
|
Should check for public API too #2155 |
|
@koushikmohan1996 @DavertMik Does it support Gherkin BDD? If so, how?
|
@gkushang Yes the option is available |
|
@koushikmohan1996 What change I have to make in my existing tests to run these workers in parallel? I have this file When I run Let me know if I am doing anything wrong here |
|
@koushikmohan1996 Here is the example repo if you'd like to try -- https://github.com/gkushang/codeceptjs-bdd/tree/feature/run-workers STEPS to reproduce: Here is the Workers output, |
| const convertToMochaTests = (testGroup) => { | ||
| const group = []; | ||
| if (testGroup instanceof Array) { | ||
| const mocha = MochaFactory.create({}, {}); |
There was a problem hiding this comment.
What happens when Codeceptjs has Gherkin but not mocha?
|
@gkushang On seeing the error |
| if (!semver.satisfies(process.version, '>=11.7.0')) this.skip('not for node version'); | ||
|
|
||
| const workerConfig = { | ||
| by: 'test', |
There was a problem hiding this comment.
you don't need this by.test because in this example you split tests by suites...

Motivation/Description of the PR
Applicable helpers:
Type of change
Checklist:
npm run docs)npm run lint)npm test)