Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: #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 committed Sep 15, 2020
commit ae257ca0008cd6d01d477d927fa9962d8f406c5f
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