11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { sleep } = require('internal/util');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Make sure we test 0ms timers, since they would had always wanted to run on
91cb0ef41Sopenharmony_ci// the current tick, and greater than 0ms timers, for scenarios where the
101cb0ef41Sopenharmony_ci// outer timer takes longer to complete than the delay of the nested timer.
111cb0ef41Sopenharmony_ci// Since the process of recreating this is identical regardless of the timer
121cb0ef41Sopenharmony_ci// delay, these scenarios are in one test.
131cb0ef41Sopenharmony_ciconst scenarios = [0, 100];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciscenarios.forEach(function(delay) {
161cb0ef41Sopenharmony_ci  let nestedCalled = false;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  setTimeout(function A() {
191cb0ef41Sopenharmony_ci    // Create the nested timer with the same delay as the outer timer so that it
201cb0ef41Sopenharmony_ci    // gets added to the current list of timers being processed by
211cb0ef41Sopenharmony_ci    // listOnTimeout.
221cb0ef41Sopenharmony_ci    setTimeout(function B() {
231cb0ef41Sopenharmony_ci      nestedCalled = true;
241cb0ef41Sopenharmony_ci    }, delay);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    // Busy loop for the same timeout used for the nested timer to ensure that
271cb0ef41Sopenharmony_ci    // we are in fact expiring the nested timer.
281cb0ef41Sopenharmony_ci    sleep(delay);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    // The purpose of running this assert in nextTick is to make sure it runs
311cb0ef41Sopenharmony_ci    // after A but before the next iteration of the libuv event loop.
321cb0ef41Sopenharmony_ci    process.nextTick(function() {
331cb0ef41Sopenharmony_ci      assert.ok(!nestedCalled);
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    // Ensure that the nested callback is indeed called prior to process exit.
371cb0ef41Sopenharmony_ci    process.on('exit', function onExit() {
381cb0ef41Sopenharmony_ci      assert.ok(nestedCalled);
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci  }, delay);
411cb0ef41Sopenharmony_ci});
42