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

[Part 1] Individual Test Case Report - Support custom messages #10293

Merged
merged 24 commits into from Jul 21, 2020

Conversation

@kunal-kushwaha
Copy link
Contributor

kunal-kushwaha commented Jul 20, 2020

Summary

PART 1 of 2

Aim: Support custom messages for jest worker
Ref: #9662

  • The original PR by @rogeliog #9662 profoundly inspires the current PR.
  • This sub-PR is an attempt to achieve #6616
  • As an independent PR, it allows custom messages to be sent to parent from the worker process using node event listeners

This PR part of an effort to break down PR #10227 , so that we can eventually have #6616

Test plan

An extension from #9662

Closes #9662

Acknowledgements

@sauravhiremath for his super awesome contribution 🚀 🚀
@rogeliog for laying down the foundation for this PR. 🚀
@SimenB and @jevakallio for their time and reviews 👏

@kunal-kushwaha kunal-kushwaha changed the title Feature/custom messages [Part 1] Individual Test Case Report - Support custom messages Jul 20, 2020
@sauravhiremath sauravhiremath mentioned this pull request Jul 20, 2020
3 of 3 tasks complete
@SimenB
SimenB approved these changes Jul 21, 2020
Copy link
Collaborator

SimenB left a comment

yay!

method: string,
...args: Array<any>
): PromiseWithCustomMessage<unknown> {
const customMessageListeners: Set<OnCustomMessage> = new Set();

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

Suggested change
const customMessageListeners: Set<OnCustomMessage> = new Set();
const customMessageListeners = new Set<OnCustomMessage>();

This comment has been minimized.

@sauravhiremath

sauravhiremath Jul 21, 2020 Contributor

Okayy, I think I missed these changes which I committed at a later stage in the previous PR; when I split it. Sorry for the repeat suggestions 😅

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

No worries!

const addCustomMessageListener = (listener: OnCustomMessage) => {
customMessageListeners.add(listener);
return () => {
// Check if the following check is required

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

we don't need to loop, just call the delete


const onEnd: OnEnd = (error: Error | null, result: unknown) => {
customMessageListeners.clear();
return error ? reject(error) : resolve(result);

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

keep if-else

WorkerPoolInterface,
WorkerPoolOptions,
} from './types';
import _messageParent from './workers/messageParent';

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

Suggested change
import _messageParent from './workers/messageParent';
export {default as messageParent} from './workers/messageParent';
@@ -146,3 +148,6 @@ export default class JestWorker {
return this._workerPool.end();
}
}

export type {PromiseWithCustomMessage};
export const messageParent = _messageParent;

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

Suggested change
export const messageParent = _messageParent;

const isWorkerThread = () => {
try {
const {isMainThread, parentPort} = require('worker_threads');

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

I think we can do import on this - we only support node 10 and up these days

@sauravhiremath sauravhiremath force-pushed the MLH-Fellowship:feature/custom-messages branch from f8fbd6b to 45492fa Jul 21, 2020
const onEnd: OnEnd = (error: Error | null, result: unknown) => {
customMessageListeners.clear();
if (error) {
return reject(error);

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

return does nothing

parentProcess: NodeJS.Process = process,
): void => {
if (isWorkerThread()) {
const {parentPort} = require('worker_threads');

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

No need to require here

import {isMainThread, parentPort} from 'worker_threads';
import {PARENT_MESSAGE_CUSTOM} from '../types';

const isWorkerThread = () => {

This comment has been minimized.

@SimenB

SimenB Jul 21, 2020 Collaborator

Can just be const isWorkerThread = !isMainThread && parentPort. If we want it to be lazy, at least ditch the try-catch

@SimenB
Copy link
Collaborator

SimenB commented Jul 21, 2020

Huh, seems we need the try catch for worker_threads looking at CI failure. Might be behind experimental flag on node 10

@sauravhiremath
Copy link
Contributor

sauravhiremath commented Jul 21, 2020

Huh, seems we need the try catch for worker_threads looking at CI failure. Might be behind experimental flag on node 10

Yeah for node v10 LTS it requires the expiremental flag

@sauravhiremath
Copy link
Contributor

sauravhiremath commented Jul 21, 2020

Huh, seems we need the try catch for worker_threads looking at CI failure. Might be behind experimental flag on node 10

@SimenB Do I open a PR for adding this?

@SimenB
Copy link
Collaborator

SimenB commented Jul 21, 2020

Nah, we should just keep the try-catch here. I'd forgotten it's flagged in v10

@SimenB SimenB merged commit 3150a81 into facebook:master Jul 21, 2020
23 checks passed
23 checks passed
cleanup-runs
Details
Running TypeScript compiler & ESLint
Details
Node v10.x on ubuntu-latest
Details
Node v10.x on macOS-latest
Details
Node v10.x on windows-latest
Details
Node v12.x on ubuntu-latest
Details
Node v12.x on macOS-latest
Details
Node v12.x on windows-latest
Details
Node v13.x on ubuntu-latest
Details
Node v13.x on macOS-latest
Details
Node v13.x on windows-latest
Details
Node v14.x on ubuntu-latest
Details
Node v14.x on macOS-latest
Details
Node v14.x on windows-latest
Details
Facebook CLA Check Contributor License Agreement is valid!
Details
ci/circleci: test-jest-circus Your tests passed on CircleCI!
Details
ci/circleci: test-node-10 Your tests passed on CircleCI!
Details
ci/circleci: test-node-12 Your tests passed on CircleCI!
Details
ci/circleci: test-node-13 Your tests passed on CircleCI!
Details
ci/circleci: test-node-14 Your tests passed on CircleCI!
Details
ci/circleci: test-or-deploy-website Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
facebook.jest #20200721.8 succeeded
Details
@SimenB
Copy link
Collaborator

SimenB commented Jul 21, 2020

🎉🎉🎉

@SimenB
Copy link
Collaborator

SimenB commented Jul 21, 2020

Btw, could you send a PR updating the readme of jest-worker with this new API?

@sunlaiman

This comment has been minimized.

Copy link

sunlaiman commented on packages/jest-worker/src/Farm.ts in c562f9f Jul 25, 2020

@sunlaiman

This comment has been minimized.

Copy link

sunlaiman commented on c562f9f Jul 25, 2020

#10227 #

@sauravhiremath sauravhiremath deleted the MLH-Fellowship:feature/custom-messages branch Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

You can’t perform that action at this time.