11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst WINDOW = 200; // Why does this need to be so big?
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci{
301cb0ef41Sopenharmony_ci  const starttime = Date.now();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(function() {
331cb0ef41Sopenharmony_ci    const endtime = Date.now();
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    const diff = endtime - starttime;
361cb0ef41Sopenharmony_ci    assert.ok(diff > 0);
371cb0ef41Sopenharmony_ci    console.error(`diff: ${diff}`);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    assert.ok(1000 <= diff && diff < 1000 + WINDOW);
401cb0ef41Sopenharmony_ci  }), 1000);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// This timer shouldn't execute
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const id = setTimeout(common.mustNotCall(), 500);
461cb0ef41Sopenharmony_ci  clearTimeout(id);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const starttime = Date.now();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  let interval_count = 0;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  setInterval(common.mustCall(function() {
551cb0ef41Sopenharmony_ci    interval_count += 1;
561cb0ef41Sopenharmony_ci    const endtime = Date.now();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    const diff = endtime - starttime;
591cb0ef41Sopenharmony_ci    assert.ok(diff > 0);
601cb0ef41Sopenharmony_ci    console.error(`diff: ${diff}`);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    const t = interval_count * 1000;
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    assert.ok(t <= diff && diff < t + (WINDOW * interval_count));
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    assert.ok(interval_count <= 3, `interval_count: ${interval_count}`);
671cb0ef41Sopenharmony_ci    if (interval_count === 3)
681cb0ef41Sopenharmony_ci      clearInterval(this);
691cb0ef41Sopenharmony_ci  }, 3), 1000);
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci// Single param:
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  setTimeout(function(param) {
761cb0ef41Sopenharmony_ci    assert.strictEqual(param, 'test param');
771cb0ef41Sopenharmony_ci  }, 1000, 'test param');
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci{
811cb0ef41Sopenharmony_ci  let interval_count = 0;
821cb0ef41Sopenharmony_ci  setInterval(function(param) {
831cb0ef41Sopenharmony_ci    ++interval_count;
841cb0ef41Sopenharmony_ci    assert.strictEqual(param, 'test param');
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    if (interval_count === 3)
871cb0ef41Sopenharmony_ci      clearInterval(this);
881cb0ef41Sopenharmony_ci  }, 1000, 'test param');
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci// Multiple param
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  setTimeout(function(param1, param2) {
951cb0ef41Sopenharmony_ci    assert.strictEqual(param1, 'param1');
961cb0ef41Sopenharmony_ci    assert.strictEqual(param2, 'param2');
971cb0ef41Sopenharmony_ci  }, 1000, 'param1', 'param2');
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci{
1011cb0ef41Sopenharmony_ci  let interval_count = 0;
1021cb0ef41Sopenharmony_ci  setInterval(function(param1, param2) {
1031cb0ef41Sopenharmony_ci    ++interval_count;
1041cb0ef41Sopenharmony_ci    assert.strictEqual(param1, 'param1');
1051cb0ef41Sopenharmony_ci    assert.strictEqual(param2, 'param2');
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci    if (interval_count === 3)
1081cb0ef41Sopenharmony_ci      clearInterval(this);
1091cb0ef41Sopenharmony_ci  }, 1000, 'param1', 'param2');
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci// setInterval(cb, 0) should be called multiple times.
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci  let count = 0;
1151cb0ef41Sopenharmony_ci  const interval = setInterval(common.mustCall(function() {
1161cb0ef41Sopenharmony_ci    if (++count > 10) clearInterval(interval);
1171cb0ef41Sopenharmony_ci  }, 11), 0);
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci// We should be able to clearTimeout multiple times without breakage.
1211cb0ef41Sopenharmony_ci{
1221cb0ef41Sopenharmony_ci  const t = common.mustCall(3);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  setTimeout(t, 200);
1251cb0ef41Sopenharmony_ci  setTimeout(t, 200);
1261cb0ef41Sopenharmony_ci  const y = setTimeout(t, 200);
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  clearTimeout(y);
1291cb0ef41Sopenharmony_ci  setTimeout(t, 200);
1301cb0ef41Sopenharmony_ci  clearTimeout(y);
1311cb0ef41Sopenharmony_ci}
132