From e0ef4eabc00892a14e25f79a0f9c5bbcd7b52598 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Fri, 24 Jun 2016 14:42:31 +0200 Subject: [PATCH] test: add tests for ensuring proper timer delays Includes a passing test that should keep passing and an issue test to be fixed. Refs: https://github.com/nodejs/node/issues/5426 Refs: https://github.com/nodejs/node/issues/7346 Refs: https://github.com/nodejs/node/issues/7386 --- .../test-timers-sync-work-delay-problem.js | 32 +++++++++++++++++++ .../test-timers-later-scheduled-delay.js | 18 +++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/known_issues/test-timers-sync-work-delay-problem.js create mode 100644 test/parallel/test-timers-later-scheduled-delay.js diff --git a/test/known_issues/test-timers-sync-work-delay-problem.js b/test/known_issues/test-timers-sync-work-delay-problem.js new file mode 100644 index 00000000000000..3181588e5d4b97 --- /dev/null +++ b/test/known_issues/test-timers-sync-work-delay-problem.js @@ -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); +} diff --git a/test/parallel/test-timers-later-scheduled-delay.js b/test/parallel/test-timers-later-scheduled-delay.js new file mode 100644 index 00000000000000..97bd3650996f0b --- /dev/null +++ b/test/parallel/test-timers-later-scheduled-delay.js @@ -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);