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: https://github.com/nodejs/node/pull/22651 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: George Adams <george.adams@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
702e11454e
commit
d4cdb2ce66
@ -8,28 +8,42 @@ const common = require('../common');
|
|||||||
if (!common.hasCrypto)
|
if (!common.hasCrypto)
|
||||||
common.skip('missing crypto');
|
common.skip('missing crypto');
|
||||||
|
|
||||||
|
const makeDuplexPair = require('../common/duplexpair');
|
||||||
|
const onGC = require('../common/ongc');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const net = require('net');
|
|
||||||
const tls = require('tls');
|
const tls = require('tls');
|
||||||
|
|
||||||
// Payload doesn't matter. We just need to have the tls
|
// Payload doesn't matter. We just need to have the tls
|
||||||
// connection try and connect somewhere.
|
// connection try and connect somewhere.
|
||||||
const yolo = Buffer.alloc(10000).fill('yolo');
|
const dummyPayload = Buffer.alloc(10000, 'yolo');
|
||||||
const server = net.createServer(function(socket) {
|
|
||||||
socket.write(yolo);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(0, common.mustCall(function() {
|
let runs = 0;
|
||||||
const { port } = server.address();
|
|
||||||
let runs = 0;
|
|
||||||
connect();
|
|
||||||
|
|
||||||
function connect() {
|
// Count garbage-collected TLS sockets.
|
||||||
|
let gced = 0;
|
||||||
|
function ongc() { gced++; }
|
||||||
|
|
||||||
|
connect();
|
||||||
|
|
||||||
|
function connect() {
|
||||||
|
if (runs % 64 === 0)
|
||||||
global.gc();
|
global.gc();
|
||||||
assert(process.memoryUsage().external >= 0);
|
const externalMemoryUsage = process.memoryUsage().external;
|
||||||
if (runs++ < 512)
|
assert(externalMemoryUsage >= 0, `${externalMemoryUsage} < 0`);
|
||||||
tls.connect(port).on('error', connect);
|
if (runs++ === 512) {
|
||||||
else
|
// Make sure at least half the TLS sockets have been gargbage collected
|
||||||
server.close();
|
// (so that this test can actually check what it's testing):
|
||||||
|
assert(gced >= 256, `${gced} < 256`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}));
|
|
||||||
|
const { clientSide, serverSide } = makeDuplexPair();
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user