Skip to content

Commit

Permalink
fixup only emit once per process
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecastelli committed Mar 31, 2024
1 parent 35f3752 commit db30941
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 50 deletions.
14 changes: 10 additions & 4 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function initAsyncResource(resource, type) {
if (initHooksExist())
emitInit(asyncId, type, triggerAsyncId, resource);
}

let warnedNegativeNumber = false;
let warnedNotNumber = false;

class Timeout {
// Timer constructor function.
// The entire prototype is defined in lib/timers.js
Expand All @@ -168,14 +172,16 @@ class Timeout {
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
if (after > TIMEOUT_MAX) {
process.emitWarning(`${after} does not fit into` +
' a 32-bit signed integer.' +
'\nTimeout duration was set to 1.',
' a 32-bit signed integer.' +
'\nTimeout duration was set to 1.',
'TimeoutOverflowWarning');
} else if (after < 0) {
} else if (after < 0 && !warnedNegativeNumber) {
warnedNegativeNumber = true;
process.emitWarning(`${after} is a negative number.` +
'\nTimeout duration was set to 1.',
'TimeoutNegativeWarning');
} else if (NumberIsNaN(after)) {
} else if (NumberIsNaN(after) && !warnedNotNumber) {
warnedNotNumber = true;
process.emitWarning(`${after} is not a number.` +
'\nTimeout duration was set to 1.',
'TimeoutNaNWarning');
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-timers-nan-duration-emit-once-per-process.js
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);
}
80 changes: 57 additions & 23 deletions test/parallel/test-timers-nan-duration-warning.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,73 @@
'use strict';

const common = require('../common');
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');
}

process.on(
'warning',
common.mustCall((warning) => {
if (warning.name === 'DeprecationWarning') return;
const testCases = ['timeout', 'interval', 'refresh'];

const lines = warning.message.split('\n');
function runTest() {
const args = process.argv.slice(2);

assert.strictEqual(warning.name, 'TimeoutNaNWarning');
assert.strictEqual(lines[0], `${NOT_A_NUMBER} is not a number.`);
assert.strictEqual(lines.length, 2);
}, 3)
);
const testChoice = args[0];

{
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER);
clearTimeout(timeout);
}
if (!testChoice) {
const filePath = path.join(__filename);

{
const interval = setInterval(timerNotCanceled, NOT_A_NUMBER);
clearInterval(interval);
}
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.');
});
}

{
const timeout = setTimeout(timerNotCanceled, NOT_A_NUMBER);
timeout.refresh();
clearTimeout(timeout);
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);
}
);
}

runTest();
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);
}
80 changes: 57 additions & 23 deletions test/parallel/test-timers-negative-duration-warning.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,73 @@
'use strict';

const common = require('../common');
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');
}

process.on(
'warning',
common.mustCall((warning) => {
if (warning.name === 'DeprecationWarning') return;
const testCases = ['timeout', 'interval', 'refresh'];

const lines = warning.message.split('\n');
function runTest() {
const args = process.argv.slice(2);

assert.strictEqual(warning.name, 'TimeoutNegativeWarning');
assert.strictEqual(lines[0], `${NEGATIVE_NUMBER} is a negative number.`);
assert.strictEqual(lines.length, 2);
}, 3)
);
const testChoice = args[0];

{
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER);
clearTimeout(timeout);
}
if (!testChoice) {
const filePath = path.join(__filename);

{
const interval = setInterval(timerNotCanceled, NEGATIVE_NUMBER);
clearInterval(interval);
}
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.');
});
}

{
const timeout = setTimeout(timerNotCanceled, NEGATIVE_NUMBER);
timeout.refresh();
clearTimeout(timeout);
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);
}
);
}

runTest();

0 comments on commit db30941

Please sign in to comment.