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
71 changes: 71 additions & 0 deletions test/parallel/test-timers-setunreftimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Flags: --expose-internals

Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
'use strict';

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

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

// ERR_INVALID_CALLBACK
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
{
// It throws ERR_INVALID_CALLBACK when first argument is not a function.
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
common.expectsError(
() => setUnrefTimeout(),
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function'
}
);
}

{
let called = false;
const timer = setUnrefTimeout(common.mustCall(() => {
called = true;
}), 1);

const testTimer = setUnrefTimeout(common.mustCall(() => {
strictEqual(called, false, 'unref pooled timer returned before check');
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(testTimer);
}), 1);

strictEqual(timer.refresh(), timer);
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
}

// test case for passing the arguments
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
{
for (let i = 3; i <= 6; i++) {
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
let called = false;
let results = [];
let inputArgs = [];
// set the input argument params
Copy link
Contributor

Choose a reason for hiding this comment

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

Linter wants this to be capitalized (make -j4 lint)

Suggested change
// set the input argument params
// Set the input argument params

for (let j = 0; j <= i - 3 ; j++) {
inputArgs.push(j);
}

const timer = setUnrefTimeout(common.mustCall((args1, args2, args3, args4) => {
called = true;
results.push(args1);
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
results.push(args2);
results.push(args3);
results.push(args4);
}), 1, inputArgs[0], inputArgs[1], inputArgs[2], inputArgs[3]);
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved

const testTimer = setTimeout(common.mustCall(() => {
strictEqual(called, true,
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
'unref pooled timer should be returned before check');
const maxArgsNum = 4;
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
for (let k = 0; k < maxArgsNum; k++) {
Copy link
Member

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
let expect = (k + 3 <= i) ? inputArgs[k] : undefined;
strictEqual(expect, results[k],
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
`result ${k} should be ${inputArgs[k]}. actual ${expect}`);
Fishrock123 marked this conversation as resolved.
Show resolved Hide resolved
}
clearTimeout(testTimer);
}), 100);

strictEqual(timer.refresh(), timer);
}
}