2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2012-11-24 15:24:36 +01:00
|
|
|
// Flags: --expose-gc
|
|
|
|
|
2016-12-30 18:38:06 -05:00
|
|
|
const common = require('../common');
|
2024-08-13 18:28:21 +08:00
|
|
|
const { onGC } = require('../common/gc');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const net = require('net');
|
2012-11-24 15:24:36 +01:00
|
|
|
|
2018-07-13 18:32:35 +02:00
|
|
|
// Test that the implicit listener for an 'connect' event on net.Sockets is
|
|
|
|
// added using `once()`, i.e. can be gc'ed once that event has occurred.
|
|
|
|
|
|
|
|
const server = net.createServer(common.mustCall()).listen(0);
|
|
|
|
|
|
|
|
let collected = false;
|
|
|
|
const gcListener = { ongc() { collected = true; } };
|
|
|
|
|
2016-07-12 19:47:32 -04:00
|
|
|
{
|
2018-07-13 18:32:35 +02:00
|
|
|
const gcObject = {};
|
2018-08-21 13:38:59 -07:00
|
|
|
onGC(gcObject, gcListener);
|
2012-11-24 15:24:36 +01:00
|
|
|
|
2018-07-13 18:32:35 +02:00
|
|
|
const sock = net.createConnection(
|
|
|
|
server.address().port,
|
|
|
|
common.mustCall(() => {
|
2019-03-22 03:44:26 +01:00
|
|
|
assert.strictEqual(gcObject, gcObject); // Keep reference alive
|
2018-07-13 18:32:35 +02:00
|
|
|
assert.strictEqual(collected, false);
|
|
|
|
setImmediate(done, sock);
|
|
|
|
}));
|
2016-07-12 19:47:32 -04:00
|
|
|
}
|
2012-11-24 15:24:36 +01:00
|
|
|
|
2018-07-13 18:32:35 +02:00
|
|
|
function done(sock) {
|
2025-01-22 15:30:30 -08:00
|
|
|
globalThis.gc();
|
2018-07-13 18:32:35 +02:00
|
|
|
setImmediate(() => {
|
|
|
|
assert.strictEqual(collected, true);
|
|
|
|
sock.end();
|
|
|
|
server.close();
|
|
|
|
});
|
2012-11-24 15:24:36 +01:00
|
|
|
}
|