Skip to content

Commit d4cdb2c

Browse files
committed
test: refactor test-gc-tls-external-memory
- Don’t use network connections, we don’t need them and they add more native objects into the mix when we care about other kinds of native objects. - Run GC only once every 64 iterations – this cuts down running time from 4 s to 400 ms. - Use `common.mustCall()` for the `connect()` handler. - Make sure that the TLS sockets do get garbage collected, since the test would otherwise also pass if they remain alive indefinitely. PR-URL: #22651 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: George Adams <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 702e114 commit d4cdb2c

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

test/parallel/test-gc-tls-external-memory.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,42 @@ const common = require('../common');
88
if (!common.hasCrypto)
99
common.skip('missing crypto');
1010

11+
const makeDuplexPair = require('../common/duplexpair');
12+
const onGC = require('../common/ongc');
1113
const assert = require('assert');
12-
const net = require('net');
1314
const tls = require('tls');
1415

1516
// Payload doesn't matter. We just need to have the tls
1617
// connection try and connect somewhere.
17-
const yolo = Buffer.alloc(10000).fill('yolo');
18-
const server = net.createServer(function(socket) {
19-
socket.write(yolo);
20-
});
18+
const dummyPayload = Buffer.alloc(10000, 'yolo');
2119

22-
server.listen(0, common.mustCall(function() {
23-
const { port } = server.address();
24-
let runs = 0;
25-
connect();
20+
let runs = 0;
2621

27-
function connect() {
22+
// Count garbage-collected TLS sockets.
23+
let gced = 0;
24+
function ongc() { gced++; }
25+
26+
connect();
27+
28+
function connect() {
29+
if (runs % 64 === 0)
2830
global.gc();
29-
assert(process.memoryUsage().external >= 0);
30-
if (runs++ < 512)
31-
tls.connect(port).on('error', connect);
32-
else
33-
server.close();
31+
const externalMemoryUsage = process.memoryUsage().external;
32+
assert(externalMemoryUsage >= 0, `${externalMemoryUsage} < 0`);
33+
if (runs++ === 512) {
34+
// Make sure at least half the TLS sockets have been gargbage collected
35+
// (so that this test can actually check what it's testing):
36+
assert(gced >= 256, `${gced} < 256`);
37+
return;
3438
}
35-
}));
39+
40+
const { clientSide, serverSide } = makeDuplexPair();
41+
42+
const tlsSocket = tls.connect({ socket: clientSide });
43+
tlsSocket.on('error', common.mustCall(connect));
44+
onGC(tlsSocket, { ongc });
45+
46+
// Use setImmediate so that we don't trigger the error within the same
47+
// event loop tick.
48+
setImmediate(() => serverSide.write(dummyPayload));
49+
}

0 commit comments

Comments
 (0)