Skip to content

Commit

Permalink
test: add test for async contexts in PerformanceObserver
Browse files Browse the repository at this point in the history
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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ZauberNerd authored and targos committed Dec 21, 2020
1 parent f368d69 commit 0f749a3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/parallel/test-performanceobserver-asynccontext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const {
performance,
PerformanceObserver,
} = require('perf_hooks');
const {
executionAsyncId,
triggerAsyncId,
executionAsyncResource,
} = require('async_hooks');

// Test Non-Buffered PerformanceObserver retains async context
{
const observer =
new PerformanceObserver(common.mustCall(callback));

const initialAsyncId = executionAsyncId();
let asyncIdInTimeout;
let asyncResourceInTimeout;

function callback(list) {
assert.strictEqual(triggerAsyncId(), initialAsyncId);
assert.strictEqual(executionAsyncId(), asyncIdInTimeout);
assert.strictEqual(executionAsyncResource(), asyncResourceInTimeout);
observer.disconnect();
}
observer.observe({ entryTypes: ['mark'] });

setTimeout(() => {
asyncIdInTimeout = executionAsyncId();
asyncResourceInTimeout = executionAsyncResource();
performance.mark('test1');
}, 0);
}

0 comments on commit 0f749a3

Please sign in to comment.