-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timers: emit warning if delay is negative or NaN
Emit process warning once per process when delay is a negative number or not a number, this will prevent unexpected behaviour caused by invalid `delay` also keep the consistency of the behaviour and warning message for `TIMEOUT_MAX` number As the negative number is invalid delay will be set to 1. PR-URL: #46678 Reviewed-By: Debadree Chatterjee <[email protected]> Reviewed-By: Ulises Gascón <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
- Loading branch information
1 parent
764b13d
commit f5ed338
Showing
9 changed files
with
272 additions
and
5 deletions.
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
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
39 changes: 39 additions & 0 deletions
39
test/parallel/test-timers-nan-duration-emit-once-per-process.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,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 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 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 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 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); | ||
} |