From 49da459ce624fe44fbe267fb02acdfce03d5d4c9 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 13 Sep 2020 09:54:56 -0700 Subject: [PATCH] test: improve pummel/test-timers.js * use Date.now() instead of new Date() because only the timestamp is ever used, so we don't need the full Date object * use separate start times recorded for the two different test cases * improve assertion messages PR-URL: https://github.com/nodejs/node/pull/35175 Reviewed-By: Daijiro Wachi Reviewed-By: Anna Henningsen Reviewed-By: Shingo Inoue --- test/pummel/test-timers.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/pummel/test-timers.js b/test/pummel/test-timers.js index cd6417abe4079f..d5d992f7b9a552 100644 --- a/test/pummel/test-timers.js +++ b/test/pummel/test-timers.js @@ -26,10 +26,11 @@ const assert = require('assert'); const WINDOW = 200; // Why does this need to be so big? -const starttime = new Date(); { + const starttime = Date.now(); + setTimeout(common.mustCall(function() { - const endtime = new Date(); + const endtime = Date.now(); const diff = endtime - starttime; assert.ok(diff > 0); @@ -46,11 +47,13 @@ const starttime = new Date(); } { + const starttime = Date.now(); + let interval_count = 0; setInterval(common.mustCall(function() { interval_count += 1; - const endtime = new Date(); + const endtime = Date.now(); const diff = endtime - starttime; assert.ok(diff > 0); @@ -58,9 +61,9 @@ const starttime = new Date(); const t = interval_count * 1000; - assert.strictEqual(t - WINDOW < diff && diff < t + WINDOW, true); + assert.ok(t - WINDOW < diff && diff < t + WINDOW, `t: ${t}`); - assert.strictEqual(interval_count <= 3, true); + assert.ok(interval_count <= 3, `interval_count: ${interval_count}`); if (interval_count === 3) clearInterval(this); }, 3), 1000);