xref: /third_party/node/test/parallel/test-async-hooks-destroy-on-gc.js
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/node/test/parallel/
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose_gc
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test ensures that userland-only AsyncResources cause a destroy event to
51cb0ef41Sopenharmony_ci// be emitted when they get gced.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst destroyedIds = new Set();
121cb0ef41Sopenharmony_ciasync_hooks.createHook({
131cb0ef41Sopenharmony_ci  destroy: common.mustCallAtLeast((asyncId) => {
141cb0ef41Sopenharmony_ci    destroyedIds.add(asyncId);
151cb0ef41Sopenharmony_ci  }, 1)
161cb0ef41Sopenharmony_ci}).enable();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cilet asyncId = null;
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const res = new async_hooks.AsyncResource('foobar');
211cb0ef41Sopenharmony_ci  asyncId = res.asyncId();
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cisetImmediate(() => {
251cb0ef41Sopenharmony_ci  global.gc();
261cb0ef41Sopenharmony_ci  setImmediate(() => assert.ok(destroyedIds.has(asyncId)));
271cb0ef41Sopenharmony_ci});
28

Indexes created Thu Nov 07 10:32:03 CST 2024