Skip to content

Commit 6b72583

Browse files
addaleaxtargos
authored andcommitted
test: refactor test-tls-connect-memleak, move to parallel
This enables the test to run as part of the regular test suite. PR-URL: #21794 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 174a9db commit 6b72583

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

test/pummel/test-tls-connect-memleak.js test/parallel/test-tls-connect-memleak.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,36 @@ const assert = require('assert');
3030
const tls = require('tls');
3131
const fixtures = require('../common/fixtures');
3232

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

40-
tls.createServer({
36+
const server = tls.createServer({
4137
cert: fixtures.readSync('test_cert.pem'),
4238
key: fixtures.readSync('test_key.pem')
43-
}).listen(common.PORT);
39+
}).listen(0);
40+
41+
let collected = false;
42+
const gcListener = { ongc() { collected = true; } };
4443

4544
{
46-
// 2**26 == 64M entries
47-
const junk = new Array(2 ** 26).fill(0);
45+
const gcObject = {};
46+
common.onGC(gcObject, gcListener);
4847

49-
const options = { rejectUnauthorized: false };
50-
tls.connect(common.PORT, '127.0.0.1', options, function() {
51-
assert.notStrictEqual(junk.length, 0); // keep reference alive
52-
setTimeout(done, 10);
53-
global.gc();
54-
});
48+
const sock = tls.connect(
49+
server.address().port,
50+
{ rejectUnauthorized: false },
51+
common.mustCall(() => {
52+
assert.strictEqual(gcObject, gcObject); // keep reference alive
53+
assert.strictEqual(collected, false);
54+
setImmediate(done, sock);
55+
}));
5556
}
5657

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

0 commit comments

Comments
 (0)