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