11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// The goal of this test is to make sure that, after the regression introduced
51cb0ef41Sopenharmony_ci// by 934bfe23a16556d05bfb1844ef4d53e8c9887c3d, the fix preserves the following
61cb0ef41Sopenharmony_ci// behavior of unref timers: if two timers are scheduled to fire at the same
71cb0ef41Sopenharmony_ci// time, if one unenrolls the other one in its _onTimeout callback, the other
81cb0ef41Sopenharmony_ci// one will *not* fire.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// This behavior is a private implementation detail and should not be
111cb0ef41Sopenharmony_ci// considered public interface.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cirequire('../common');
141cb0ef41Sopenharmony_ciconst timers = require('timers');
151cb0ef41Sopenharmony_ciconst assert = require('assert');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cilet nbTimersFired = 0;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst foo = {
201cb0ef41Sopenharmony_ci  _onTimeout: function() {
211cb0ef41Sopenharmony_ci    ++nbTimersFired;
221cb0ef41Sopenharmony_ci    timers.unenroll(bar);
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst bar = {
271cb0ef41Sopenharmony_ci  _onTimeout: function() {
281cb0ef41Sopenharmony_ci    ++nbTimersFired;
291cb0ef41Sopenharmony_ci    timers.unenroll(foo);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci};
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_citimers.enroll(bar, 1);
341cb0ef41Sopenharmony_citimers._unrefActive(bar);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citimers.enroll(foo, 1);
371cb0ef41Sopenharmony_citimers._unrefActive(foo);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cisetTimeout(function() {
401cb0ef41Sopenharmony_ci  assert.notStrictEqual(nbTimersFired, 2);
411cb0ef41Sopenharmony_ci}, 20);
42