From fdae9ad188e9a113e46663a43aaf6abc0f3bfc3c Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Sun, 8 Nov 2020 00:58:08 +0100 Subject: [PATCH] test: fix races in test-performance-eventlooputil Fix two races in test-performance-eventlooputil resulting in a flaky test. elu1 was capture after start time t from spin look. If OS descides to reschedule the process after capturing t but before getting elu for >=50ms the spin loop is actually a nop. elu1 doesn't show this and as a result elut3 = eventLoopUtilization(elu1) results in elu3.active === 0. Moving capturing of t after capturing t, just before the spin look avoids this. Similar if OS decides to shedule a different process between getting the total elu from start and the diff elu showing the spin loop the check to verify that total active time is long then the spin loop fails. Exchanging these statements avoids this race. PR-URL: https://github.com/nodejs/node/pull/36028 Fixes: https://github.com/nodejs/node/issues/35309 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott --- .../test-performance-eventlooputil.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-performance-eventlooputil.js b/test/parallel/test-performance-eventlooputil.js index 5d039209dadde7..6e62a666443289 100644 --- a/test/parallel/test-performance-eventlooputil.js +++ b/test/parallel/test-performance-eventlooputil.js @@ -23,28 +23,28 @@ if (nodeTiming.loopStart === -1) { } setTimeout(mustCall(function r() { - const t = Date.now(); const elu1 = eventLoopUtilization(); // Force idle time to accumulate before allowing test to continue. if (elu1.idle <= 0) return setTimeout(mustCall(r), 5); + const t = Date.now(); while (Date.now() - t < SPIN_DUR) { } - const elu2 = eventLoopUtilization(); - const elu3 = eventLoopUtilization(elu1); - const elu4 = eventLoopUtilization(elu2, elu1); + const elu2 = eventLoopUtilization(elu1); + const elu3 = eventLoopUtilization(); + const elu4 = eventLoopUtilization(elu3, elu1); - assert.strictEqual(elu3.idle, 0); + assert.strictEqual(elu2.idle, 0); assert.strictEqual(elu4.idle, 0); - assert.strictEqual(elu3.utilization, 1); + assert.strictEqual(elu2.utilization, 1); assert.strictEqual(elu4.utilization, 1); - assert.strictEqual(elu2.active - elu1.active, elu4.active); - assert.ok(elu3.active > SPIN_DUR - 10, `${elu3.active} <= ${SPIN_DUR - 10}`); + assert.strictEqual(elu3.active - elu1.active, elu4.active); + assert.ok(elu2.active > SPIN_DUR - 10, `${elu2.active} <= ${SPIN_DUR - 10}`); + assert.ok(elu2.active < elu4.active, `${elu2.active} >= ${elu4.active}`); + assert.ok(elu3.active > elu2.active, `${elu3.active} <= ${elu2.active}`); assert.ok(elu3.active > elu4.active, `${elu3.active} <= ${elu4.active}`); - assert.ok(elu2.active > elu3.active, `${elu2.active} <= ${elu3.active}`); - assert.ok(elu2.active > elu4.active, `${elu2.active} <= ${elu4.active}`); setTimeout(mustCall(runIdleTimeTest), TIMEOUT); }), 5);