0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-process-getactiveresources-track-active-handles.js
Darshan Sen 0d9f3bd9e8
process: add getActiveResourcesInfo()
This is supposed to be a public alternative of the private APIs,
`process._getActiveResources()` and `process._getActiveHandles()`. When
called, it returns an array of strings containing the types of the
active resources that are currently keeping the event loop alive.

Signed-off-by: Darshan Sen <darshan.sen@postman.com>

PR-URL: https://github.com/nodejs/node/pull/40813
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2021-12-14 14:09:08 +00:00

45 lines
1008 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const net = require('net');
const NUM = 8;
const connections = [];
const clients = [];
let clients_counter = 0;
const server = net.createServer(function listener(c) {
connections.push(c);
}).listen(0, makeConnection);
function makeConnection() {
if (clients_counter >= NUM) return;
net.connect(server.address().port, function connected() {
clientConnected(this);
makeConnection();
});
}
function clientConnected(client) {
clients.push(client);
if (++clients_counter >= NUM)
checkAll();
}
function checkAll() {
assert.strictEqual(process.getActiveResourcesInfo().filter(
(type) => type === 'TCPSocketWrap').length,
clients.length + connections.length);
clients.forEach((item) => item.destroy());
connections.forEach((item) => item.end());
assert.strictEqual(process.getActiveResourcesInfo().filter(
(type) => type === 'TCPServerWrap').length, 1);
server.close();
}