Remove excessive copyright/license boilerplate
[platform/upstream/nodejs.git] / test / pummel / test-timers.js
1 var common = require('../common');
2 var assert = require('assert');
3
4 var WINDOW = 200; // why is does this need to be so big?
5
6 var interval_count = 0;
7 var setTimeout_called = false;
8
9 // check that these don't blow up.
10 clearTimeout(null);
11 clearInterval(null);
12
13 assert.equal(true, setTimeout instanceof Function);
14 var starttime = new Date;
15 setTimeout(function() {
16   var endtime = new Date;
17
18   var diff = endtime - starttime;
19   assert.ok(diff > 0);
20   console.error('diff: ' + diff);
21
22   assert.equal(true, 1000 - WINDOW < diff && diff < 1000 + WINDOW);
23   setTimeout_called = true;
24 }, 1000);
25
26 // this timer shouldn't execute
27 var id = setTimeout(function() { assert.equal(true, false); }, 500);
28 clearTimeout(id);
29
30 setInterval(function() {
31   interval_count += 1;
32   var endtime = new Date;
33
34   var diff = endtime - starttime;
35   assert.ok(diff > 0);
36   console.error('diff: ' + diff);
37
38   var t = interval_count * 1000;
39
40   assert.equal(true, t - WINDOW < diff && diff < t + WINDOW);
41
42   assert.equal(true, interval_count <= 3);
43   if (interval_count == 3)
44     clearInterval(this);
45 }, 1000);
46
47
48 // Single param:
49 setTimeout(function(param) {
50   assert.equal('test param', param);
51 }, 1000, 'test param');
52
53 var interval_count2 = 0;
54 setInterval(function(param) {
55   ++interval_count2;
56   assert.equal('test param', param);
57
58   if (interval_count2 == 3)
59     clearInterval(this);
60 }, 1000, 'test param');
61
62
63 // Multiple param
64 setTimeout(function(param1, param2) {
65   assert.equal('param1', param1);
66   assert.equal('param2', param2);
67 }, 1000, 'param1', 'param2');
68
69 var interval_count3 = 0;
70 setInterval(function(param1, param2) {
71   ++interval_count3;
72   assert.equal('param1', param1);
73   assert.equal('param2', param2);
74
75   if (interval_count3 == 3)
76     clearInterval(this);
77 }, 1000, 'param1', 'param2');
78
79 // setInterval(cb, 0) should be called multiple times.
80 var count4 = 0;
81 var interval4 = setInterval(function() {
82   if (++count4 > 10) clearInterval(interval4);
83 }, 0);
84
85
86 // we should be able to clearTimeout multiple times without breakage.
87 var expectedTimeouts = 3;
88
89 function t() {
90   expectedTimeouts--;
91 }
92
93 var w = setTimeout(t, 200);
94 var x = setTimeout(t, 200);
95 var y = setTimeout(t, 200);
96
97 clearTimeout(y);
98 var z = setTimeout(t, 200);
99 clearTimeout(y);
100
101
102 process.on('exit', function() {
103   assert.equal(true, setTimeout_called);
104   assert.equal(3, interval_count);
105   assert.equal(11, count4);
106   assert.equal(0, expectedTimeouts, 'clearTimeout cleared too many timeouts');
107 });