Skip to content

Commit 64c291a

Browse files
committed
test: refactor eventloopdelay test for clarity
Extract assertions into separate function and simplify control flow. Use setImmediate for final assertions to ensure histogram has recorded all samples.
1 parent a0b9aa0 commit 64c291a

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

test/sequential/test-performance-eventloopdelay.js

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,49 @@ const { sleep } = require('internal/util');
5959
return histogram.count > 0 && histogram.min > 0 && histogram.max > 0;
6060
}
6161

62+
// Final assertions - call this when ready to verify histogram values
63+
function runAssertions() {
64+
setImmediate(common.mustCall(() => {
65+
histogram.disable();
66+
// The values are non-deterministic, so we just check that a value is
67+
// present, as opposed to a specific value.
68+
assert(histogram.count > 0, `Expected samples to be recorded, got count=${histogram.count}`);
69+
assert(histogram.min > 0, `Expected min > 0, got ${histogram.min}`);
70+
assert(histogram.max > 0);
71+
assert(histogram.stddev > 0);
72+
assert(histogram.mean > 0);
73+
assert(histogram.percentiles.size > 0);
74+
for (let n = 1; n < 100; n = n + 0.1) {
75+
assert(histogram.percentile(n) >= 0);
76+
}
77+
histogram.reset();
78+
assert.strictEqual(histogram.min, 9223372036854776000);
79+
assert.strictEqual(histogram.max, 0);
80+
assert(Number.isNaN(histogram.stddev));
81+
assert(Number.isNaN(histogram.mean));
82+
assert.strictEqual(histogram.percentiles.size, 1);
83+
84+
['a', false, {}, []].forEach((i) => {
85+
assert.throws(
86+
() => histogram.percentile(i),
87+
{
88+
name: 'TypeError',
89+
code: 'ERR_INVALID_ARG_TYPE',
90+
}
91+
);
92+
});
93+
[-1, 0, 101, NaN].forEach((i) => {
94+
assert.throws(
95+
() => histogram.percentile(i),
96+
{
97+
name: 'RangeError',
98+
code: 'ERR_OUT_OF_RANGE',
99+
}
100+
);
101+
});
102+
}));
103+
}
104+
62105
// Spin the event loop with blocking work to generate measurable delays.
63106
// Some configurations (s390x, sharedlibs) need more iterations.
64107
let spinsRemaining = 5;
@@ -72,55 +115,15 @@ const { sleep } = require('internal/util');
72115
} else {
73116
// Give the histogram a chance to record final samples before checking.
74117
setImmediate(() => {
75-
// If we don't have valid samples yet, retry with more spinning.
76-
// This handles slower configurations like sharedlibs builds.
77-
if (!hasValidSamples() && retries < maxRetries) {
118+
// If we have valid samples or exhausted retries, run assertions.
119+
// Otherwise retry with more spinning for slower configurations.
120+
if (hasValidSamples() || retries >= maxRetries) {
121+
runAssertions();
122+
} else {
78123
retries++;
79124
spinsRemaining = 5;
80125
setTimeout(spinAWhile, common.platformTimeout(500));
81-
return;
82126
}
83-
84-
// Wrap final assertions in mustCall to ensure they run
85-
common.mustCall(() => {
86-
histogram.disable();
87-
// The values are non-deterministic, so we just check that a value is
88-
// present, as opposed to a specific value.
89-
assert(histogram.count > 0, `Expected samples to be recorded, got count=${histogram.count}`);
90-
assert(histogram.min > 0, `Expected min > 0, got ${histogram.min}`);
91-
assert(histogram.max > 0);
92-
assert(histogram.stddev > 0);
93-
assert(histogram.mean > 0);
94-
assert(histogram.percentiles.size > 0);
95-
for (let n = 1; n < 100; n = n + 0.1) {
96-
assert(histogram.percentile(n) >= 0);
97-
}
98-
histogram.reset();
99-
assert.strictEqual(histogram.min, 9223372036854776000);
100-
assert.strictEqual(histogram.max, 0);
101-
assert(Number.isNaN(histogram.stddev));
102-
assert(Number.isNaN(histogram.mean));
103-
assert.strictEqual(histogram.percentiles.size, 1);
104-
105-
['a', false, {}, []].forEach((i) => {
106-
assert.throws(
107-
() => histogram.percentile(i),
108-
{
109-
name: 'TypeError',
110-
code: 'ERR_INVALID_ARG_TYPE',
111-
}
112-
);
113-
});
114-
[-1, 0, 101, NaN].forEach((i) => {
115-
assert.throws(
116-
() => histogram.percentile(i),
117-
{
118-
name: 'RangeError',
119-
code: 'ERR_OUT_OF_RANGE',
120-
}
121-
);
122-
});
123-
})();
124127
});
125128
}
126129
}

0 commit comments

Comments
 (0)