|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const fs = require('fs'); |
| 6 | +const { AsyncResource, executionAsyncResource } = require('async_hooks'); |
| 7 | +const { internalBinding } = require('internal/test/binding'); |
| 8 | +const { FSReqCallback } = internalBinding('fs'); |
| 9 | +const { async_hook_fields, constants: { kLazyScopes } } = internalBinding('async_wrap'); |
| 10 | + |
| 11 | +// No hook and no executionAsyncResource() yet, so the scope of this callback skips the id stack. |
| 12 | +// Asked for here, its resource comes off the chain of lazy scopes, and for an AsyncWrap the |
| 13 | +// resource is held as a Global. |
| 14 | +fs.stat(__filename, common.mustSucceed(() => { |
| 15 | + assert.ok(async_hook_fields[kLazyScopes] > 0); |
| 16 | + const resource = executionAsyncResource(); |
| 17 | + assert.ok(resource instanceof FSReqCallback); |
| 18 | + assert.strictEqual(executionAsyncResource(), resource); |
| 19 | + |
| 20 | + // A context pushed on the stack while the lazy scope is still open: the stack answers, not the |
| 21 | + // chain. |
| 22 | + const inner = new AsyncResource('TEST'); |
| 23 | + inner.runInAsyncScope(common.mustCall(() => { |
| 24 | + assert.strictEqual(executionAsyncResource(), inner); |
| 25 | + })); |
| 26 | + assert.strictEqual(executionAsyncResource(), resource); |
| 27 | +})); |
0 commit comments