1// Flags: --expose-internals
2'use strict';
3
4require('../common');
5const assert = require('assert');
6const { sleep } = require('internal/util');
7
8// Make sure we test 0ms timers, since they would had always wanted to run on
9// the current tick, and greater than 0ms timers, for scenarios where the
10// outer timer takes longer to complete than the delay of the nested timer.
11// Since the process of recreating this is identical regardless of the timer
12// delay, these scenarios are in one test.
13const scenarios = [0, 100];
14
15scenarios.forEach(function(delay) {
16  let nestedCalled = false;
17
18  setTimeout(function A() {
19    // Create the nested timer with the same delay as the outer timer so that it
20    // gets added to the current list of timers being processed by
21    // listOnTimeout.
22    setTimeout(function B() {
23      nestedCalled = true;
24    }, delay);
25
26    // Busy loop for the same timeout used for the nested timer to ensure that
27    // we are in fact expiring the nested timer.
28    sleep(delay);
29
30    // The purpose of running this assert in nextTick is to make sure it runs
31    // after A but before the next iteration of the libuv event loop.
32    process.nextTick(function() {
33      assert.ok(!nestedCalled);
34    });
35
36    // Ensure that the nested callback is indeed called prior to process exit.
37    process.on('exit', function onExit() {
38      assert.ok(nestedCalled);
39    });
40  }, delay);
41});
42