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: add gc tracking API to common, refactor pummel net test + move it to parallel #21794

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: add gc tracking to common API
  • Loading branch information
addaleax committed Jul 13, 2018
commit 0642e68c98ff72861bb95d475d9031d0e73000fc
15 changes: 15 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ See `common.expectWarning()` for usage.

Indicates whether 'opensslCli' is supported.

### onGC(target, listener)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this section go before the opensslCli(), ABC-wise?

* `target` [<Object>]
* `listener` [<Object>]
* `listener.ongc` [<Function>]
Copy link
Contributor

Choose a reason for hiding this comment

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

`listener.ongc` -> `ongc` for consistency with other doc cases?


Installs a GC listener for the collection of `target`.

This uses `async_hooks` for GC tracking. This means that it enables
`async_hooks` tracking, which may affect the test functionality. It also
means that between a `global.gc()` call and the listener being invoked
a full `setImmediate()` invocation passes.

`listener` is an object to make it easier to use a closure; the target object
should not be in scope when `listener.ongc` is created.
Copy link
Contributor

Choose a reason for hiding this comment

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

`listener.ongc` -> `listener.ongc()`?


### platformTimeout(ms)
* `ms` [<number>]
* return [<number>]
Expand Down
27 changes: 27 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,30 @@ exports.isCPPSymbolsNotMapped = exports.isWindows ||
exports.isAIX ||
exports.isLinuxPPCBE ||
exports.isFreeBSD;

const gcTrackerMap = new WeakMap();
const gcTrackerTag = 'NODE_TEST_COMMON_GC_TRACKER';

exports.onGC = function(obj, gcListener) {
const async_hooks = require('async_hooks');

const onGcAsyncHook = async_hooks.createHook({
init: exports.mustCallAtLeast(function(id, type, trigger, resource) {
if (this.trackedId === undefined) {
assert.strictEqual(type, gcTrackerTag);
this.trackedId = id;
}
}),
destroy(id) {
assert.notStrictEqual(this.trackedId, -1);
if (id === this.trackedId) {
this.gcListener.ongc();
onGcAsyncHook.disable();
}
}
}).enable();
onGcAsyncHook.gcListener = gcListener;

gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag));
obj = null;
};
15 changes: 15 additions & 0 deletions test/parallel/test-common-gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
// Flags: --expose-gc
const common = require('../common');

{
const gcListener = { ongc: common.mustCall() };
common.onGC({}, gcListener);
global.gc();
}

{
const gcListener = { ongc: common.mustNotCall() };
common.onGC(process, gcListener);
global.gc();
}