|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +// This test ensures that tracing channels create an independent mutable |
| 6 | +// context for each call while preserving explicitly provided contexts. |
| 7 | + |
| 8 | +const assert = require('node:assert'); |
| 9 | +const dc = require('node:diagnostics_channel'); |
| 10 | + |
| 11 | +const lengthChannel = dc.tracingChannel('test:length'); |
| 12 | +assert.strictEqual(lengthChannel.traceSync.length, 1); |
| 13 | +assert.strictEqual(lengthChannel.tracePromise.length, 1); |
| 14 | + |
| 15 | +function subscribe(channel, contexts, isPromise = false) { |
| 16 | + let endCalls = 0; |
| 17 | + let asyncStartCalls = 0; |
| 18 | + let asyncEndCalls = 0; |
| 19 | + |
| 20 | + channel.subscribe({ |
| 21 | + start: common.mustCall((context) => { |
| 22 | + context.invocation = contexts.length; |
| 23 | + contexts.push(context); |
| 24 | + }, 4), |
| 25 | + end: common.mustCall((context) => { |
| 26 | + assert.strictEqual(context.invocation, endCalls++); |
| 27 | + }, 4), |
| 28 | + asyncStart: common.mustCall((context) => { |
| 29 | + assert.strictEqual(context.invocation, asyncStartCalls++); |
| 30 | + }, isPromise ? 4 : 0), |
| 31 | + asyncEnd: common.mustCall((context) => { |
| 32 | + assert.strictEqual(context.invocation, asyncEndCalls++); |
| 33 | + }, isPromise ? 4 : 0), |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +const syncChannel = dc.tracingChannel('test:sync-context'); |
| 38 | +const syncContexts = []; |
| 39 | +subscribe(syncChannel, syncContexts); |
| 40 | + |
| 41 | +const syncProvided = { provided: true }; |
| 42 | +assert.strictEqual(syncChannel.traceSync(() => 'omitted'), 'omitted'); |
| 43 | +assert.strictEqual(syncChannel.traceSync(() => 'undefined', undefined), |
| 44 | + 'undefined'); |
| 45 | +assert.strictEqual(syncChannel.traceSync(() => 'provided', syncProvided), |
| 46 | + 'provided'); |
| 47 | + |
| 48 | +assert.strictEqual(Object.getPrototypeOf(syncContexts[0]), null); |
| 49 | +assert.strictEqual(Object.getPrototypeOf(syncContexts[1]), null); |
| 50 | +assert.notStrictEqual(syncContexts[0], syncContexts[1]); |
| 51 | +assert.strictEqual(syncContexts[2], syncProvided); |
| 52 | +assert.deepStrictEqual(syncContexts.slice(0, 3).map(({ result }) => result), |
| 53 | + ['omitted', 'undefined', 'provided']); |
| 54 | + |
| 55 | +const syncError = new Error('sync'); |
| 56 | +assert.throws(() => syncChannel.traceSync(() => { |
| 57 | + throw syncError; |
| 58 | +}), (error) => error === syncError); |
| 59 | +assert.strictEqual(syncContexts[3].error, syncError); |
| 60 | + |
| 61 | +const promiseChannel = dc.tracingChannel('test:promise-context'); |
| 62 | +const promiseContexts = []; |
| 63 | +subscribe(promiseChannel, promiseContexts, true); |
| 64 | + |
| 65 | +const promiseProvided = { provided: true }; |
| 66 | +const promiseError = new Error('promise'); |
| 67 | +Promise.resolve() |
| 68 | + .then(() => promiseChannel.tracePromise(() => Promise.resolve('omitted'))) |
| 69 | + .then(() => promiseChannel.tracePromise( |
| 70 | + () => Promise.resolve('undefined'), undefined)) |
| 71 | + .then(() => promiseChannel.tracePromise( |
| 72 | + () => Promise.resolve('provided'), promiseProvided)) |
| 73 | + .then(() => assert.rejects( |
| 74 | + promiseChannel.tracePromise(() => Promise.reject(promiseError)), |
| 75 | + promiseError)) |
| 76 | + .then(common.mustCall(() => { |
| 77 | + assert.strictEqual(Object.getPrototypeOf(promiseContexts[0]), null); |
| 78 | + assert.strictEqual(Object.getPrototypeOf(promiseContexts[1]), null); |
| 79 | + assert.notStrictEqual(promiseContexts[0], promiseContexts[1]); |
| 80 | + assert.strictEqual(promiseContexts[2], promiseProvided); |
| 81 | + assert.deepStrictEqual( |
| 82 | + promiseContexts.slice(0, 3).map(({ result }) => result), |
| 83 | + ['omitted', 'undefined', 'provided']); |
| 84 | + assert.strictEqual(promiseContexts[3].error, promiseError); |
| 85 | + })); |
0 commit comments