Skip to content

Commit d85bb91

Browse files
committed
async hooks tests
1 parent d6c483e commit d85bb91

29 files changed

+302
-76
lines changed

test/async-hooks/init-hooks.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ if (typeof global.gc === 'function') {
1818

1919
function noop() {}
2020

21+
const bootstrapIds = new Set();
22+
let firstTriggerAsyncId;
23+
2124
class ActivityCollector {
2225
constructor(start, {
2326
allowNoInit = false,
@@ -61,6 +64,10 @@ class ActivityCollector {
6164
this._asyncHook.disable();
6265
}
6366

67+
get firstTriggerAsyncId() {
68+
return firstTriggerAsyncId;
69+
}
70+
6471
sanityCheck(types) {
6572
if (types != null && !Array.isArray(types)) types = [ types ];
6673

@@ -177,7 +184,13 @@ class ActivityCollector {
177184
return h;
178185
}
179186

180-
_init(uid, type, triggerAsyncId, handle) {
187+
_init(uid, type, triggerAsyncId, handle, bootstrap) {
188+
if (bootstrap) {
189+
bootstrapIds.add(uid);
190+
return;
191+
} else if (!firstTriggerAsyncId) {
192+
firstTriggerAsyncId = triggerAsyncId;
193+
}
181194
const activity = {
182195
uid,
183196
type,
@@ -190,38 +203,43 @@ class ActivityCollector {
190203
this._stamp(activity, 'init');
191204
this._activities.set(uid, activity);
192205
this._maybeLog(uid, type, 'init');
193-
this.oninit(uid, type, triggerAsyncId, handle);
206+
this.oninit(uid, type, triggerAsyncId, handle, bootstrap);
194207
}
195208

196209
_before(uid) {
210+
if (bootstrapIds.has(uid)) return;
197211
const h = this._getActivity(uid, 'before');
198212
this._stamp(h, 'before');
199213
this._maybeLog(uid, h && h.type, 'before');
200214
this.onbefore(uid);
201215
}
202216

203217
_after(uid) {
218+
if (bootstrapIds.has(uid)) return;
204219
const h = this._getActivity(uid, 'after');
205220
this._stamp(h, 'after');
206221
this._maybeLog(uid, h && h.type, 'after');
207222
this.onafter(uid);
208223
}
209224

210225
_destroy(uid) {
226+
if (bootstrapIds.has(uid)) return;
211227
const h = this._getActivity(uid, 'destroy');
212228
this._stamp(h, 'destroy');
213229
this._maybeLog(uid, h && h.type, 'destroy');
214230
this.ondestroy(uid);
215231
}
216232

217233
_promiseResolve(uid) {
234+
if (bootstrapIds.has(uid)) return;
218235
const h = this._getActivity(uid, 'promiseResolve');
219236
this._stamp(h, 'promiseResolve');
220237
this._maybeLog(uid, h && h.type, 'promiseResolve');
221238
this.onpromiseResolve(uid);
222239
}
223240

224241
_maybeLog(uid, type, name) {
242+
if (bootstrapIds.has(uid)) return;
225243
if (this._logid &&
226244
(type == null || this._logtype == null || this._logtype === type)) {
227245
print(`${this._logid}.${name}.uid-${uid}`);

test/async-hooks/test-async-await.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const initHooks = require('./init-hooks');
1212
const util = require('util');
1313

1414
const sleep = util.promisify(setTimeout);
15+
1516
// Either 'inited' or 'resolved'
1617
const promisesInitState = new Map();
1718
// Either 'before' or 'after' AND asyncId must be present in the other map
@@ -26,7 +27,7 @@ const hooks = initHooks({
2627
});
2728
hooks.enable();
2829

29-
function oninit(asyncId, type) {
30+
function oninit(asyncId, type, triggerAsyncId, resource) {
3031
if (type === 'PROMISE') {
3132
promisesInitState.set(asyncId, 'inited');
3233
}

test/async-hooks/test-crypto-pbkdf2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function onexit() {
3737
const a = as[0];
3838
assert.strictEqual(a.type, 'PBKDF2REQUEST');
3939
assert.strictEqual(typeof a.uid, 'number');
40-
assert.strictEqual(a.triggerAsyncId, 1);
40+
assert.strictEqual(a.triggerAsyncId, hooks.firstTriggerAsyncId);
4141
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
4242
'when process exits');
4343
}

test/async-hooks/test-crypto-randomBytes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function onexit() {
3838
const a = as[0];
3939
assert.strictEqual(a.type, 'RANDOMBYTESREQUEST');
4040
assert.strictEqual(typeof a.uid, 'number');
41-
assert.strictEqual(a.triggerAsyncId, 1);
41+
assert.strictEqual(a.triggerAsyncId, hooks.firstTriggerAsyncId);
4242
checkInvocations(a, { init: 1, before: 1, after: 1, destroy: 1 },
4343
'when process exits');
4444
}

test/async-hooks/test-disable-in-init.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@
33
const common = require('../common');
44
const async_hooks = require('async_hooks');
55
const fs = require('fs');
6+
const assert = require('assert');
67

78
let nestedCall = false;
9+
let cnt = 0;
810

911
async_hooks.createHook({
10-
init: common.mustCall(function() {
12+
init: function(asyncId, type, triggerAsyncId, handle, bootstrap) {
13+
if (type === 'TickObject' || type === 'PROMISE')
14+
return;
15+
cnt++;
1116
nestedHook.disable();
1217
if (!nestedCall) {
1318
nestedCall = true;
1419
fs.access(__filename, common.mustCall());
1520
}
16-
}, 2)
21+
}
1722
}).enable();
1823

1924
const nestedHook = async_hooks.createHook({
20-
init: common.mustCall(2)
25+
init: function(asyncId, type, triggerAsyncId, handle, bootstrap) {
26+
cnt++;
27+
}
2128
}).enable();
2229

2330
fs.access(__filename, common.mustCall());
31+
32+
assert.strictEqual(cnt, 4);

test/async-hooks/test-emit-before-after.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,33 @@ const expectedType = 'test_emit_before_after_type';
4545
async_hooks.emitBefore(expectedId, expectedTriggerId);
4646
async_hooks.emitAfter(expectedId);
4747

48-
initHooks({
49-
onbefore: common.mustCall((id) => assert.strictEqual(id, expectedId)),
50-
onafter: common.mustCall((id) => assert.strictEqual(id, expectedId)),
48+
const ignoreIds = new Set();
49+
const initIds = new Map();
50+
51+
let okCnt = 0;
52+
const hooks = initHooks({
53+
oninit(id, type) {
54+
if (type === 'TickObject' || type === 'PROMISE') {
55+
ignoreIds.add(id);
56+
} else {
57+
initIds.set(id, type);
58+
}
59+
},
60+
onbefore(id) {
61+
if (ignoreIds.has(id) || !initIds.has(id)) return;
62+
assert.strictEqual(id, expectedId);
63+
okCnt++;
64+
},
65+
onafter(id) {
66+
if (ignoreIds.has(id) || !initIds.has(id)) return;
67+
assert.strictEqual(id, expectedId);
68+
okCnt++;
69+
},
5170
allowNoInit: true
52-
}).enable();
71+
});
72+
hooks.enable();
5373

5474
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId);
5575
async_hooks.emitBefore(expectedId, expectedTriggerId);
5676
async_hooks.emitAfter(expectedId);
77+
assert.strictEqual(okCnt, 2);

test/async-hooks/test-emit-init.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,18 @@ async_hooks.emitInit(expectedId, expectedType, expectedTriggerId,
6767

6868
hooks1.disable();
6969

70-
initHooks({
71-
oninit: common.mustCall((id, type, triggerAsyncId, resource) => {
70+
let okCnt = 0;
71+
const hooks = initHooks({
72+
oninit: (id, type, triggerAsyncId, resource) => {
73+
if (type === 'PROMISE' || type === 'TickObject') return;
7274
assert.strictEqual(id, expectedId);
7375
assert.strictEqual(type, expectedType);
7476
assert.notStrictEqual(triggerAsyncId, expectedTriggerId);
7577
assert.strictEqual(resource.key, expectedResource.key);
76-
})
77-
}).enable();
78+
okCnt++;
79+
}
80+
});
81+
hooks.enable();
7882

7983
async_hooks.emitInit(expectedId, expectedType, null, expectedResource);
84+
assert.strictEqual(okCnt, 1);

test/async-hooks/test-enable-in-init.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
const common = require('../common');
44
const async_hooks = require('async_hooks');
55
const fs = require('fs');
6+
const assert = require('assert');
67

78
const nestedHook = async_hooks.createHook({
89
init: common.mustNotCall()
910
});
1011
let nestedCall = false;
1112

13+
let cnt = 0;
1214
async_hooks.createHook({
13-
init: common.mustCall(() => {
15+
init(asyncId, type) {
16+
if (type === 'PROMISE' || type === 'TickObject')
17+
return;
18+
cnt++;
1419
nestedHook.enable();
1520
if (!nestedCall) {
1621
nestedCall = true;
1722
fs.access(__filename, common.mustCall());
1823
}
19-
}, 2)
24+
}
2025
}).enable();
2126

2227
fs.access(__filename, common.mustCall());
28+
29+
assert.strictEqual(cnt, 2);

test/async-hooks/test-fseventwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ function onexit() {
3131
const a = as[0];
3232
assert.strictEqual(a.type, 'FSEVENTWRAP');
3333
assert.strictEqual(typeof a.uid, 'number');
34-
assert.strictEqual(a.triggerAsyncId, 1);
34+
assert.strictEqual(a.triggerAsyncId, hooks.firstTriggerAsyncId);
3535
checkInvocations(a, { init: 1, destroy: 1 }, 'when process exits');
3636
}

test/async-hooks/test-fsreqcallback-readFile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fs.readFile(__filename, common.mustCall(onread));
1717

1818
function onread() {
1919
const as = hooks.activitiesOfTypes('FSREQCALLBACK');
20-
let lastParent = 1;
20+
let lastParent = hooks.firstTriggerAsyncId;
2121
for (let i = 0; i < as.length; i++) {
2222
const a = as[i];
2323
assert.strictEqual(a.type, 'FSREQCALLBACK');

test/async-hooks/test-getaddrinforeqwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function onlookup() {
2424
const a = as[0];
2525
assert.strictEqual(a.type, 'GETADDRINFOREQWRAP');
2626
assert.strictEqual(typeof a.uid, 'number');
27-
assert.strictEqual(a.triggerAsyncId, 1);
27+
assert.strictEqual(a.triggerAsyncId, hooks.firstTriggerAsyncId);
2828
checkInvocations(a, { init: 1, before: 1 }, 'while in onlookup callback');
2929
tick(2);
3030
}

test/async-hooks/test-getnameinforeqwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function onlookupService() {
2424
const a = as[0];
2525
assert.strictEqual(a.type, 'GETNAMEINFOREQWRAP');
2626
assert.strictEqual(typeof a.uid, 'number');
27-
assert.strictEqual(a.triggerAsyncId, 1);
27+
assert.strictEqual(a.triggerAsyncId, hooks.firstTriggerAsyncId);
2828
checkInvocations(a, { init: 1, before: 1 },
2929
'while in onlookupService callback');
3030
tick(2);

test/async-hooks/test-late-hook-enable.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ const fnsToTest = [setTimeout, (cb) => {
2929
});
3030
}];
3131

32+
let beforeCnt = 0;
33+
let afterCnt = 0;
34+
let destroyCnt = 0;
35+
3236
const hook = async_hooks.createHook({
33-
before: common.mustNotCall(),
34-
after: common.mustCall(() => {}, 3),
35-
destroy: common.mustCall(() => {
37+
before(asyncId) {
38+
beforeCnt++;
39+
},
40+
after(asyncId) {
41+
afterCnt++;
42+
},
43+
destroy(asyncId) {
44+
destroyCnt++;
3645
hook.disable();
3746
nextTest();
38-
}, 3)
47+
}
3948
});
4049

4150
nextTest();
@@ -47,3 +56,9 @@ function nextTest() {
4756
}));
4857
}
4958
}
59+
60+
process.on('nextTick', () => {
61+
assert.strictEqual(beforeCnt, 0);
62+
assert.strictEqual(afterCnt, 3);
63+
assert.strictEqual(destroyCnt, 3);
64+
});

test/async-hooks/test-pipewrap.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ const pipe2 = pipes[1];
3535
const pipe3 = pipes[2];
3636

3737
assert.strictEqual(processwrap.type, 'PROCESSWRAP');
38-
assert.strictEqual(processwrap.triggerAsyncId, 1);
38+
assert.strictEqual(processwrap.triggerAsyncId, hooks.firstTriggerAsyncId);
3939
checkInvocations(processwrap, { init: 1 },
4040
'processwrap when sleep.spawn was called');
4141

4242
[ pipe1, pipe2, pipe3 ].forEach((x) => {
4343
assert.strictEqual(x.type, 'PIPEWRAP');
44-
assert.strictEqual(x.triggerAsyncId, 1);
44+
assert.strictEqual(x.triggerAsyncId, hooks.firstTriggerAsyncId);
4545
checkInvocations(x, { init: 1 }, 'pipe wrap when sleep.spawn was called');
4646
});
4747

@@ -73,7 +73,7 @@ function onexit() {
7373

7474
[ pipe1, pipe2, pipe3 ].forEach((x) => {
7575
assert.strictEqual(x.type, 'PIPEWRAP');
76-
assert.strictEqual(x.triggerAsyncId, 1);
76+
assert.strictEqual(x.triggerAsyncId, hooks.firstTriggerAsyncId);
7777
});
7878

7979
const ioEvents = Math.min(pipe2.before.length, pipe2.after.length);

test/async-hooks/test-promise.chain-promise-before-init-hooks.js

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const hooks = initHooks();
2222
hooks._allowNoInit = true;
2323
hooks.enable();
2424

25-
2625
process.on('exit', function onexit() {
2726
hooks.disable();
2827
hooks.sanityCheck('PROMISE');

test/async-hooks/test-promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function onexit() {
4242
const a0 = as[0];
4343
assert.strictEqual(a0.type, 'PROMISE');
4444
assert.strictEqual(typeof a0.uid, 'number');
45-
assert.strictEqual(a0.triggerAsyncId, 1);
45+
assert.strictEqual(a0.triggerAsyncId, hooks.firstTriggerAsyncId);
4646
checkInvocations(a0, { init: 1 }, 'when process exits');
4747

4848
const a1 = as[1];

test/async-hooks/test-statwatcher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ assert.strictEqual(as.length, 1);
3535
const statwatcher1 = as[0];
3636
assert.strictEqual(statwatcher1.type, 'STATWATCHER');
3737
assert.strictEqual(typeof statwatcher1.uid, 'number');
38-
assert.strictEqual(statwatcher1.triggerAsyncId, 1);
38+
assert.strictEqual(statwatcher1.triggerAsyncId, hooks.firstTriggerAsyncId);
3939
checkInvocations(statwatcher1, { init: 1 },
4040
'watcher1: when started to watch file');
4141

@@ -53,7 +53,7 @@ assert.strictEqual(as.length, 2);
5353
const statwatcher2 = as[1];
5454
assert.strictEqual(statwatcher2.type, 'STATWATCHER');
5555
assert.strictEqual(typeof statwatcher2.uid, 'number');
56-
assert.strictEqual(statwatcher2.triggerAsyncId, 1);
56+
assert.strictEqual(statwatcher2.triggerAsyncId, hooks.firstTriggerAsyncId);
5757
checkInvocations(statwatcher1, { init: 1 },
5858
'watcher1: when started to watch second file');
5959
checkInvocations(statwatcher2, { init: 1 },

test/common/ongc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ function onGC(obj, gcListener) {
99
const async_hooks = require('async_hooks');
1010

1111
const onGcAsyncHook = async_hooks.createHook({
12-
init: common.mustCallAtLeast(function(id, type) {
12+
init: common.mustCallAtLeast(function(id, type, triggerAsncId, resource,
13+
bootstrap) {
14+
if (bootstrap) return;
1315
if (this.trackedId === undefined) {
1416
assert.strictEqual(type, gcTrackerTag);
1517
this.trackedId = id;

test/parallel/test-async-hooks-async-await.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const assert = require('assert');
88

99
const asyncIds = [];
1010
async_hooks.createHook({
11-
init: (asyncId, type, triggerAsyncId) => {
11+
init: (asyncId, type, triggerAsyncId, resource, bootstrap) => {
12+
if (bootstrap) return;
1213
asyncIds.push([triggerAsyncId, asyncId]);
1314
}
1415
}).enable();

0 commit comments

Comments
 (0)