@@ -13,25 +13,23 @@ tmpdir.refresh();
1313
1414let testIndex = 0;
1515
16- // It's possible that the file stats are updated between the two statSync()
17- // calls so allow for a small difference.
18- const allowableDelta = 5;
19-
2016function getFilename() {
2117 const filename = path.join(tmpdir.path, `test-file-${++testIndex}`);
2218 fs.writeFileSync(filename, 'test');
2319 return filename;
2420}
2521
26- function verifyStats(bigintStats, numStats) {
22+ function verifyStats(bigintStats, numStats, allowableDelta) {
23+ // allowableDelta: It's possible that the file stats are updated between the
24+ // two stat() calls so allow for a small difference.
2725 for (const key of Object.keys(numStats)) {
2826 const val = numStats[key];
2927 if (isDate(val)) {
3028 const time = val.getTime();
3129 const time2 = bigintStats[key].getTime();
3230 assert(
33- Math.abs( time - time2) < allowableDelta,
34- `difference of ${key}.getTime() should < ${allowableDelta}.\n` +
31+ time - time2 <= allowableDelta,
32+ `difference of ${key}.getTime() should <= ${allowableDelta}.\n` +
3533 `Number version ${time}, BigInt version ${time2}n`);
3634 } else if (key === 'mode') {
3735 assert.strictEqual(bigintStats[key], BigInt(val));
@@ -71,15 +69,16 @@ function verifyStats(bigintStats, numStats) {
7169 const msFromNum = numStats[key];
7270
7371 assert(
74- Math.abs( msFromNum - Number(msFromBigInt)) < allowableDelta,
72+ msFromNum - Number(msFromBigInt) <= allowableDelta,
7573 `Number version ${key} = ${msFromNum}, ` +
76- `BigInt version ${key} = ${msFromBigInt}n`);
74+ `BigInt version ${key} = ${msFromBigInt}n, ` +
75+ `Allowable delta = ${allowableDelta}`);
7776
7877 assert(
79- Math.abs( msFromNum - Number(msFromBigIntNs)) < allowableDelta,
78+ msFromNum - Number(msFromBigIntNs) <= allowableDelta,
8079 `Number version ${key} = ${msFromNum}, ` +
8180 `BigInt version ${nsKey} = ${nsFromBigInt}n` +
82- ` = ${msFromBigIntNs}ms`);
81+ ` = ${msFromBigIntNs}ms, Allowable delta = ${allowableDelta} `);
8382 } else if (Number.isSafeInteger(val)) {
8483 assert.strictEqual(
8584 bigintStats[key], BigInt(val),
@@ -88,92 +87,102 @@ function verifyStats(bigintStats, numStats) {
8887 );
8988 } else {
9089 assert(
91- Math.abs( Number(bigintStats[key]) - val) < 1,
90+ Number(bigintStats[key]) - val < 1,
9291 `${key} is not a safe integer, difference should < 1.\n` +
9392 `Number version ${val}, BigInt version ${bigintStats[key]}n`);
9493 }
9594 }
9695}
9796
97+ const runSyncTest = (func, arg) => {
98+ const startTime = process.hrtime.bigint();
99+ const bigintStats = func(arg, { bigint: true });
100+ const numStats = func(arg);
101+ const endTime = process.hrtime.bigint();
102+ const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
103+ verifyStats(bigintStats, numStats, allowableDelta);
104+ };
105+
98106{
99107 const filename = getFilename();
100- const bigintStats = fs.statSync(filename, { bigint: true });
101- const numStats = fs.statSync(filename);
102- verifyStats(bigintStats, numStats);
108+ runSyncTest(fs.statSync, filename);
103109}
104110
105111if (!common.isWindows) {
106112 const filename = getFilename();
107113 const link = `${filename}-link`;
108114 fs.symlinkSync(filename, link);
109- const bigintStats = fs.lstatSync(link, { bigint: true });
110- const numStats = fs.lstatSync(link);
111- verifyStats(bigintStats, numStats);
115+ runSyncTest(fs.lstatSync, link);
112116}
113117
114118{
115119 const filename = getFilename();
116120 const fd = fs.openSync(filename, 'r');
117- const bigintStats = fs.fstatSync(fd, { bigint: true });
118- const numStats = fs.fstatSync(fd);
119- verifyStats(bigintStats, numStats);
121+ runSyncTest(fs.fstatSync, fd);
120122 fs.closeSync(fd);
121123}
122124
125+ const runCallbackTest = (func, arg, done) => {
126+ const startTime = process.hrtime.bigint();
127+ func(arg, { bigint: true }, common.mustCall((err, bigintStats) => {
128+ func(arg, common.mustCall((err, numStats) => {
129+ const endTime = process.hrtime.bigint();
130+ const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
131+ verifyStats(bigintStats, numStats, allowableDelta);
132+ if (done) {
133+ done();
134+ }
135+ }));
136+ }));
137+ };
138+
123139{
124140 const filename = getFilename();
125- fs.stat(filename, { bigint: true }, (err, bigintStats) => {
126- fs.stat(filename, (err, numStats) => {
127- verifyStats(bigintStats, numStats);
128- });
129- });
141+ runCallbackTest(fs.stat, filename);
130142}
131143
132144if (!common.isWindows) {
133145 const filename = getFilename();
134146 const link = `${filename}-link`;
135147 fs.symlinkSync(filename, link);
136- fs.lstat(link, { bigint: true }, (err, bigintStats) => {
137- fs.lstat(link, (err, numStats) => {
138- verifyStats(bigintStats, numStats);
139- });
140- });
148+ runCallbackTest(fs.lstat, link);
141149}
142150
143151{
144152 const filename = getFilename();
145153 const fd = fs.openSync(filename, 'r');
146- fs.fstat(fd, { bigint: true }, (err, bigintStats) => {
147- fs.fstat(fd, (err, numStats) => {
148- verifyStats(bigintStats, numStats);
149- fs.closeSync(fd);
150- });
151- });
154+ runCallbackTest(fs.fstat, fd, () => { fs.closeSync(fd); });
152155}
153156
154- (async function() {
157+ const runPromiseTest = async (func, arg) => {
158+ const startTime = process.hrtime.bigint();
159+ const bigintStats = await func(arg, { bigint: true });
160+ const numStats = await func(arg);
161+ const endTime = process.hrtime.bigint();
162+ const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
163+ verifyStats(bigintStats, numStats, allowableDelta);
164+ };
165+
166+ {
155167 const filename = getFilename();
156- const bigintStats = await promiseFs.stat(filename, { bigint: true });
157- const numStats = await promiseFs.stat(filename);
158- verifyStats(bigintStats, numStats);
159- })();
168+ runPromiseTest(promiseFs.stat, filename);
169+ }
160170
161171if (!common.isWindows) {
162- (async function() {
163- const filename = getFilename();
164- const link = `${filename}-link`;
165- fs.symlinkSync(filename, link);
166- const bigintStats = await promiseFs.lstat(link, { bigint: true });
167- const numStats = await promiseFs.lstat(link);
168- verifyStats(bigintStats, numStats);
169- })();
172+ const filename = getFilename();
173+ const link = `${filename}-link`;
174+ fs.symlinkSync(filename, link);
175+ runPromiseTest(promiseFs.lstat, link);
170176}
171177
172178(async function() {
173179 const filename = getFilename();
174180 const handle = await promiseFs.open(filename, 'r');
181+ const startTime = process.hrtime.bigint();
175182 const bigintStats = await handle.stat({ bigint: true });
176183 const numStats = await handle.stat();
177- verifyStats(bigintStats, numStats);
184+ const endTime = process.hrtime.bigint();
185+ const allowableDelta = Math.ceil(Number(endTime - startTime) / 1e6);
186+ verifyStats(bigintStats, numStats, allowableDelta);
178187 await handle.close();
179188})();
0 commit comments