-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
timers: emit process warning if delay is negative or NaN #46678
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
Merged
nodejs-github-bot
merged 3 commits into
nodejs:main
from
jakecastelli:improve-timer-warning-message
Jun 28, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
39 changes: 39 additions & 0 deletions
39
test/parallel/test-timers-nan-duration-emit-once-per-process.js
This file contains hidden or 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,39 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const NOT_A_NUMBER = NaN; | ||
|
||
function timerNotCanceled() { | ||
assert.fail('Timer should be canceled'); | ||
} | ||
|
||
process.on( | ||
'warning', | ||
common.mustCall((warning) => { | ||
if (warning.name === 'DeprecationWarning') return; | ||
|
||
const lines = warning.message.split('\n'); | ||
|
||
assert.strictEqual(warning.name, 'TimeoutNaNWarning'); | ||
assert.strictEqual(lines[0], `${NOT_A_NUMBER} is not a number.`); | ||
assert.strictEqual(lines.length, 2); | ||
}, 1) | ||
); | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER); | ||
clearTimeout(timeout); | ||
} | ||
|
||
{ | ||
const interval = setInterval(timerNotCanceled, NOT_A_NUMBER); | ||
clearInterval(interval); | ||
} | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER); | ||
timeout.refresh(); | ||
clearTimeout(timeout); | ||
} |
This file contains hidden or 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,67 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
|
||
const NOT_A_NUMBER = NaN; | ||
|
||
function timerNotCanceled() { | ||
assert.fail('Timer should be canceled'); | ||
} | ||
|
||
const testCases = ['timeout', 'interval', 'refresh']; | ||
|
||
function runTests() { | ||
const args = process.argv.slice(2); | ||
|
||
const testChoice = args[0]; | ||
|
||
if (!testChoice) { | ||
const filePath = path.join(__filename); | ||
|
||
testCases.forEach((testCase) => { | ||
const { stdout } = child_process.spawnSync( | ||
process.execPath, | ||
[filePath, testCase], | ||
{ encoding: 'utf8' } | ||
); | ||
|
||
const lines = stdout.split('\n'); | ||
|
||
if (lines[0] === 'DeprecationWarning') return; | ||
|
||
assert.strictEqual(lines[0], 'TimeoutNaNWarning'); | ||
assert.strictEqual(lines[1], `${NOT_A_NUMBER} is not a number.`); | ||
assert.strictEqual(lines[2], 'Timeout duration was set to 1.'); | ||
}); | ||
} | ||
|
||
if (args[0] === testCases[0]) { | ||
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER); | ||
clearTimeout(timeout); | ||
} | ||
|
||
if (args[0] === testCases[1]) { | ||
const interval = setInterval(timerNotCanceled, NOT_A_NUMBER); | ||
clearInterval(interval); | ||
} | ||
|
||
if (args[0] === testCases[2]) { | ||
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER); | ||
timeout.refresh(); | ||
clearTimeout(timeout); | ||
} | ||
|
||
process.on( | ||
'warning', | ||
|
||
(warning) => { | ||
console.log(warning.name); | ||
console.log(warning.message); | ||
} | ||
); | ||
} | ||
|
||
runTests(); |
39 changes: 39 additions & 0 deletions
39
test/parallel/test-timers-negative-duration-warning-emit-once-per-process.js
This file contains hidden or 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,39 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const NEGATIVE_NUMBER = -1; | ||
|
||
function timerNotCanceled() { | ||
assert.fail('Timer should be canceled'); | ||
} | ||
|
||
process.on( | ||
'warning', | ||
common.mustCall((warning) => { | ||
if (warning.name === 'DeprecationWarning') return; | ||
|
||
const lines = warning.message.split('\n'); | ||
|
||
assert.strictEqual(warning.name, 'TimeoutNegativeWarning'); | ||
assert.strictEqual(lines[0], `${NEGATIVE_NUMBER} is a negative number.`); | ||
assert.strictEqual(lines.length, 2); | ||
}, 1) | ||
); | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER); | ||
clearTimeout(timeout); | ||
} | ||
|
||
{ | ||
const interval = setInterval(timerNotCanceled, NEGATIVE_NUMBER); | ||
clearInterval(interval); | ||
} | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER); | ||
timeout.refresh(); | ||
clearTimeout(timeout); | ||
} |
This file contains hidden or 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,67 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
|
||
const NEGATIVE_NUMBER = -1; | ||
|
||
function timerNotCanceled() { | ||
assert.fail('Timer should be canceled'); | ||
} | ||
|
||
const testCases = ['timeout', 'interval', 'refresh']; | ||
|
||
function runTests() { | ||
const args = process.argv.slice(2); | ||
|
||
const testChoice = args[0]; | ||
|
||
if (!testChoice) { | ||
const filePath = path.join(__filename); | ||
|
||
testCases.forEach((testCase) => { | ||
const { stdout } = child_process.spawnSync( | ||
process.execPath, | ||
[filePath, testCase], | ||
{ encoding: 'utf8' } | ||
); | ||
|
||
const lines = stdout.split('\n'); | ||
|
||
if (lines[0] === 'DeprecationWarning') return; | ||
|
||
assert.strictEqual(lines[0], 'TimeoutNegativeWarning'); | ||
assert.strictEqual(lines[1], `${NEGATIVE_NUMBER} is a negative number.`); | ||
assert.strictEqual(lines[2], 'Timeout duration was set to 1.'); | ||
}); | ||
} | ||
|
||
if (args[0] === testCases[0]) { | ||
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER); | ||
clearTimeout(timeout); | ||
} | ||
|
||
if (args[0] === testCases[1]) { | ||
const interval = setInterval(timerNotCanceled, NEGATIVE_NUMBER); | ||
clearInterval(interval); | ||
} | ||
|
||
if (args[0] === testCases[2]) { | ||
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER); | ||
timeout.refresh(); | ||
clearTimeout(timeout); | ||
} | ||
|
||
process.on( | ||
'warning', | ||
|
||
(warning) => { | ||
console.log(warning.name); | ||
console.log(warning.message); | ||
} | ||
); | ||
} | ||
|
||
runTests(); |
This file contains hidden or 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'); | ||
|
||
function timerNotCanceled() { | ||
assert.fail('Timer should be canceled'); | ||
} | ||
|
||
process.on( | ||
'warning', | ||
common.mustNotCall(() => { | ||
assert.fail('Timer should be canceled'); | ||
}) | ||
); | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, 0); | ||
clearTimeout(timeout); | ||
} | ||
|
||
{ | ||
const interval = setInterval(timerNotCanceled, 0); | ||
clearInterval(interval); | ||
} | ||
|
||
{ | ||
const timeout = setTimeout(timerNotCanceled, 0); | ||
timeout.refresh(); | ||
clearTimeout(timeout); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.