Skip to content

Commit 87e8e8b

Browse files
authored
test: fix flaky test-performance-eventloopdelay
The test was flaky because the histogram sampling might not have completed before the assertions ran. By using setImmediate before disabling the histogram and checking values, we give the event loop a chance to record final samples on slower systems. Also added an explicit check for histogram.count > 0 to provide a clearer error message if no samples are recorded. Refs: nodejs/reliability#1450 PR-URL: #61629 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 8c32389 commit 87e8e8b

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

test/sequential/test-performance-eventloopdelay.js

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,47 @@ const { sleep } = require('internal/util');
6464
if (--m > 0) {
6565
setTimeout(spinAWhile, common.platformTimeout(500));
6666
} else {
67-
histogram.disable();
68-
// The values are non-deterministic, so we just check that a value is
69-
// present, as opposed to a specific value.
70-
assert(histogram.min > 0);
71-
assert(histogram.max > 0);
72-
assert(histogram.stddev > 0);
73-
assert(histogram.mean > 0);
74-
assert(histogram.percentiles.size > 0);
75-
for (let n = 1; n < 100; n = n + 0.1) {
76-
assert(histogram.percentile(n) >= 0);
77-
}
78-
histogram.reset();
79-
assert.strictEqual(histogram.min, 9223372036854776000);
80-
assert.strictEqual(histogram.max, 0);
81-
assert(Number.isNaN(histogram.stddev));
82-
assert(Number.isNaN(histogram.mean));
83-
assert.strictEqual(histogram.percentiles.size, 1);
67+
// Give the histogram a chance to record final samples before disabling.
68+
// This helps on slower systems where sampling may be delayed.
69+
setImmediate(common.mustCall(() => {
70+
histogram.disable();
71+
// The values are non-deterministic, so we just check that a value is
72+
// present, as opposed to a specific value.
73+
assert(histogram.count > 0, `Expected samples to be recorded, got count=${histogram.count}`);
74+
assert(histogram.min > 0);
75+
assert(histogram.max > 0);
76+
assert(histogram.stddev > 0);
77+
assert(histogram.mean > 0);
78+
assert(histogram.percentiles.size > 0);
79+
for (let n = 1; n < 100; n = n + 0.1) {
80+
assert(histogram.percentile(n) >= 0);
81+
}
82+
histogram.reset();
83+
assert.strictEqual(histogram.min, 9223372036854776000);
84+
assert.strictEqual(histogram.max, 0);
85+
assert(Number.isNaN(histogram.stddev));
86+
assert(Number.isNaN(histogram.mean));
87+
assert.strictEqual(histogram.percentiles.size, 1);
8488

85-
['a', false, {}, []].forEach((i) => {
86-
assert.throws(
87-
() => histogram.percentile(i),
88-
{
89-
name: 'TypeError',
90-
code: 'ERR_INVALID_ARG_TYPE',
91-
}
92-
);
93-
});
94-
[-1, 0, 101, NaN].forEach((i) => {
95-
assert.throws(
96-
() => histogram.percentile(i),
97-
{
98-
name: 'RangeError',
99-
code: 'ERR_OUT_OF_RANGE',
100-
}
101-
);
102-
});
89+
['a', false, {}, []].forEach((i) => {
90+
assert.throws(
91+
() => histogram.percentile(i),
92+
{
93+
name: 'TypeError',
94+
code: 'ERR_INVALID_ARG_TYPE',
95+
}
96+
);
97+
});
98+
[-1, 0, 101, NaN].forEach((i) => {
99+
assert.throws(
100+
() => histogram.percentile(i),
101+
{
102+
name: 'RangeError',
103+
code: 'ERR_OUT_OF_RANGE',
104+
}
105+
);
106+
});
107+
}));
103108
}
104109
}
105110
spinAWhile();

0 commit comments

Comments
 (0)