summaryrefslogtreecommitdiff
path: root/test/parallel/test-timers-unref-remove-other-unref-timers-only-one-fires.js
blob: ce63ad8d2968e72a95169387ad8e6daa3c20936d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';

/*
 * The goal of this test is to make sure that, after the regression introduced
 * by 934bfe23a16556d05bfb1844ef4d53e8c9887c3d, the fix preserves the following
 * behavior of unref timers: if two timers are scheduled to fire at the same
 * time, if one unenrolls the other one in its _onTimeout callback, the other
 * one will *not* fire.
 *
 * This behavior is a private implementation detail and should not be
 * considered public interface.
 */
require('../common');
const timers = require('timers');
const assert = require('assert');

let nbTimersFired = 0;

const foo = {
  _onTimeout: function() {
    ++nbTimersFired;
    timers.unenroll(bar);
  }
};

const bar = {
  _onTimeout: function() {
    ++nbTimersFired;
    timers.unenroll(foo);
  }
};

timers.enroll(bar, 1);
timers._unrefActive(bar);

timers.enroll(foo, 1);
timers._unrefActive(foo);

setTimeout(function() {
  assert.notStrictEqual(nbTimersFired, 2);
}, 20);