11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci// Flags: --gc-interval=100 --gc-global 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../../common'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks'); 71cb0ef41Sopenharmony_ciconst { createAsyncResource } = require(`./build/${common.buildType}/binding`); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// Test for https://github.com/nodejs/node/issues/27218: 101cb0ef41Sopenharmony_ci// napi_async_destroy() can be called during a regular garbage collection run. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst hook_result = { 131cb0ef41Sopenharmony_ci id: null, 141cb0ef41Sopenharmony_ci init_called: false, 151cb0ef41Sopenharmony_ci destroy_called: false, 161cb0ef41Sopenharmony_ci}; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst test_hook = async_hooks.createHook({ 191cb0ef41Sopenharmony_ci init: (id, type) => { 201cb0ef41Sopenharmony_ci if (type === 'test_async') { 211cb0ef41Sopenharmony_ci hook_result.id = id; 221cb0ef41Sopenharmony_ci hook_result.init_called = true; 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci }, 251cb0ef41Sopenharmony_ci destroy: (id) => { 261cb0ef41Sopenharmony_ci if (id === hook_result.id) hook_result.destroy_called = true; 271cb0ef41Sopenharmony_ci }, 281cb0ef41Sopenharmony_ci}); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_citest_hook.enable(); 311cb0ef41Sopenharmony_cicreateAsyncResource({}); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci// Trigger GC. This does *not* use global.gc(), because what we want to verify 341cb0ef41Sopenharmony_ci// is that `napi_async_destroy()` can be called when there is no JS context 351cb0ef41Sopenharmony_ci// on the stack at the time of GC. 361cb0ef41Sopenharmony_ci// Currently, using --gc-interval=100 + 1M elements seems to work fine for this. 371cb0ef41Sopenharmony_ciconst arr = new Array(1024 * 1024); 381cb0ef41Sopenharmony_cifor (let i = 0; i < arr.length; i++) 391cb0ef41Sopenharmony_ci arr[i] = {}; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciassert.strictEqual(hook_result.destroy_called, false); 421cb0ef41Sopenharmony_cisetImmediate(() => { 431cb0ef41Sopenharmony_ci assert.strictEqual(hook_result.destroy_called, true); 441cb0ef41Sopenharmony_ci}); 45