-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
timer: make sure timer is cleared #4303
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we can't just do this?:/test/parallel
const common = require('../common');
to the top.common.mustCall()
Get rid oftest-timers-disarmed-in-callbak-not-fire-anymore.js
Never mind!