Skip to content
Closed
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions test/parallel/test-timers-setunreftimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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'
}
);
}

// Test that setUnrefTimeout correctly passes arguments to the callback
{
const maxArgsNum = 4;
for (let i = 0; i < maxArgsNum; i++) {
const results = [];
const inputArgs = [];
// set the input argument params
Copy link
Copy Markdown
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; j++) {
inputArgs.push(j);
}

const timer = setUnrefTimeout(common.mustCall((...args) => {
// check the number of arguments passed to this callback.
Copy link
Copy Markdown
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
// check the number of arguments passed to this callback.
// Check the number of arguments passed to this callback.

strictEqual(args.length, i + 1,
`arguments.length should be ${i + 1}.` +
`actual ${args.length}`
);
for (const arg of args) {
results.push(arg);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just check the results here instead of populating an array.

Copy link
Copy Markdown
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'll fix not populating an array.

}
}), 1, ...inputArgs);
Copy link
Copy Markdown
Contributor

@apapirovski apapirovski Oct 15, 2018

Choose a reason for hiding this comment

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

clearTimeout(testTimer); here (after line 39, inside the function body).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you! But if I fix this as you requested, it occures an error.
I fixed as below
ce868dd


const testTimer = setTimeout(common.mustCall(() => {
for (let k = 0; k < maxArgsNum; k++) {
Copy link
Copy Markdown
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
Copy Markdown
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(results[k], expected,
`result ${k} should be ${expected}.` +
`actual ${inputArgs[k]}`);
}
clearTimeout(testTimer);
clearTimeout(timer);
}), 100);
}
}