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