Skip to content
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

test: add test cases for setUnrefTimeout. #20945

Closed
wants to merge 12 commits into from
48 changes: 48 additions & 0 deletions test/parallel/test-timers-setunreftimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');

const { strictEqual } = require('assert');
const { setUnrefTimeout } = require('internal/timers');

{
common.expectsError(
() => setUnrefTimeout(),
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function. Received undefined'
}
);
}

// Test that setUnrefTimeout correctly passes arguments to the callback
{
const maxArgsNum = 4;
for (let i = 0; i < maxArgsNum; i++) {
const inputArgs = [];
// Set the input argument params
for (let j = 0; j <= i; j++) {
inputArgs.push(j);
}

const timer = setUnrefTimeout(common.mustCall((...args) => {
// Check the number of arguments passed to this callback.
strictEqual(args.length, i + 1,
`arguments.length should be ${i + 1}.` +
`actual ${args.length}`
);
for (let k = 0; k < maxArgsNum; k++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of populating results, just do the check in the first timeout. Then this timeout shouldn't have a body. Literally just const testTimer = setTimeout(common.mustCall(), 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
I've fixed it.

// Checking the arguments passed to setUnrefTimeout
const expected = (k <= i) ? inputArgs[k] : undefined;
strictEqual(args[k], expected,
`result ${k} should be ${expected}.` +
`actual ${args[k]}`);
}
clearTimeout(timer);
}), 1, ...inputArgs);

setTimeout(common.mustCall(), 1);
}
}