2019-02-07 21:19:07 +01:00
|
|
|
// Flags: --expose-gc
|
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
2024-08-13 12:28:21 +02:00
|
|
|
const { onGC } = require('../common/gc');
|
|
|
|
const { gcUntil } = require('../common/gc');
|
2019-02-07 21:19:07 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const async_hooks = require('async_hooks');
|
|
|
|
const domain = require('domain');
|
|
|
|
const EventEmitter = require('events');
|
2019-02-20 01:00:06 +01:00
|
|
|
const isEnumerable = Function.call.bind(Object.prototype.propertyIsEnumerable);
|
2019-02-07 21:19:07 +01:00
|
|
|
|
|
|
|
// This test makes sure that the (async id → domain) map which is part of the
|
|
|
|
// domain module does not get in the way of garbage collection.
|
|
|
|
// See: https://github.com/nodejs/node/issues/23862
|
|
|
|
|
|
|
|
let d = domain.create();
|
2023-08-07 16:24:06 +02:00
|
|
|
let resourceGCed = false; let domainGCed = false; let
|
|
|
|
emitterGCed = false;
|
2019-02-07 21:19:07 +01:00
|
|
|
d.run(() => {
|
|
|
|
const resource = new async_hooks.AsyncResource('TestResource');
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
|
|
|
|
d.remove(emitter);
|
|
|
|
d.add(emitter);
|
|
|
|
|
|
|
|
emitter.linkToResource = resource;
|
|
|
|
assert.strictEqual(emitter.domain, d);
|
2019-02-20 01:00:06 +01:00
|
|
|
assert.strictEqual(isEnumerable(emitter, 'domain'), false);
|
2019-02-07 21:19:07 +01:00
|
|
|
assert.strictEqual(resource.domain, d);
|
2019-02-20 01:00:06 +01:00
|
|
|
assert.strictEqual(isEnumerable(resource, 'domain'), false);
|
2019-02-07 21:19:07 +01:00
|
|
|
|
|
|
|
// This would otherwise be a circular chain now:
|
|
|
|
// emitter → resource → async id ⇒ domain → emitter.
|
|
|
|
// Make sure that all of these objects are released:
|
|
|
|
|
2023-08-07 16:24:06 +02:00
|
|
|
onGC(resource, { ongc: common.mustCall(() => { resourceGCed = true; }) });
|
|
|
|
onGC(d, { ongc: common.mustCall(() => { domainGCed = true; }) });
|
|
|
|
onGC(emitter, { ongc: common.mustCall(() => { emitterGCed = true; }) });
|
2019-02-07 21:19:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
d = null;
|
2023-08-07 16:24:06 +02:00
|
|
|
|
|
|
|
async function main() {
|
2024-08-13 12:28:21 +02:00
|
|
|
await gcUntil(
|
2023-08-07 16:24:06 +02:00
|
|
|
'All objects garbage collected',
|
|
|
|
() => resourceGCed && domainGCed && emitterGCed);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|