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: improve test-fs-stat-bigint #31726

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
111 changes: 60 additions & 51 deletions test/parallel/test-fs-stat-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,23 @@ tmpdir.refresh();

let testIndex = 0;

// It's possible that the file stats are updated between the two statSync()
// calls so allow for a small difference.
const allowableDelta = 5;

function getFilename() {
const filename = path.join(tmpdir.path, `test-file-${++testIndex}`);
fs.writeFileSync(filename, 'test');
return filename;
}

function verifyStats(bigintStats, numStats) {
function verifyStats(bigintStats, numStats, allowableDelta) {
// allowableDelta: It's possible that the file stats are updated between the
// two stat() calls so allow for a small difference.
for (const key of Object.keys(numStats)) {
const val = numStats[key];
if (isDate(val)) {
const time = val.getTime();
const time2 = bigintStats[key].getTime();
assert(
Math.abs(time - time2) < allowableDelta,
`difference of ${key}.getTime() should < ${allowableDelta}.\n` +
time - time2 <= allowableDelta,
`difference of ${key}.getTime() should <= ${allowableDelta}.\n` +
`Number version ${time}, BigInt version ${time2}n`);
} else if (key === 'mode') {
assert.strictEqual(bigintStats[key], BigInt(val));
Expand Down Expand Up @@ -71,15 +69,16 @@ function verifyStats(bigintStats, numStats) {
const msFromNum = numStats[key];

assert(
Math.abs(msFromNum - Number(msFromBigInt)) < allowableDelta,
msFromNum - Number(msFromBigInt) <= allowableDelta,
`Number version ${key} = ${msFromNum}, ` +
`BigInt version ${key} = ${msFromBigInt}n`);
`BigInt version ${key} = ${msFromBigInt}n, ` +
`Allowable delta = ${allowableDelta}`);

assert(
Math.abs(msFromNum - Number(msFromBigIntNs)) < allowableDelta,
msFromNum - Number(msFromBigIntNs) <= allowableDelta,
`Number version ${key} = ${msFromNum}, ` +
`BigInt version ${nsKey} = ${nsFromBigInt}n` +
` = ${msFromBigIntNs}ms`);
` = ${msFromBigIntNs}ms, Allowable delta = ${allowableDelta}`);
} else if (Number.isSafeInteger(val)) {
assert.strictEqual(
bigintStats[key], BigInt(val),
Expand All @@ -88,92 +87,102 @@ function verifyStats(bigintStats, numStats) {
);
} else {
assert(
Math.abs(Number(bigintStats[key]) - val) < 1,
Number(bigintStats[key]) - val < 1,
`${key} is not a safe integer, difference should < 1.\n` +
`Number version ${val}, BigInt version ${bigintStats[key]}n`);
}
}
}

const runSyncTest = (func, arg) => {
const startTime = process.hrtime.bigint();
const bigintStats = func(arg, { bigint: true });
const numStats = func(arg);
const endTime = process.hrtime.bigint();
const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
verifyStats(bigintStats, numStats, allowableDelta);
};

{
const filename = getFilename();
const bigintStats = fs.statSync(filename, { bigint: true });
const numStats = fs.statSync(filename);
verifyStats(bigintStats, numStats);
runSyncTest(fs.statSync, filename);
}

if (!common.isWindows) {
const filename = getFilename();
const link = `${filename}-link`;
fs.symlinkSync(filename, link);
const bigintStats = fs.lstatSync(link, { bigint: true });
const numStats = fs.lstatSync(link);
verifyStats(bigintStats, numStats);
runSyncTest(fs.lstatSync, link);
}

{
const filename = getFilename();
const fd = fs.openSync(filename, 'r');
const bigintStats = fs.fstatSync(fd, { bigint: true });
const numStats = fs.fstatSync(fd);
verifyStats(bigintStats, numStats);
runSyncTest(fs.fstatSync, fd);
fs.closeSync(fd);
}

const runCallbackTest = (func, arg, done) => {
const startTime = process.hrtime.bigint();
func(arg, { bigint: true }, common.mustCall((err, bigintStats) => {
func(arg, common.mustCall((err, numStats) => {
const endTime = process.hrtime.bigint();
const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
verifyStats(bigintStats, numStats, allowableDelta);
if (done) {
done();
}
}));
}));
};

{
const filename = getFilename();
fs.stat(filename, { bigint: true }, (err, bigintStats) => {
fs.stat(filename, (err, numStats) => {
verifyStats(bigintStats, numStats);
});
});
runCallbackTest(fs.stat, filename);
}

if (!common.isWindows) {
const filename = getFilename();
const link = `${filename}-link`;
fs.symlinkSync(filename, link);
fs.lstat(link, { bigint: true }, (err, bigintStats) => {
fs.lstat(link, (err, numStats) => {
verifyStats(bigintStats, numStats);
});
});
runCallbackTest(fs.lstat, link);
}

{
const filename = getFilename();
const fd = fs.openSync(filename, 'r');
fs.fstat(fd, { bigint: true }, (err, bigintStats) => {
fs.fstat(fd, (err, numStats) => {
verifyStats(bigintStats, numStats);
fs.closeSync(fd);
});
});
runCallbackTest(fs.fstat, fd, () => { fs.closeSync(fd); });
}

(async function() {
const runPromiseTest = async (func, arg) => {
const startTime = process.hrtime.bigint();
const bigintStats = await func(arg, { bigint: true });
const numStats = await func(arg);
const endTime = process.hrtime.bigint();
const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
verifyStats(bigintStats, numStats, allowableDelta);
};

{
const filename = getFilename();
const bigintStats = await promiseFs.stat(filename, { bigint: true });
const numStats = await promiseFs.stat(filename);
verifyStats(bigintStats, numStats);
})();
runPromiseTest(promiseFs.stat, filename);
}

if (!common.isWindows) {
(async function() {
const filename = getFilename();
const link = `${filename}-link`;
fs.symlinkSync(filename, link);
const bigintStats = await promiseFs.lstat(link, { bigint: true });
const numStats = await promiseFs.lstat(link);
verifyStats(bigintStats, numStats);
})();
const filename = getFilename();
const link = `${filename}-link`;
fs.symlinkSync(filename, link);
runPromiseTest(promiseFs.lstat, link);
}

(async function() {
const filename = getFilename();
const handle = await promiseFs.open(filename, 'r');
const startTime = process.hrtime.bigint();
const bigintStats = await handle.stat({ bigint: true });
const numStats = await handle.stat();
verifyStats(bigintStats, numStats);
const endTime = process.hrtime.bigint();
const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
verifyStats(bigintStats, numStats, allowableDelta);
await handle.close();
})();