Skip to content

Commit 21a6ea2

Browse files
committed
test: cover the resource lookup of a lazy async scope
executionAsyncResource() asked first inside a native callback, where the scope skipped the id stack and holds its resource as a Global, and with a context pushed on the stack above that scope. Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
1 parent 5bd5449 commit 21a6ea2

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

Comments
 (0)