• 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 inputs = [
27  undefined,
28  null,
29  true,
30  false,
31  '',
32  [],
33  {},
34  NaN,
35  +Infinity,
36  -Infinity,
37  (1.0 / 0.0),      // sanity check
38  parseFloat('x'),  // NaN
39  -10,
40  -1,
41  -0.5,
42  -0.1,
43  -0.0,
44  0,
45  0.0,
46  0.1,
47  0.5,
48  1,
49  1.0,
50  2147483648,     // Browser behavior: timeouts > 2^31-1 run on next tick
51  12345678901234,  // ditto
52];
53
54const timeouts = [];
55const intervals = [];
56
57inputs.forEach((value, index) => {
58  setTimeout(() => {
59    timeouts[index] = true;
60  }, value);
61
62  const handle = setInterval(() => {
63    clearInterval(handle); // Disarm timer or we'll never finish
64    intervals[index] = true;
65  }, value);
66});
67
68// All values in inputs array coerce to 1 ms. Therefore, they should all run
69// before a timer set here for 2 ms.
70
71setTimeout(common.mustCall(() => {
72  // Assert that all other timers have run
73  inputs.forEach((value, index) => {
74    assert(timeouts[index]);
75    assert(intervals[index]);
76  });
77}), 2);
78
79// Test 10 ms timeout separately.
80setTimeout(common.mustCall(), 10);
81setInterval(common.mustCall(function() { clearInterval(this); }), 10);
82