Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for ensuring proper timer delays #7404

Closed
Closed
Show file tree
Hide file tree
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
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);