0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/test/mjsunit/test-timers.js

47 lines
1.0 KiB
JavaScript
Raw Normal View History

process.mixin(require("./common"));
2009-09-23 16:39:43 +02:00
var WINDOW = 200; // why is does this need to be so big?
var interval_count = 0;
var setTimeout_called = false;
2009-08-26 18:22:00 +02:00
assertInstanceof(setTimeout, Function);
var starttime = new Date;
setTimeout(function () {
var endtime = new Date;
2009-09-23 16:39:43 +02:00
2009-08-26 18:22:00 +02:00
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
2009-09-23 16:39:43 +02:00
puts("diff: " + diff);
2009-08-26 18:22:00 +02:00
assertTrue(1000 - WINDOW < diff && diff < 1000 + WINDOW);
setTimeout_called = true;
}, 1000);
// this timer shouldn't execute
var id = setTimeout(function () { assertTrue(false); }, 500);
clearTimeout(id);
setInterval(function () {
interval_count += 1;
var endtime = new Date;
2009-09-23 16:39:43 +02:00
2009-08-26 18:22:00 +02:00
var diff = endtime - starttime;
if (diff < 0) diff = -diff;
2009-09-23 16:39:43 +02:00
puts("diff: " + diff);
2009-08-26 18:22:00 +02:00
var t = interval_count * 1000;
assertTrue(t - WINDOW < diff && diff < t + WINDOW);
2009-08-26 22:03:19 +02:00
2009-08-26 18:22:00 +02:00
assertTrue(interval_count <= 3);
if (interval_count == 3)
clearInterval(this);
}, 1000);
process.addListener("exit", function () {
assertTrue(setTimeout_called);
assertEquals(3, interval_count);
});