-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When close is called in timer(set by setInterval) callback, active and
listOnTimeout will be called again which should not be called anymore. When unenroll is called in timer(set by setInterval) callback, the timer still works. With this fix, the timer behaves the same as clearTimeout/clearInterval is called in callback.
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const T = require('timers'); | ||
|
||
// This test is to make sure the timer will not fire again | ||
// when it is disarmed in callback. | ||
// This script is called by | ||
// ../parallel/test-timers-disarmed-in-callbak-not-fire-anymore.js | ||
// with NODE_DEBUG=timer to make sure 'active' and 'listOnTimeout' | ||
// will not be called again after the timer is disarmed. | ||
// Before fix https://github.com/nodejs/node/pull/4303, | ||
// When disarm with clearInterval, it works fine. | ||
// When disarm with close, active and listOnTimeout will be called again. | ||
// When disarm with unenroll, timer still works. | ||
// After this fix, timer behaves the same as clearTimeout/clearInterval | ||
// when it is disared by close/unenroll in callback. | ||
|
||
var nbIntervalFired1 = 0; | ||
var nbIntervalFired2 = 0; | ||
var nbIntervalFired3 = 0; | ||
const timer1 = setInterval(() => { | ||
nbIntervalFired1++; | ||
clearInterval(timer1); | ||
}, 11); | ||
const timer2 = setInterval(() => { | ||
nbIntervalFired2++; | ||
timer2.close(); | ||
}, 12); | ||
const timer3 = setInterval(() => { | ||
nbIntervalFired3++; | ||
T.unenroll(timer3); | ||
}, 13); | ||
setTimeout(() => { | ||
assert.strictEqual(nbIntervalFired1, 1); | ||
assert.strictEqual(nbIntervalFired2, 1); | ||
assert.strictEqual(nbIntervalFired3, 1); | ||
}, 100); |
31 changes: 31 additions & 0 deletions
31
test/parallel/test-timers-disarmed-in-callback-not-fire-anymore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const exec = require('child_process').exec; | ||
|
||
const testPath = './test/fixtures/test-timer-disarm.js'; | ||
const testCommand = 'NODE_DEBUG=timer ' + process.execPath + ' ' + testPath; | ||
|
||
setTimeout(() => { | ||
exec(testCommand, (err, stdout, stderr) => { | ||
const o = stderr.split('\n'); | ||
let timer1Fired = 0; | ||
let timer2Fired = 0; | ||
let timer3Fired = 0; | ||
for (var i = 0; i < o.length; i++) { | ||
var line = o[i]; | ||
if (line.indexOf('timeout callback 11') >= 0) { | ||
timer1Fired++; | ||
} | ||
if (line.indexOf('timeout callback 12') >= 0) { | ||
timer2Fired++; | ||
} | ||
if (line.indexOf('timeout callback 13') >= 0) { | ||
timer3Fired++; | ||
} | ||
} | ||
assert.strictEqual(timer1Fired, 1); | ||
assert.strictEqual(timer2Fired, 1); | ||
assert.strictEqual(timer3Fired, 1); | ||
}); | ||
}, 100); |