2015-05-19 19:00:06 +08:00
|
|
|
'use strict';
|
2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2009-09-23 22:39:43 +08:00
|
|
|
var WINDOW = 200; // why is does this need to be so big?
|
2009-06-09 01:10:23 +08:00
|
|
|
|
|
|
|
var interval_count = 0;
|
|
|
|
var setTimeout_called = false;
|
|
|
|
|
2010-10-29 15:00:43 +08:00
|
|
|
// check that these don't blow up.
|
|
|
|
clearTimeout(null);
|
|
|
|
clearInterval(null);
|
|
|
|
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, setTimeout instanceof Function);
|
2015-05-19 19:00:06 +08:00
|
|
|
var starttime = new Date();
|
2010-12-03 09:03:18 +08:00
|
|
|
setTimeout(function() {
|
2015-05-19 19:00:06 +08:00
|
|
|
var endtime = new Date();
|
2009-09-23 22:39:43 +08:00
|
|
|
|
2009-08-27 00:22:00 +08:00
|
|
|
var diff = endtime - starttime;
|
2010-02-12 07:51:46 +08:00
|
|
|
assert.ok(diff > 0);
|
2011-02-19 03:20:42 +08:00
|
|
|
console.error('diff: ' + diff);
|
2009-09-23 22:39:43 +08:00
|
|
|
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, 1000 - WINDOW < diff && diff < 1000 + WINDOW);
|
2009-08-27 00:22:00 +08:00
|
|
|
setTimeout_called = true;
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
// this timer shouldn't execute
|
2010-12-03 09:03:18 +08:00
|
|
|
var id = setTimeout(function() { assert.equal(true, false); }, 500);
|
2009-08-27 00:22:00 +08:00
|
|
|
clearTimeout(id);
|
|
|
|
|
2010-12-03 09:03:18 +08:00
|
|
|
setInterval(function() {
|
2009-08-27 00:22:00 +08:00
|
|
|
interval_count += 1;
|
2015-05-19 19:00:06 +08:00
|
|
|
var endtime = new Date( );
|
2009-09-23 22:39:43 +08:00
|
|
|
|
2009-08-27 00:22:00 +08:00
|
|
|
var diff = endtime - starttime;
|
2010-02-12 07:51:46 +08:00
|
|
|
assert.ok(diff > 0);
|
2011-02-19 03:20:42 +08:00
|
|
|
console.error('diff: ' + diff);
|
2009-08-27 00:22:00 +08:00
|
|
|
|
|
|
|
var t = interval_count * 1000;
|
|
|
|
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, t - WINDOW < diff && diff < t + WINDOW);
|
2009-08-27 04:03:19 +08:00
|
|
|
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, interval_count <= 3);
|
2009-08-27 00:22:00 +08:00
|
|
|
if (interval_count == 3)
|
|
|
|
clearInterval(this);
|
|
|
|
}, 1000);
|
2009-06-09 01:10:23 +08:00
|
|
|
|
2010-01-19 01:51:50 +08:00
|
|
|
|
|
|
|
// Single param:
|
2010-12-06 06:33:52 +08:00
|
|
|
setTimeout(function(param) {
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal('test param', param);
|
|
|
|
}, 1000, 'test param');
|
2010-01-19 01:51:50 +08:00
|
|
|
|
|
|
|
var interval_count2 = 0;
|
2010-12-06 06:33:52 +08:00
|
|
|
setInterval(function(param) {
|
2010-01-19 01:51:50 +08:00
|
|
|
++interval_count2;
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal('test param', param);
|
2010-02-12 07:51:46 +08:00
|
|
|
|
2010-12-06 06:33:52 +08:00
|
|
|
if (interval_count2 == 3)
|
2010-01-19 01:51:50 +08:00
|
|
|
clearInterval(this);
|
2010-12-03 09:03:18 +08:00
|
|
|
}, 1000, 'test param');
|
2010-01-19 01:51:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Multiple param
|
2010-12-06 06:33:52 +08:00
|
|
|
setTimeout(function(param1, param2) {
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal('param1', param1);
|
|
|
|
assert.equal('param2', param2);
|
|
|
|
}, 1000, 'param1', 'param2');
|
2010-01-19 01:51:50 +08:00
|
|
|
|
|
|
|
var interval_count3 = 0;
|
2010-12-06 06:33:52 +08:00
|
|
|
setInterval(function(param1, param2) {
|
2010-01-19 01:51:50 +08:00
|
|
|
++interval_count3;
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal('param1', param1);
|
|
|
|
assert.equal('param2', param2);
|
2010-02-12 07:51:46 +08:00
|
|
|
|
2010-12-06 06:33:52 +08:00
|
|
|
if (interval_count3 == 3)
|
2010-01-19 01:51:50 +08:00
|
|
|
clearInterval(this);
|
2010-12-03 09:03:18 +08:00
|
|
|
}, 1000, 'param1', 'param2');
|
2010-01-19 01:51:50 +08:00
|
|
|
|
2010-09-01 07:45:17 +08:00
|
|
|
// setInterval(cb, 0) should be called multiple times.
|
2010-12-05 08:11:57 +08:00
|
|
|
var count4 = 0;
|
|
|
|
var interval4 = setInterval(function() {
|
2010-09-01 07:45:17 +08:00
|
|
|
if (++count4 > 10) clearInterval(interval4);
|
|
|
|
}, 0);
|
|
|
|
|
2010-11-30 02:48:25 +08:00
|
|
|
|
|
|
|
// we should be able to clearTimeout multiple times without breakage.
|
|
|
|
var expectedTimeouts = 3;
|
|
|
|
|
2010-12-06 06:33:52 +08:00
|
|
|
function t() {
|
2010-11-30 02:48:25 +08:00
|
|
|
expectedTimeouts--;
|
|
|
|
}
|
|
|
|
|
2010-12-05 08:11:57 +08:00
|
|
|
var w = setTimeout(t, 200);
|
|
|
|
var x = setTimeout(t, 200);
|
|
|
|
var y = setTimeout(t, 200);
|
2010-11-30 02:48:25 +08:00
|
|
|
|
2010-12-05 08:11:57 +08:00
|
|
|
clearTimeout(y);
|
|
|
|
var z = setTimeout(t, 200);
|
2010-11-30 02:48:25 +08:00
|
|
|
clearTimeout(y);
|
|
|
|
|
|
|
|
|
2011-10-15 07:08:36 +08:00
|
|
|
process.on('exit', function() {
|
2009-11-29 01:26:59 +08:00
|
|
|
assert.equal(true, setTimeout_called);
|
|
|
|
assert.equal(3, interval_count);
|
2010-09-01 07:45:17 +08:00
|
|
|
assert.equal(11, count4);
|
2010-12-03 09:03:18 +08:00
|
|
|
assert.equal(0, expectedTimeouts, 'clearTimeout cleared too many timeouts');
|
2009-08-27 00:51:04 +08:00
|
|
|
});
|