-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unique id for each worker and pass it to the child process #5494
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5494 +/- ##
==========================================
+ Coverage 61.66% 61.67% +<.01%
==========================================
Files 213 213
Lines 7070 7071 +1
Branches 3 4 +1
==========================================
+ Hits 4360 4361 +1
Misses 2709 2709
Partials 1 1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just some minor changes and we're ready to merge!
packages/jest-worker/src/types.js
Outdated
@@ -47,6 +47,7 @@ export type WorkerOptions = {| | |||
forkOptions: ForkOptions, | |||
maxRetries: number, | |||
workerPath: string, | |||
workerId: number, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alphabetically sorted props 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep :)
packages/jest-worker/src/index.js
Outdated
@@ -67,13 +67,16 @@ export default class { | |||
} | |||
|
|||
// Build the options once for all workers to avoid allocating extra objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to keep all shared props at a common object, but probably the comment does not apply anymore, since we need to create one object per worker now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
Regarding the README, it'd be good to mention it; I think that not only Jest itself can benefit from this (e.g. Metro also uses |
@mjesun that's awesome! About the README, do you think that something like a small note regarding the existence of such environment param on each worker would be enough? what do you say about: Note: Each worker has a unique id (index that starts with Which will be placed right before this part |
Yep, that sounds good to me! If you can demonstrate it in a concise example too, it would be awesome 😄 |
What do you say about extending the original example of the standard usage? something like: import Worker from 'jest-worker';
async function main() {
const myWorker = new Worker(require.resolve('./worker'), {
exposedMethods: ['foo', 'bar', 'getWorkerId'],
numWorkers: 4,
});
console.log(await myWorker.foo('Alice')); // "Hello from foo: Alice"
console.log(await myWorker.bar('Bob')); // "Hello from bar: Bob"
console.log(await myWorker.getWorkerId()); // "3" -> this message has sent from the 3rd worker
myWorker.end();
}
main(); File
|
You're awesome @ranyitz, thanks for implementing this! :) |
The comment you added LGTM, but if you want to extend the example it'd be awesome! |
@mjesun Example added. |
Yup! Let's wait for the green light in CI and we'll merge. Thanks for your contribution! |
no problem, thanks for the review! |
Hey! Thanks for solving #2284! |
Is there a way to access this workerId from the |
I assume that |
Or using |
thanks, @mjesun, your answer helped. I have a question related to this. All tests within a single file run one-by-one. Could we say the same about tests in different files but per one worker (process)? I mean, could we say that there is always only one test running per worker (and that would mean maxWorker == max running parallel tests, because each worker has only 1 running test) I want to do something like this: let db: TestDb;
const POOL_ID = process.env.JEST_WORKER_ID || '1';
beforeAll(async () => { db = await TestDb.connect(POOL_ID); });
beforeEach(async () => {
await db.clearAll();
});
afterAll(async () => { db.manager.connection.close(); }); If there is always only one test per worker in the "running" state, that's fine. If not, that wouldn't work and I would need to implement some sort of mutex per file that locks a connection in Documentation say nothing about it at all. |
Yes, that is precisely what happens internally in Jest. Each of the test files is sent to an available worker, only one at a time per worker. What you have to be aware though, is to fully clean your stuff using the available |
Regarding the |
@mjesun thanks for the explanation. |
What version of jest do I need to have to use this feature? I just updated to v23.0.0-alpha.6r and process.env.JEST_WORKER_ID gives me undefined. Am I missing something? |
It should be there. |
@SimenB is right. Without any other context it's hard to say what is going on. Can you share the test code or a minimal repro? |
Yes. He was right. My global jest was on version 23 but my local jest was on version 22. I’m appending it to my database names to avoid contamination. I wanted to do a bit more testing before I responded, but I upgraded and it seems to be working beautifully. Thanks a lot!
… On Apr 18, 2018, at 9:15 AM, Miguel Jiménez Esún ***@***.***> wrote:
@SimenB <https://github.com/SimenB> is right. Without any other context it's hard to say what is going on. Can you share the test code or a minimal repro?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#5494 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAYGCuZOPWjBOMccRcNky2iqb7nDNP8uks5tp1h_gaJpZM4R9pOo>.
|
I would always suggest |
@evantahler That was fixed some time ago by @ranyitz :) |
Oh! Wonderful. |
In #5860 fwiw |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR is related to issue #2284
The Problem
When trying to run integration tests in parallel, there could be a race of conditions.
For example when two tests are trying to modify the same record in a database at the same time.
To get full isolation, the user can choose one of two solutions:
But in order to use the second, you need unique identifiers for each worker, so you could assign each test to a different server.
For example:
Summary
Assign a unique id for each worker and pass it to the child process. It will be available via
process.env.JEST_WORKER_ID
.1
.number
and it is being cast tostring
only when passed to the newly forked process.options
that passes to each worker needed to be modified with the newworkerId
option.process.env
, lucky enough, jest uses itself for testing, so we could use the sameJEST_WORKER_ID
😄Test plan
workerId
to theenv.JEST_WORKER_ID
.Questions