11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This is a regression test for https://github.com/nodejs/node/issues/7722.
41cb0ef41Sopenharmony_ci//
51cb0ef41Sopenharmony_ci// When nested timers have the same timeout, calling clearTimeout on the
61cb0ef41Sopenharmony_ci// older timer after it has fired causes the list the newer timer is in
71cb0ef41Sopenharmony_ci// to be deleted. Since the newer timer was not cleared, it still blocks
81cb0ef41Sopenharmony_ci// the event loop completing for the duration of its timeout, however, since
91cb0ef41Sopenharmony_ci// no reference exists to it in its list, it cannot be canceled and its
101cb0ef41Sopenharmony_ci// callback is not called when the timeout elapses.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst common = require('../common');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst TIMEOUT = common.platformTimeout(100);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst handle1 = setTimeout(common.mustCall(function() {
171cb0ef41Sopenharmony_ci  // Cause the old TIMEOUT list to be deleted
181cb0ef41Sopenharmony_ci  clearTimeout(handle1);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // Cause a new list with the same key (TIMEOUT) to be created for this timer
211cb0ef41Sopenharmony_ci  const handle2 = setTimeout(common.mustNotCall(), TIMEOUT);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(function() {
241cb0ef41Sopenharmony_ci    // Attempt to cancel the second timer. Fix for this bug will keep the
251cb0ef41Sopenharmony_ci    // newer timer from being dereferenced by keeping its list from being
261cb0ef41Sopenharmony_ci    // erroneously deleted. If we are able to cancel the timer successfully,
271cb0ef41Sopenharmony_ci    // the bug is fixed.
281cb0ef41Sopenharmony_ci    clearTimeout(handle2);
291cb0ef41Sopenharmony_ci  }), 1);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // When this callback completes, `listOnTimeout` should now look at the
321cb0ef41Sopenharmony_ci  // correct list and refrain from removing the new TIMEOUT list which
331cb0ef41Sopenharmony_ci  // contains the reference to the newer timer.
341cb0ef41Sopenharmony_ci}), TIMEOUT);
35