1// Flags: --expose-gc 2'use strict'; 3const common = require('../common'); 4const assert = require('assert'); 5const async_hooks = require('async_hooks'); 6const v8 = require('v8'); 7 8// Regression test for https://github.com/nodejs/node/issues/28786 9// Make sure that creating a heap snapshot inside an async_hooks hook 10// works for Promises. 11 12const createSnapshot = common.mustCall(() => { 13 v8.getHeapSnapshot().resume(); 14}, 8); // 2 × init + 2 × resolve + 1 × (after + before) + 2 × destroy = 8 calls 15 16const promiseIds = []; 17 18async_hooks.createHook({ 19 init(id, type) { 20 if (type === 'PROMISE') { 21 createSnapshot(); 22 promiseIds.push(id); 23 } 24 }, 25 26 before(id) { 27 if (promiseIds.includes(id)) createSnapshot(); 28 }, 29 30 after(id) { 31 if (promiseIds.includes(id)) createSnapshot(); 32 }, 33 34 promiseResolve(id) { 35 assert(promiseIds.includes(id)); 36 createSnapshot(); 37 }, 38 39 destroy(id) { 40 if (promiseIds.includes(id)) createSnapshot(); 41 } 42}).enable(); 43 44 45Promise.resolve().then(() => {}); 46setImmediate(global.gc); 47