• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'use strict';
23const common = require('../common');
24const assert = require('assert');
25
26const WINDOW = 200; // Why does this need to be so big?
27
28
29{
30  const starttime = Date.now();
31
32  setTimeout(common.mustCall(function() {
33    const endtime = Date.now();
34
35    const diff = endtime - starttime;
36    assert.ok(diff > 0);
37    console.error(`diff: ${diff}`);
38
39    assert.ok(1000 <= diff && diff < 1000 + WINDOW);
40  }), 1000);
41}
42
43// This timer shouldn't execute
44{
45  const id = setTimeout(common.mustNotCall(), 500);
46  clearTimeout(id);
47}
48
49{
50  const starttime = Date.now();
51
52  let interval_count = 0;
53
54  setInterval(common.mustCall(function() {
55    interval_count += 1;
56    const endtime = Date.now();
57
58    const diff = endtime - starttime;
59    assert.ok(diff > 0);
60    console.error(`diff: ${diff}`);
61
62    const t = interval_count * 1000;
63
64    assert.ok(t <= diff && diff < t + (WINDOW * interval_count));
65
66    assert.ok(interval_count <= 3, `interval_count: ${interval_count}`);
67    if (interval_count === 3)
68      clearInterval(this);
69  }, 3), 1000);
70}
71
72
73// Single param:
74{
75  setTimeout(function(param) {
76    assert.strictEqual(param, 'test param');
77  }, 1000, 'test param');
78}
79
80{
81  let interval_count = 0;
82  setInterval(function(param) {
83    ++interval_count;
84    assert.strictEqual(param, 'test param');
85
86    if (interval_count === 3)
87      clearInterval(this);
88  }, 1000, 'test param');
89}
90
91
92// Multiple param
93{
94  setTimeout(function(param1, param2) {
95    assert.strictEqual(param1, 'param1');
96    assert.strictEqual(param2, 'param2');
97  }, 1000, 'param1', 'param2');
98}
99
100{
101  let interval_count = 0;
102  setInterval(function(param1, param2) {
103    ++interval_count;
104    assert.strictEqual(param1, 'param1');
105    assert.strictEqual(param2, 'param2');
106
107    if (interval_count === 3)
108      clearInterval(this);
109  }, 1000, 'param1', 'param2');
110}
111
112// setInterval(cb, 0) should be called multiple times.
113{
114  let count = 0;
115  const interval = setInterval(common.mustCall(function() {
116    if (++count > 10) clearInterval(interval);
117  }, 11), 0);
118}
119
120// We should be able to clearTimeout multiple times without breakage.
121{
122  const t = common.mustCall(3);
123
124  setTimeout(t, 200);
125  setTimeout(t, 200);
126  const y = setTimeout(t, 200);
127
128  clearTimeout(y);
129  setTimeout(t, 200);
130  clearTimeout(y);
131}
132