Skip to content

Commit 0f749a3

Browse files
ZauberNerdtargos
authored andcommitted
test: add test for async contexts in PerformanceObserver
This test proves that the PerformanceObserver callback gets called with the async context of the callsite of performance.mark()/measure() and therefore AsyncLocalStorage can be used inside a PerformanceObserver. PR: #36343 PR-URL: #36343 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent f368d69 commit 0f749a3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const {
6+
performance,
7+
PerformanceObserver,
8+
} = require('perf_hooks');
9+
const {
10+
executionAsyncId,
11+
triggerAsyncId,
12+
executionAsyncResource,
13+
} = require('async_hooks');
14+
15+
// Test Non-Buffered PerformanceObserver retains async context
16+
{
17+
const observer =
18+
new PerformanceObserver(common.mustCall(callback));
19+
20+
const initialAsyncId = executionAsyncId();
21+
let asyncIdInTimeout;
22+
let asyncResourceInTimeout;
23+
24+
function callback(list) {
25+
assert.strictEqual(triggerAsyncId(), initialAsyncId);
26+
assert.strictEqual(executionAsyncId(), asyncIdInTimeout);
27+
assert.strictEqual(executionAsyncResource(), asyncResourceInTimeout);
28+
observer.disconnect();
29+
}
30+
observer.observe({ entryTypes: ['mark'] });
31+
32+
setTimeout(() => {
33+
asyncIdInTimeout = executionAsyncId();
34+
asyncResourceInTimeout = executionAsyncResource();
35+
performance.mark('test1');
36+
}, 0);
37+
}

0 commit comments

Comments
 (0)