test: improve multiple timers tests · nodejs/node@fc49cf4
11'use strict';
223-require('../common');
3+const common = require('../common');
44const assert = require('assert');
556-let interval_fired = false;
7-let timeout_fired = false;
86let unref_interval = false;
97let unref_timer = false;
10-let unref_callbacks = 0;
118let checks = 0;
1291310const LONG_TIME = 10 * 1000;
1411const SHORT_TIME = 100;
151216-assert.doesNotThrow(function() {
13+assert.doesNotThrow(() => {
1714setTimeout(() => {}, 10).unref().ref().unref();
1815}, 'ref and unref are chainable');
191620-assert.doesNotThrow(function() {
17+assert.doesNotThrow(() => {
2118setInterval(() => {}, 10).unref().ref().unref();
2219}, 'ref and unref are chainable');
232024-setInterval(function() {
25-interval_fired = true;
26-}, LONG_TIME).unref();
21+setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
22+setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref();
272328-setTimeout(function() {
29-timeout_fired = true;
30-}, LONG_TIME).unref();
31-32-const interval = setInterval(function() {
24+const interval = setInterval(common.mustCall(() => {
3325unref_interval = true;
3426clearInterval(interval);
35-}, SHORT_TIME);
27+}), SHORT_TIME);
3628interval.unref();
372938-setTimeout(function() {
30+setTimeout(common.mustCall(() => {
3931unref_timer = true;
40-}, SHORT_TIME).unref();
32+}), SHORT_TIME).unref();
413342-const check_unref = setInterval(function() {
34+const check_unref = setInterval(() => {
4335if (checks > 5 || (unref_interval && unref_timer))
4436clearInterval(check_unref);
4537checks += 1;
4638}, 100);
473948-setTimeout(function() {
49-unref_callbacks++;
50-this.unref();
51-}, SHORT_TIME);
40+{
41+const timeout =
42+setTimeout(common.mustCall(() => {
43+timeout.unref();
44+}), SHORT_TIME);
45+}
524653-// Should not timeout the test
54-setInterval(function() {
55-this.unref();
56-}, SHORT_TIME);
47+{
48+// Should not timeout the test
49+const timeout =
50+setInterval(() => timeout.unref(), SHORT_TIME);
51+}
57525853// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
5954{
6055const t = setInterval(() => {}, 1);
6156process.nextTick(t.unref.bind({}));
6257process.nextTick(t.unref.bind(t));
6358}
64-65-process.on('exit', function() {
66-assert.strictEqual(interval_fired, false,
67-'Interval should not fire');
68-assert.strictEqual(timeout_fired, false,
69-'Timeout should not fire');
70-assert.strictEqual(unref_timer, true,
71-'An unrefd timeout should still fire');
72-assert.strictEqual(unref_interval, true,
73-'An unrefd interval should still fire');
74-assert.strictEqual(unref_callbacks, 1,
75-'Callback should only run once');
76-});