|
| 1 | +// Regression test for https://github.com/nodejs/node/issues/42743 |
| 2 | +// Test that a timerified function which throws asynchronously (i.e. returns |
| 3 | +// a rejected promise / thenable) behaves consistently with one that throws |
| 4 | +// synchronously: no 'function' performance entry is produced, and no |
| 5 | +// histogram record is added. |
| 6 | + |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +const common = require('../common'); |
| 10 | +const assert = require('assert'); |
| 11 | +const { |
| 12 | + createHistogram, |
| 13 | + performance, |
| 14 | + PerformanceObserver, |
| 15 | + timerify, |
| 16 | +} = require('perf_hooks'); |
| 17 | + |
| 18 | +// 1. Sync vs async throw should record the same number of histogram samples. |
| 19 | +{ |
| 20 | + const h1 = createHistogram(); |
| 21 | + const h2 = createHistogram(); |
| 22 | + |
| 23 | + function syncThrow() { throw new Error('sync'); } |
| 24 | + |
| 25 | + async function asyncThrow() { throw new Error('async'); } |
| 26 | + |
| 27 | + const g1 = timerify(syncThrow, { histogram: h1 }); |
| 28 | + const g2 = timerify(asyncThrow, { histogram: h2 }); |
| 29 | + |
| 30 | + assert.throws(() => g1(), /^Error: sync$/); |
| 31 | + |
| 32 | + assert.rejects(g2(), /^Error: async$/).then(common.mustCall(() => { |
| 33 | + // Both histograms must agree: neither should have recorded a sample, |
| 34 | + // because the throwing async function should behave like the throwing |
| 35 | + // sync function. |
| 36 | + assert.strictEqual(h1.count, 0); |
| 37 | + assert.strictEqual(h2.count, 0); |
| 38 | + assert.strictEqual(h1.count, h2.count); |
| 39 | + })); |
| 40 | +} |
| 41 | + |
| 42 | +// 2. No 'function' PerformanceEntry should be produced for either path. |
| 43 | +{ |
| 44 | + const obs = new PerformanceObserver(common.mustNotCall( |
| 45 | + 'no function entry should be enqueued for a throwing timerified fn', |
| 46 | + )); |
| 47 | + obs.observe({ entryTypes: ['function'] }); |
| 48 | + |
| 49 | + const sync = timerify(() => { throw new Error('sync2'); }); |
| 50 | + const async_ = timerify(async () => { throw new Error('async2'); }); |
| 51 | + |
| 52 | + assert.throws(() => sync(), /^Error: sync2$/); |
| 53 | + assert.rejects(async_(), /^Error: async2$/).then(common.mustCall(() => { |
| 54 | + // Give the observer a tick to flush any (incorrectly) enqueued entries |
| 55 | + // before disconnecting. |
| 56 | + setImmediate(common.mustCall(() => obs.disconnect())); |
| 57 | + })); |
| 58 | +} |
| 59 | + |
| 60 | +// 3. A custom thenable that only implements `then` (no `finally`) and |
| 61 | +// fulfills should still produce a performance entry and resolve to the |
| 62 | +// original value. This guards against a regression where the wrapper |
| 63 | +// relied on `result.finally`. |
| 64 | +{ |
| 65 | + const obs = new PerformanceObserver(common.mustCall((list, observer) => { |
| 66 | + const entries = list.getEntries(); |
| 67 | + assert.strictEqual(entries.length, 1); |
| 68 | + const entry = entries[0]; |
| 69 | + assert.strictEqual(entry.entryType, 'function'); |
| 70 | + assert.strictEqual(typeof entry.duration, 'number'); |
| 71 | + assert.strictEqual(typeof entry.startTime, 'number'); |
| 72 | + observer.disconnect(); |
| 73 | + })); |
| 74 | + obs.observe({ entryTypes: ['function'] }); |
| 75 | + |
| 76 | + function thenableOnly() { |
| 77 | + return { |
| 78 | + then(onFulfilled) { |
| 79 | + // Resolve asynchronously to mimic real promise semantics. |
| 80 | + setImmediate(() => onFulfilled('value')); |
| 81 | + }, |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + const wrapped = timerify(thenableOnly); |
| 86 | + const ret = wrapped(); |
| 87 | + // The wrapper must return something thenable; `.then` must work and |
| 88 | + // the resolved value must be propagated unchanged. |
| 89 | + assert.strictEqual(typeof ret.then, 'function'); |
| 90 | + ret.then(common.mustCall((value) => { |
| 91 | + assert.strictEqual(value, 'value'); |
| 92 | + })); |
| 93 | +} |
| 94 | + |
| 95 | +// 4. Async fulfillment must still propagate the resolved value. |
| 96 | +{ |
| 97 | + const wrapped = timerify(async () => 42); |
| 98 | + wrapped().then(common.mustCall((value) => { |
| 99 | + assert.strictEqual(value, 42); |
| 100 | + })); |
| 101 | +} |
| 102 | + |
| 103 | +// Reference `performance` so the binding is exercised in this file too. |
| 104 | +assert.strictEqual(performance.timerify, timerify); |
0 commit comments