From ef0f5b185d318c48f4331797e7be86241d676902 Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Wed, 2 Dec 2020 23:04:15 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/36343 PR-URL: https://github.com/nodejs/node/pull/36343 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- .../test-performanceobserver-asynccontext.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/parallel/test-performanceobserver-asynccontext.js diff --git a/test/parallel/test-performanceobserver-asynccontext.js b/test/parallel/test-performanceobserver-asynccontext.js new file mode 100644 index 00000000000000..d3b5bec925d2c3 --- /dev/null +++ b/test/parallel/test-performanceobserver-asynccontext.js @@ -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); +}