2c45bf05ef75e0f4f7ae3e4c2dbebd78469bfcec
[platform/upstream/nodejs.git] / test / pummel / test-timers.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 var common = require('../common');
23 var assert = require('assert');
24
25 var WINDOW = 200; // why is does this need to be so big?
26
27 var interval_count = 0;
28 var setTimeout_called = false;
29
30 // check that these don't blow up.
31 clearTimeout(null);
32 clearInterval(null);
33
34 assert.equal(true, setTimeout instanceof Function);
35 var starttime = new Date;
36 setTimeout(function() {
37   var endtime = new Date;
38
39   var diff = endtime - starttime;
40   assert.ok(diff > 0);
41   console.error('diff: ' + diff);
42
43   assert.equal(true, 1000 - WINDOW < diff && diff < 1000 + WINDOW);
44   setTimeout_called = true;
45 }, 1000);
46
47 // this timer shouldn't execute
48 var id = setTimeout(function() { assert.equal(true, false); }, 500);
49 clearTimeout(id);
50
51 setInterval(function() {
52   interval_count += 1;
53   var endtime = new Date;
54
55   var diff = endtime - starttime;
56   assert.ok(diff > 0);
57   console.error('diff: ' + diff);
58
59   var t = interval_count * 1000;
60
61   assert.equal(true, t - WINDOW < diff && diff < t + WINDOW);
62
63   assert.equal(true, interval_count <= 3);
64   if (interval_count == 3)
65     clearInterval(this);
66 }, 1000);
67
68
69 // Single param:
70 setTimeout(function(param) {
71   assert.equal('test param', param);
72 }, 1000, 'test param');
73
74 var interval_count2 = 0;
75 setInterval(function(param) {
76   ++interval_count2;
77   assert.equal('test param', param);
78
79   if (interval_count2 == 3)
80     clearInterval(this);
81 }, 1000, 'test param');
82
83
84 // Multiple param
85 setTimeout(function(param1, param2) {
86   assert.equal('param1', param1);
87   assert.equal('param2', param2);
88 }, 1000, 'param1', 'param2');
89
90 var interval_count3 = 0;
91 setInterval(function(param1, param2) {
92   ++interval_count3;
93   assert.equal('param1', param1);
94   assert.equal('param2', param2);
95
96   if (interval_count3 == 3)
97     clearInterval(this);
98 }, 1000, 'param1', 'param2');
99
100 // setInterval(cb, 0) should be called multiple times.
101 var count4 = 0;
102 var interval4 = setInterval(function() {
103   if (++count4 > 10) clearInterval(interval4);
104 }, 0);
105
106
107 // we should be able to clearTimeout multiple times without breakage.
108 var expectedTimeouts = 3;
109
110 function t() {
111   expectedTimeouts--;
112 }
113
114 var w = setTimeout(t, 200);
115 var x = setTimeout(t, 200);
116 var y = setTimeout(t, 200);
117
118 clearTimeout(y);
119 var z = setTimeout(t, 200);
120 clearTimeout(y);
121
122
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');
128 });