1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var common = require('../common');
23 var assert = require('assert');
25 var WINDOW = 200; // why is does this need to be so big?
27 var interval_count = 0;
28 var setTimeout_called = false;
30 // check that these don't blow up.
34 assert.equal(true, setTimeout instanceof Function);
35 var starttime = new Date;
36 setTimeout(function() {
37 var endtime = new Date;
39 var diff = endtime - starttime;
41 console.error('diff: ' + diff);
43 assert.equal(true, 1000 - WINDOW < diff && diff < 1000 + WINDOW);
44 setTimeout_called = true;
47 // this timer shouldn't execute
48 var id = setTimeout(function() { assert.equal(true, false); }, 500);
51 setInterval(function() {
53 var endtime = new Date;
55 var diff = endtime - starttime;
57 console.error('diff: ' + diff);
59 var t = interval_count * 1000;
61 assert.equal(true, t - WINDOW < diff && diff < t + WINDOW);
63 assert.equal(true, interval_count <= 3);
64 if (interval_count == 3)
70 setTimeout(function(param) {
71 assert.equal('test param', param);
72 }, 1000, 'test param');
74 var interval_count2 = 0;
75 setInterval(function(param) {
77 assert.equal('test param', param);
79 if (interval_count2 == 3)
81 }, 1000, 'test param');
85 setTimeout(function(param1, param2) {
86 assert.equal('param1', param1);
87 assert.equal('param2', param2);
88 }, 1000, 'param1', 'param2');
90 var interval_count3 = 0;
91 setInterval(function(param1, param2) {
93 assert.equal('param1', param1);
94 assert.equal('param2', param2);
96 if (interval_count3 == 3)
98 }, 1000, 'param1', 'param2');
100 // setInterval(cb, 0) should be called multiple times.
102 var interval4 = setInterval(function() {
103 if (++count4 > 10) clearInterval(interval4);
107 // we should be able to clearTimeout multiple times without breakage.
108 var expectedTimeouts = 3;
114 var w = setTimeout(t, 200);
115 var x = setTimeout(t, 200);
116 var y = setTimeout(t, 200);
119 var z = setTimeout(t, 200);
123 process.on('exit', function() {
124 assert.equal(true, setTimeout_called);
125 assert.equal(3, interval_count);
126 assert.equal(11, count4);
127 assert.equal(0, expectedTimeouts, 'clearTimeout cleared too many timeouts');