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 all commits
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
15 changes: 15 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,21 @@ otherwise.
### noWarnCode
See `common.expectWarning()` for usage.

### onGC(target, listener)
* `target` [<Object>]
* `listener` [<Object>]
* `ongc` [<Function>]

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.

### opensslCli
* [<boolean>]

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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');

console.log('Run this test with --expose-gc');
assert.strictEqual(
typeof global.gc,
'function'
);
net.createServer(function() {}).listen(common.PORT);

let before = 0;
// Test that the implicit listener for an 'connect' event on net.Sockets is
// added using `once()`, i.e. can be gc'ed once that event has occurred.

const server = net.createServer(common.mustCall()).listen(0);

let collected = false;
const gcListener = { ongc() { collected = true; } };

{
// 2**26 == 64M entries
global.gc();
const junk = new Array(2 ** 26).fill(0);
before = process.memoryUsage().rss;
const gcObject = {};
common.onGC(gcObject, gcListener);

net.createConnection(common.PORT, '127.0.0.1', function() {
assert.notStrictEqual(junk.length, 0); // keep reference alive
setTimeout(done, 10);
global.gc();
});
const sock = net.createConnection(
server.address().port,
common.mustCall(() => {
assert.strictEqual(gcObject, gcObject); // keep reference alive
assert.strictEqual(collected, false);
setImmediate(done, sock);
}));
}

function done() {
function done(sock) {
global.gc();
const after = process.memoryUsage().rss;
const reclaimed = (before - after) / 1024;
console.log('%d kB reclaimed', reclaimed);
assert(reclaimed > 128 * 1024); // It's around 256 MB on x64.
process.exit();
setImmediate(() => {
assert.strictEqual(collected, true);
sock.end();
server.close();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ const assert = require('assert');
const tls = require('tls');
const fixtures = require('../common/fixtures');

assert.strictEqual(
typeof global.gc,
'function',
`Type of global.gc is not a function. Type: ${typeof global.gc}.` +
' Run this test with --expose-gc'
);
// Test that the implicit listener for an 'connect' event on tls.Sockets is
// added using `once()`, i.e. can be gc'ed once that event has occurred.

tls.createServer({
const server = tls.createServer({
cert: fixtures.readSync('test_cert.pem'),
key: fixtures.readSync('test_key.pem')
}).listen(common.PORT);
}).listen(0);

let collected = false;
const gcListener = { ongc() { collected = true; } };

{
// 2**26 == 64M entries
const junk = new Array(2 ** 26).fill(0);
const gcObject = {};
common.onGC(gcObject, gcListener);

const options = { rejectUnauthorized: false };
tls.connect(common.PORT, '127.0.0.1', options, function() {
assert.notStrictEqual(junk.length, 0); // keep reference alive
setTimeout(done, 10);
global.gc();
});
const sock = tls.connect(
server.address().port,
{ rejectUnauthorized: false },
common.mustCall(() => {
assert.strictEqual(gcObject, gcObject); // keep reference alive
assert.strictEqual(collected, false);
setImmediate(done, sock);
}));
}

function done() {
const before = process.memoryUsage().rss;
function done(sock) {
global.gc();
const after = process.memoryUsage().rss;
const reclaimed = (before - after) / 1024;
console.log('%d kB reclaimed', reclaimed);
assert(reclaimed > 256 * 1024); // it's more like 512M on x64
process.exit();
setImmediate(() => {
assert.strictEqual(collected, true);
sock.end();
server.close();
});
}