forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for ensuring proper timer delays
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
1 parent
5e8cbd7
commit e0ef4ea
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |