Skip to content

Commit

Permalink
test: improve pummel/test-timers.js
Browse files Browse the repository at this point in the history
* 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: #35175
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
  • Loading branch information
Trott authored and ruyadorno committed Sep 21, 2020
1 parent 066148d commit 49da459
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/pummel/test-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -46,21 +47,23 @@ 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);
console.error(`diff: ${diff}`);

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);
Expand Down

0 comments on commit 49da459

Please sign in to comment.