Skip to content

Commit 620412b

Browse files
committed
tests: update for timers unref optimization
1 parent 65640c2 commit 620412b

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

test/parallel/test-handle-wrap-isrefed.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ function makeAssert(message) {
9191
{
9292
const assert = makeAssert('hasRef() not working on timer_wrap');
9393
const timer = setTimeout(() => {}, 500);
94-
timer.unref();
95-
assert(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'), true);
96-
assert(timer._handle.hasRef(), false);
97-
timer.ref();
98-
assert(timer._handle.hasRef(), true);
99-
timer._handle.close(
100-
common.mustCall(() => assert(timer._handle.hasRef(), false)));
94+
setImmediate(() => {
95+
timer.unref();
96+
assert(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'), true);
97+
assert(timer._handle.hasRef(), false);
98+
timer.ref();
99+
assert(timer._handle.hasRef(), true);
100+
timer._handle.close(
101+
common.mustCall(() => assert(timer._handle.hasRef(), false)));
102+
});
101103
}

test/parallel/test-timers-active.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ var legitTimers = [
1212
legitTimers.forEach(function(legit) {
1313
const savedTimeout = legit._idleTimeout;
1414
active(legit);
15-
// active() should mutate these objects
16-
assert(legit._idleTimeout === savedTimeout);
17-
assert(Number.isInteger(legit._idleStart));
18-
assert(legit._idleNext);
19-
assert(legit._idlePrev);
15+
process.nextTick(() => {
16+
// active() should mutate these objects
17+
assert(legit._idleTimeout === savedTimeout);
18+
assert(Number.isInteger(legit._idleStart));
19+
assert(legit._idleNext);
20+
assert(legit._idlePrev);
21+
});
2022
});
2123

2224

@@ -29,6 +31,8 @@ var bogusTimers = [
2931
bogusTimers.forEach(function(bogus) {
3032
const savedTimeout = bogus._idleTimeout;
3133
active(bogus);
32-
// active() should not mutate these objects
33-
assert.deepStrictEqual(bogus, {_idleTimeout: savedTimeout});
34+
process.nextTick(() => {
35+
// active() should not mutate these objects
36+
assert.deepStrictEqual(bogus, {_idleTimeout: savedTimeout});
37+
});
3438
});

test/parallel/test-timers-same-timeout-wrong-list-deleted.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,27 @@ const handle1 = setTimeout(common.mustCall(function() {
5656
}));
5757
}), 10);
5858

59-
// Make sure our timers got added to the list.
60-
const activeHandles = process._getActiveHandles();
61-
const activeTimers = activeHandles.filter(function(handle) {
62-
return handle instanceof Timer;
63-
});
64-
const shortTimer = activeTimers.find(function(handle) {
65-
return handle._list.msecs === 10;
66-
});
67-
const longTimers = activeTimers.filter(function(handle) {
68-
return handle._list.msecs === TIMEOUT;
69-
});
59+
setImmediate(() => {
60+
// Make sure our timers got added to the list.
61+
const activeHandles = process._getActiveHandles();
62+
const activeTimers = activeHandles.filter(function(handle) {
63+
return handle instanceof Timer;
64+
});
65+
const shortTimer = activeTimers.find(function(handle) {
66+
return handle._list.msecs === 10;
67+
});
68+
const longTimers = activeTimers.filter(function(handle) {
69+
return handle._list.msecs === TIMEOUT;
70+
});
7071

71-
// Make sure our clearTimeout succeeded. One timer finished and
72-
// the other was canceled, so none should be active.
73-
assert.equal(activeTimers.length, 3, 'There are 3 timers in the list.');
74-
assert(shortTimer instanceof Timer, 'The shorter timer is in the list.');
75-
assert.equal(longTimers.length, 2, 'Both longer timers are in the list.');
72+
// Make sure our clearTimeout succeeded. One timer finished and
73+
// the other was canceled, so none should be active.
74+
assert.equal(activeTimers.length, 3, 'There are 3 timers in the list.');
75+
assert(shortTimer instanceof Timer, 'The shorter timer is in the list.');
76+
assert.equal(longTimers.length, 2, 'Both longer timers are in the list.');
7677

77-
// When this callback completes, `listOnTimeout` should now look at the
78-
// correct list and refrain from removing the new TIMEOUT list which
79-
// contains the reference to the newer timer.
78+
// When this callback completes, `listOnTimeout` should now look at the
79+
// correct list and refrain from removing the new TIMEOUT list which
80+
// contains the reference to the newer timer.
81+
});
8082
}), TIMEOUT);

test/parallel/test-timers-unref-leak.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ var closed = 0;
88
var timeout = setTimeout(function() {
99
called++;
1010
}, 10);
11-
timeout.unref();
1211

1312
// Wrap `close` method to check if the handle was closed
14-
var close = timeout._handle.close;
15-
timeout._handle.close = function() {
16-
closed++;
17-
return close.apply(this, arguments);
18-
};
13+
process.nextTick(() => {
14+
timeout.unref();
15+
var close = timeout._handle.close;
16+
timeout._handle.close = function() {
17+
closed++;
18+
return close.apply(this, arguments);
19+
};
20+
});
1921

2022
// Just to keep process alive and let previous timer's handle die
2123
setTimeout(function() {

0 commit comments

Comments
 (0)