1'use strict'; 2 3const common = require('../../common'); 4const assert = require('assert'); 5const async_hooks = require('async_hooks'); 6 7// The async_hook that we enable would register the process.emitWarning() 8// call from loading the N-API addon as asynchronous activity because 9// it contains a process.nextTick() call. Monkey patch it to be a no-op 10// before we load the addon in order to avoid this. 11process.emitWarning = () => {}; 12 13const { runInCallbackScope } = require(`./build/${common.buildType}/binding`); 14 15const expectedResource = {}; 16const expectedResourceType = 'test-resource'; 17let insideHook = false; 18let expectedId; 19async_hooks.createHook({ 20 init: common.mustCall((id, type, triggerAsyncId, resource) => { 21 if (type !== expectedResourceType) { 22 return; 23 } 24 assert.strictEqual(resource, expectedResource); 25 expectedId = id; 26 }), 27 before: common.mustCall((id) => { 28 assert.strictEqual(id, expectedId); 29 insideHook = true; 30 }), 31 after: common.mustCall((id) => { 32 assert.strictEqual(id, expectedId); 33 insideHook = false; 34 }) 35}).enable(); 36 37runInCallbackScope(expectedResource, expectedResourceType, () => { 38 assert(insideHook); 39}); 40