1'use strict'; 2 3/* 4 * This test is aimed at making sure that unref timers queued with 5 * timers._unrefActive work correctly. 6 * 7 * Basically, it queues one timer in the unref queue, and then queues 8 * it again each time its timeout callback is fired until the callback 9 * has been called ten times. 10 * 11 * At that point, it unenrolls the unref timer so that its timeout callback 12 * is not fired ever again. 13 * 14 * Finally, a ref timeout is used with a delay large enough to make sure that 15 * all 10 timeouts had the time to expire. 16 */ 17 18require('../common'); 19const timers = require('timers'); 20const assert = require('assert'); 21 22const someObject = {}; 23let nbTimeouts = 0; 24 25/* 26 * libuv 0.10.x uses GetTickCount on Windows to implement timers, which uses 27 * system's timers whose resolution is between 10 and 16ms. See 28 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408.aspx 29 * for more information. That's the lowest resolution for timers across all 30 * supported platforms. We're using it as the lowest common denominator, 31 * and thus expect 5 timers to be able to fire in under 100 ms. 32 */ 33const N = 5; 34const TEST_DURATION = 1000; 35 36timers.unenroll(someObject); 37timers.enroll(someObject, 1); 38 39someObject._onTimeout = function _onTimeout() { 40 ++nbTimeouts; 41 42 if (nbTimeouts === N) timers.unenroll(someObject); 43 44 timers._unrefActive(someObject); 45}; 46 47timers._unrefActive(someObject); 48 49setTimeout(function() { 50 assert.strictEqual(nbTimeouts, N); 51}, TEST_DURATION); 52