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

Node: Add new worker.on('subprocessclose') to fix Node tests #1307

Merged
merged 4 commits into from Jan 9, 2024
Merged

Conversation

ibc
Copy link
Member

@ibc ibc commented Jan 8, 2024

Fixes #1306

Details

  • Problem only happened when running a test file separately (it worked by accident when running all tests together as usual).
  • Problem was that the ChildProcess object in Worker (the this.#child member) remains open for a bit until it emits 'close' event, which is emitted after 'exit' event or after 'error' event.
  • But we didn't await for that event at all since out worker.close() method is sync and we do not care of what is happening in the subprocess after calling it. And this is good. However it's bad for tests if we use --detectOpenHandles in Jest command.
  • Here we have added a new 'subprocessclose' event in Worker which is emitted when the worker subprocess has closed. It may be (and it will be) emitted AFTER calling worker.close() but not immediately after it.
  • This event is basically useful for scenarios like testing where we want to verify that nothing remains running/open when a test is finished.
  • Also added a worker.subprocessClosed getter.
  • Removed an ugly setTimeout in Channel::close() method. If the app wants to get the very latest notifications/logs from worker when closing it, use the above event to wait for final closure.

Bonus Tracks

  • Be consistent in command line arguments and always use --option-foo bar instead of --option-foo=bar.
  • Make tasks.py and Makefile more resistant to paths with spaces.

Fixes #1306

### Details

- Problem only happened when running a test file separately (it worked by accident when running all together as usual).
- Problem was that the `ChildProcess` object in `Worker` (the `this.#child` member) remains open for a bit until it emits 'close' event, which is emitted after 'exit' event or after 'error' event.
- Here we have added a new 'subprocessclose' event in `Worker` which is emitted when the worker subprocess has closed. It may be (and it will be) emitted AFTER calling `worker.close()` but not immediately after it.
- This event is basically useful for scenarios like testing where we want to verify that nothing remains running/open when a test is finished.
- Also added a `worker.subprocessClosed` getter.
- Removed an ugly `setTimeout` in `Channel::close()` method. If the app wants to get the very latest notifications/logs from worker when closing it, use the above event to wait for final closure.

### Bonus Tracks

- Be consistent in command line arguments and always use `--option-foo bar` instead of `--option-foo=bar`.
@ibc ibc requested review from jmillan and nazar-pc January 8, 2024 16:55
@@ -138,7 +144,7 @@ test('dataProducer.send() succeeds', async () => {
);
});

sendNextMessage();
await sendNextMessage();
Copy link
Member Author

Choose a reason for hiding this comment

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

I have some hope that this will fix this CI random failure: https://github.com/versatica/mediasoup/actions/runs/7448459449/job/20262892577?pr=1305

ctx.worker?.close();

if (ctx.worker?.subprocessClosed === false) {
Copy link
Member Author

@ibc ibc Jan 8, 2024

Choose a reason for hiding this comment

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

We check === false because if the worker failed to be created in beforeEach hook, then ctx.worker won't exist here so ctx.worker?.subprocessClosed will return undefined. In that case we do NOT want to await for any event.

node/src/Worker.ts Outdated Show resolved Hide resolved
@ibc ibc changed the title Fix Node tests Node: Add new worker.on('subprocessclose') to fix Node tests Jan 8, 2024
Copy link
Member

@jmillan jmillan left a comment

Choose a reason for hiding this comment

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

Just added a cosmetic/style comment.

node/src/test/test-ActiveSpeakerObserver.ts Show resolved Hide resolved
@ibc ibc merged commit e3ab41f into v3 Jan 9, 2024
20 checks passed
@ibc ibc deleted the fix-node-tests branch January 9, 2024 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Issues in Node tests
3 participants