node/test/parallel/test-timers-nan-duration-emit-once-per-process.js
jakecastelli f5ed3386fd
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: https://github.com/nodejs/node/pull/46678
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-06-28 11:36:31 +00:00

40 lines
834 B
JavaScript

'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);
}