11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst onGC = require('../common/ongc');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
71cb0ef41Sopenharmony_ciconst domain = require('domain');
81cb0ef41Sopenharmony_ciconst EventEmitter = require('events');
91cb0ef41Sopenharmony_ciconst isEnumerable = Function.call.bind(Object.prototype.propertyIsEnumerable);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// This test makes sure that the (async id → domain) map which is part of the
121cb0ef41Sopenharmony_ci// domain module does not get in the way of garbage collection.
131cb0ef41Sopenharmony_ci// See: https://github.com/nodejs/node/issues/23862
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cilet d = domain.create();
161cb0ef41Sopenharmony_cid.run(() => {
171cb0ef41Sopenharmony_ci  const resource = new async_hooks.AsyncResource('TestResource');
181cb0ef41Sopenharmony_ci  const emitter = new EventEmitter();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  d.remove(emitter);
211cb0ef41Sopenharmony_ci  d.add(emitter);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  emitter.linkToResource = resource;
241cb0ef41Sopenharmony_ci  assert.strictEqual(emitter.domain, d);
251cb0ef41Sopenharmony_ci  assert.strictEqual(isEnumerable(emitter, 'domain'), false);
261cb0ef41Sopenharmony_ci  assert.strictEqual(resource.domain, d);
271cb0ef41Sopenharmony_ci  assert.strictEqual(isEnumerable(resource, 'domain'), false);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // This would otherwise be a circular chain now:
301cb0ef41Sopenharmony_ci  // emitter → resource → async id ⇒ domain → emitter.
311cb0ef41Sopenharmony_ci  // Make sure that all of these objects are released:
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  onGC(resource, { ongc: common.mustCall() });
341cb0ef41Sopenharmony_ci  onGC(d, { ongc: common.mustCall() });
351cb0ef41Sopenharmony_ci  onGC(emitter, { ongc: common.mustCall() });
361cb0ef41Sopenharmony_ci});
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cid = null;
391cb0ef41Sopenharmony_ciglobal.gc();
40