From ebe95cbd69f06aaad36c736940806b4bec99e338 Mon Sep 17 00:00:00 2001 From: Michael Church Date: Thu, 19 Nov 2020 21:18:05 +0000 Subject: [PATCH] test: Fix a flaky test In the modified test file, sleep() is backed by setTimeout(), and some Node runtimes might execute setTimeout callback before the specified delay, meaning that tmi.getValue() will occasionally return `9`. This produces a flaky test. An extra ms leeway in the assertion works round the issue. https://github.com/nodejs/node/issues/10154 Test name updated to reflect that the timer's accuracy does not have single-digit precision --- test/metrics.spec.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/metrics.spec.ts b/test/metrics.spec.ts index 7c031b64ae..20593c0985 100644 --- a/test/metrics.spec.ts +++ b/test/metrics.spec.ts @@ -49,12 +49,15 @@ describe('TimerMetricInstance', () => { expect(tmi.getValue()).toBeUndefined; }); - it('can time things', async () => { + it('can time things with sufficient accuracy', async () => { const tmi = new TimerMetricInstance('timer/network_time'); tmi.start(); await sleep(10); tmi.stop(); - expect(tmi.getValue()).toBeGreaterThan(9); + // Sleep() is backed by setTimeout(), and some Node runtimes might execute setTimeout callback before the specified + // delay, meaning that tmi.getValue() will occasionally return `9` at this point. An extra ms leeway in this + // assertion works round the issue. https://github.com/nodejs/node/issues/10154 + expect(tmi.getValue()).toBeGreaterThanOrEqual(9); }); it('.start() / .stop() logs start/top and improper use warnings if you try to start/stop it again after stopping it', async () => {