Skip to content

Commit

Permalink
test: add tests for ensuring proper timer delays
Browse files Browse the repository at this point in the history
Includes a passing test that should keep passing and an issue test to
be fixed.

Refs: nodejs#5426
Refs: nodejs#7346
Refs: nodejs#7386
  • Loading branch information
Fishrock123 committed Jun 24, 2016
1 parent 5e8cbd7 commit e0ef4ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/known_issues/test-timers-sync-work-delay-problem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

// See https://github.com/nodejs/node/issues/5426

const common = require('../common');
const TimerWrap = process.binding('timer_wrap').Timer;

function sleep(duration) {
var start = Date.now();
while ((Date.now() - start) < duration) {
for (var i = 0; i < 1e5;) i++;
}
}

var last = TimerWrap.now();
var count = 0;

const interval = setInterval(common.mustCall(repeat, 4), 500);

function repeat() {
if (++count === 4) {
clearInterval(interval);
}
const now = TimerWrap.now();

if (now < last + 500 - 200 || now > last + 500 + 200) {
common.fail(`\`repeat\` delay: ${count} (now: ${now}, last: ${last})\n` +
'is not within the margin of error.');
}
last = now;
sleep(500);
}
18 changes: 18 additions & 0 deletions test/parallel/test-timers-later-scheduled-delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

// See https://github.com/nodejs/node/issues/5426#issuecomment-228294811

const common = require('../common');
const TimerWrap = process.binding('timer_wrap').Timer;

setTimeout(common.mustCall(function first() {}), 1000);

setTimeout(function scheduleLast() {
setTimeout(common.mustCall(function last() {
const now = TimerWrap.now();

if (now < 1500 - 200 || now > 1500 + 200) {
common.fail('Delay for `last` is not within the margin of error.');
}
}), 1000);
}, 500);

0 comments on commit e0ef4ea

Please sign in to comment.