1'use strict'; 2// Flags: --gc-interval=100 --gc-global 3 4const common = require('../../common'); 5const assert = require('assert'); 6const async_hooks = require('async_hooks'); 7const { createAsyncResource } = require(`./build/${common.buildType}/binding`); 8 9// Test for https://github.com/nodejs/node/issues/27218: 10// napi_async_destroy() can be called during a regular garbage collection run. 11 12const hook_result = { 13 id: null, 14 init_called: false, 15 destroy_called: false, 16}; 17 18const test_hook = async_hooks.createHook({ 19 init: (id, type) => { 20 if (type === 'test_async') { 21 hook_result.id = id; 22 hook_result.init_called = true; 23 } 24 }, 25 destroy: (id) => { 26 if (id === hook_result.id) hook_result.destroy_called = true; 27 }, 28}); 29 30test_hook.enable(); 31createAsyncResource({}); 32 33// Trigger GC. This does *not* use global.gc(), because what we want to verify 34// is that `napi_async_destroy()` can be called when there is no JS context 35// on the stack at the time of GC. 36// Currently, using --gc-interval=100 + 1M elements seems to work fine for this. 37const arr = new Array(1024 * 1024); 38for (let i = 0; i < arr.length; i++) 39 arr[i] = {}; 40 41assert.strictEqual(hook_result.destroy_called, false); 42setImmediate(() => { 43 assert.strictEqual(hook_result.destroy_called, true); 44}); 45