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

test: use async/await for gc #773

Closed

Conversation

gabrielschulhof
Copy link
Contributor

Convert tests that gc into async functions that await 10 ticks after
each gc. This is because gc has become async and because references
will soon be deleted via a native SetImmediate().

Re: nodejs/node#34386

@gabrielschulhof gabrielschulhof marked this pull request as draft July 23, 2020 04:42
Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM assume the tests pass consitently

* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.
@gabrielschulhof gabrielschulhof marked this pull request as ready for review July 26, 2020 15:44
@gabrielschulhof
Copy link
Contributor Author

@mhdawson I actually had to make some more substantial changes, because I realized that, in order for exceptions to be reported from the correct test, we need to make sure that the tests are properly sequenced. This means that all tests that do things asynchronously must hold up the loop in test/index.js which advances through the list of tests it is given.

A symptom of what we have without this change is that, when you run the tests, it will print out All tests passed!, then hang for a while, and only then return. I've had it where it printed that out, and then segfaulted. With this change, it only prints out All tests passed! when it gets to the end with no outstanding async callbacks.

Gabriel Schulhof added 2 commits July 26, 2020 09:08
This reduces the number of promises created internally
binding.queueWork(worker);
}

function cancel(binding) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to remove this test, because create → queue → cancel is a racy operation. After queueing completes there is a small chance that by the time the next line – cancel – executes, the work will already have been queued. This was happening on

https://ci.nodejs.org/job/node-test-node-addon-api-new/2520/nodes=debian8-64/console

Error: Unknown failure
    at /home/iojs/build/workspace/node-test-node-addon-api-new/nodes/debian8-64/node-addon-api/test/asyncprogressqueueworker.js:67:13
    at new Promise (<anonymous>)
    at cancel (/home/iojs/build/workspace/node-test-node-addon-api-new/nodes/debian8-64/node-addon-api/test/asyncprogressqueueworker.js:52:10)
    at test (/home/iojs/build/workspace/node-test-node-addon-api-new/nodes/debian8-64/node-addon-api/test/asyncprogressqueueworker.js:13:9)
    at async /home/iojs/build/workspace/node-test-node-addon-api-new/nodes/debian8-64/node-addon-api/test/index.js:102:5

We have a similar test in core:

https://github.com/nodejs/node/blob/1f94b89309bcd56a2883cd1b0eb2db610251095e/test/node-api/test_async/test_async.cc#L140-L173

I can only conclude that it works in core because the stack is shallower and therefore the distance between queue and cancel is shorter and so it happens to work.

@gabrielschulhof
Copy link
Contributor Author

@mhdawson I got the tests to pass but I had to remove one that was unfixably racy and failing consistently on our CI, although not on all platforms. It was AsyncProgressQueueWorker cancel.

@legendecas
Copy link
Member

legendecas commented Jul 30, 2020

Does this PR depend on nodejs/node#34386?

@gabrielschulhof
Copy link
Contributor Author

@legendecas the fact that nodejs/node#34386 has landed is causing node-addon-api PRs to fail when testing against v15.x. Landing this will fix those failures, and makes it possible to start landing node-addon-api PRs again.

@legendecas
Copy link
Member

😄👍, just wondering if the changes will break on Node.js versions that not backported 34386. So the question is clarified. LGTM.

@gabrielschulhof
Copy link
Contributor Author

@legendecas if you find the PR to be LGTM could you please approve it?

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

gabrielschulhof pushed a commit that referenced this pull request Jul 30, 2020
* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.

PR-URL: #773
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
@gabrielschulhof
Copy link
Contributor Author

Landed in 461e364.

@gabrielschulhof gabrielschulhof deleted the async-await-for-gc branch July 30, 2020 16:36
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.

PR-URL: nodejs/node-addon-api#773
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.

PR-URL: nodejs/node-addon-api#773
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.

PR-URL: nodejs/node-addon-api#773
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
* Use promises and async/await to ensure that one test finishes before
  another one starts. This prevents errors thrown in one test from
  appearing to originate from another test.

* Perform garbage collection consistently, via `testUtil.runGCTests()`,
  to ensure that it is performed correctly and that its results are
  awaited.

PR-URL: nodejs/node-addon-api#773
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants