0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-gc-tls-external-memory.js
tannal 0b6b2a4dc7
test: merge ongc and gcutil into gc.js
PR-URL: https://github.com/nodejs/node/pull/54355
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-08-29 08:49:52 +01:00

50 lines
1.4 KiB
JavaScript

'use strict';
// Flags: --expose-gc
// Tests that memoryUsage().external doesn't go negative
// when a lot tls connections are opened and closed
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { duplexPair } = require('stream');
const { onGC } = require('../common/gc');
const assert = require('assert');
const tls = require('tls');
// Payload doesn't matter. We just need to have the tls
// connection try and connect somewhere.
const dummyPayload = Buffer.alloc(10000, 'yolo');
let runs = 0;
// Count garbage-collected TLS sockets.
let gced = 0;
function ongc() { gced++; }
connect();
function connect() {
if (runs % 64 === 0)
global.gc();
const externalMemoryUsage = process.memoryUsage().external;
assert(externalMemoryUsage >= 0, `${externalMemoryUsage} < 0`);
if (runs++ === 512) {
// Make sure at least half the TLS sockets have been garbage collected
// (so that this test can actually check what it's testing):
assert(gced >= 256, `${gced} < 256`);
return;
}
const [ clientSide, serverSide ] = duplexPair();
const tlsSocket = tls.connect({ socket: clientSide });
tlsSocket.on('error', common.mustCall(connect));
onGC(tlsSocket, { ongc });
// Use setImmediate so that we don't trigger the error within the same
// event loop tick.
setImmediate(() => serverSide.write(dummyPayload));
}