2019-06-02 17:11:19 +00:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const async_hooks = require('async_hooks');
|
|
|
|
|
|
|
|
// Checks that enabling async hooks in a callback actually
|
|
|
|
// triggers after & destroy as expected.
|
|
|
|
|
|
|
|
const fnsToTest = [setTimeout, (cb) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
cb();
|
|
|
|
|
|
|
|
// We need to keep the event loop open for this to actually work
|
|
|
|
// since destroy hooks are triggered in unrefed Immediates
|
|
|
|
setImmediate(() => {
|
|
|
|
hook.disable();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, (cb) => {
|
2020-07-14 17:06:37 +00:00
|
|
|
setImmediate(() => {
|
|
|
|
process.nextTick(() => {
|
|
|
|
cb();
|
2019-06-02 17:11:19 +00:00
|
|
|
|
2020-07-14 17:06:37 +00:00
|
|
|
// We need to keep the event loop open for this to actually work
|
|
|
|
// since destroy hooks are triggered in unrefed Immediates
|
|
|
|
setImmediate(() => {
|
|
|
|
hook.disable();
|
|
|
|
assert.strictEqual(fnsToTest.length, 0);
|
|
|
|
});
|
2019-06-02 17:11:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}];
|
|
|
|
|
|
|
|
const hook = async_hooks.createHook({
|
|
|
|
before: common.mustNotCall(),
|
2022-10-18 12:13:41 +00:00
|
|
|
after: common.mustCall(3),
|
2019-06-02 17:11:19 +00:00
|
|
|
destroy: common.mustCall(() => {
|
|
|
|
hook.disable();
|
|
|
|
nextTest();
|
2022-11-21 17:43:47 +00:00
|
|
|
}, 3),
|
2019-06-02 17:11:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
nextTest();
|
|
|
|
|
|
|
|
function nextTest() {
|
|
|
|
if (fnsToTest.length > 0) {
|
|
|
|
fnsToTest.shift()(common.mustCall(() => {
|
|
|
|
hook.enable();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|