mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 04:28:50 +00:00

The test addons/async-hooks-promise depends on there being only one hook available. So skip it if NODE_TEST_WITH_ASYNC_HOOKS is set. PR-URL: https://github.com/nodejs/node/pull/14208 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('async_hooks');
|
|
const binding = require(`./build/${common.buildType}/binding`);
|
|
|
|
if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
|
|
common.skip('cannot test with env var NODE_TEST_WITH_ASYNC_HOOKS');
|
|
return;
|
|
}
|
|
|
|
// Baseline to make sure the internal field isn't being set.
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|
|
|
|
const hook0 = async_hooks.createHook({}).enable();
|
|
|
|
// Check that no PromiseWrap is created when there are no hook callbacks.
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|
|
|
|
hook0.disable();
|
|
|
|
let pwrap = null;
|
|
const hook1 = async_hooks.createHook({
|
|
init(id, type, tid, resource) {
|
|
pwrap = resource;
|
|
}
|
|
}).enable();
|
|
|
|
// Check that the internal field returns the same PromiseWrap passed to init().
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
pwrap,
|
|
'Unexpected PromiseWrap');
|
|
|
|
hook1.disable();
|
|
|
|
// Check that internal fields are no longer being set. This needs to be delayed
|
|
// a bit because the `disable()` call only schedules disabling the hook in a
|
|
// future microtask.
|
|
setImmediate(() => {
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|
|
});
|