11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test is aimed at making sure that unref timers queued with
41cb0ef41Sopenharmony_ci// timers._unrefActive work correctly.
51cb0ef41Sopenharmony_ci//
61cb0ef41Sopenharmony_ci// Basically, it queues one timer in the unref queue, and then queues
71cb0ef41Sopenharmony_ci// it again each time its timeout callback is fired until the callback
81cb0ef41Sopenharmony_ci// has been called ten times.
91cb0ef41Sopenharmony_ci//
101cb0ef41Sopenharmony_ci// At that point, it unenrolls the unref timer so that its timeout callback
111cb0ef41Sopenharmony_ci// is not fired ever again.
121cb0ef41Sopenharmony_ci//
131cb0ef41Sopenharmony_ci// Finally, a ref timeout is used with a delay large enough to make sure that
141cb0ef41Sopenharmony_ci// all 10 timeouts had the time to expire.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cirequire('../common');
171cb0ef41Sopenharmony_ciconst timers = require('timers');
181cb0ef41Sopenharmony_ciconst assert = require('assert');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst someObject = {};
211cb0ef41Sopenharmony_cilet nbTimeouts = 0;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// libuv 0.10.x uses GetTickCount on Windows to implement timers, which uses
241cb0ef41Sopenharmony_ci// system's timers whose resolution is between 10 and 16ms. See
251cb0ef41Sopenharmony_ci// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408.aspx
261cb0ef41Sopenharmony_ci// for more information. That's the lowest resolution for timers across all
271cb0ef41Sopenharmony_ci// supported platforms. We're using it as the lowest common denominator,
281cb0ef41Sopenharmony_ci// and thus expect 5 timers to be able to fire in under 100 ms.
291cb0ef41Sopenharmony_ciconst N = 5;
301cb0ef41Sopenharmony_ciconst TEST_DURATION = 1000;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_citimers.unenroll(someObject);
331cb0ef41Sopenharmony_citimers.enroll(someObject, 1);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cisomeObject._onTimeout = function _onTimeout() {
361cb0ef41Sopenharmony_ci  ++nbTimeouts;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  if (nbTimeouts === N) timers.unenroll(someObject);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  timers._unrefActive(someObject);
411cb0ef41Sopenharmony_ci};
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_citimers._unrefActive(someObject);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cisetTimeout(function() {
461cb0ef41Sopenharmony_ci  assert.strictEqual(nbTimeouts, N);
471cb0ef41Sopenharmony_ci}, TEST_DURATION);
48