Skip to content

Commit

Permalink
test: refactor test-net-connect-memleak, move to parallel
Browse files Browse the repository at this point in the history
This enables the test to run as part of the regular test suite.

PR-URL: #21794
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and targos committed Jul 16, 2018
1 parent b338ff5 commit 174a9db
Showing 1 changed file with 23 additions and 23 deletions.
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();
});
}

0 comments on commit 174a9db

Please sign in to comment.