Skip to content
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 option to call initialize and end methods in workers #7014

Merged
merged 1 commit into from
Sep 21, 2018

Conversation

rafeca
Copy link
Contributor

@rafeca rafeca commented Sep 20, 2018

Summary

There is no way to make jest-worker call an initialize method before calling a method in the worker for the first time.

This PR adds the possibility to call an initialize method if it exists in the worker and an initializeArgs option has been passed (I've opted to not call by default this method to not make this PR a breaking change, but I can change this easily).

Also, to make the behaviour consistent, I've added some logic to call an end method in the worker if it exists when the Worker farm is being ended.

Test plan

I've created unit tests to verify the behaviour

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be documented in the readme as well.

What about setup and teardown? They feel more symmetrical than initialize and end. or onStart onEnd

// $FlowFixMe: This has to be a dynamic require.
const main = require(file);

if (!main['end']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.end

return;
}

execFunction(main['end'], main, [], () => process.exit(0), () => {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.end

ctx = main;
}

if (!initializeArgsForFlow || initialized || !main['initialize']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.initialize

initialized = true;

execFunction(
main['initialize'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main.initialize

@rafeca
Copy link
Contributor Author

rafeca commented Sep 20, 2018

What about setup and teardown? They feel more symmetrical than initialize and end. or onStart onEnd

👍

@rafeca
Copy link
Contributor Author

rafeca commented Sep 20, 2018

I've renamed the methods to setup and teardown and also the config param to setupArgs to make it consistent

Copy link
Contributor

@mjesun mjesun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; just minor fixes required.

CHANGELOG.md Outdated
@@ -4,6 +4,7 @@

- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))
- `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)).
- `[jest-worker]` Add `initializeArgs` option to call an `initialize` method in the worker before the first call. Call `end` method in each worker when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to setup and teardown.

return;
}

execFunction(main.teardown, main, [], () => process.exit(0), () => {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also do the process.exit(0) for errors.

}

function execFunction(
fn: Function,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn: (...args: $ReadOnlyArray<mixed>) => mixed

function execFunction(
fn: Function,
ctx: any,
args: $ReadOnlyArray<any>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed, not any.

fn: Function,
ctx: any,
args: $ReadOnlyArray<any>,
onResult: (result: any) => void,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

@@ -61,7 +65,7 @@ function reportSuccess(result: any) {
process.send([PARENT_MESSAGE_OK, result]);
}

function reportError(error: Error) {
function reportError(error: Error, type?: number = PARENT_MESSAGE_ERROR) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 129, you do reportError(error, PARENT_MESSAGE_INITIALIZE_ERROR); however PARENT_MESSAGE_INITIALIZE_ERROR is incompatible with PARENT_MESSAGE_ERROR.

Also, type must not be optional.

@rafeca rafeca force-pushed the initialize-worker branch 2 times, most recently from 34cec31 to deb30be Compare September 21, 2018 11:13
Copy link
Contributor

@mjesun mjesun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great job!

@rafeca rafeca merged commit 69654b5 into jestjs:master Sep 21, 2018
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants