@@ -13,25 +13,23 @@ tmpdir.refresh();
13
13
14
14
let testIndex = 0 ;
15
15
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
-
20
16
function getFilename ( ) {
21
17
const filename = path . join ( tmpdir . path , `test-file-${ ++ testIndex } ` ) ;
22
18
fs . writeFileSync ( filename , 'test' ) ;
23
19
return filename ;
24
20
}
25
21
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.
27
25
for ( const key of Object . keys ( numStats ) ) {
28
26
const val = numStats [ key ] ;
29
27
if ( isDate ( val ) ) {
30
28
const time = val . getTime ( ) ;
31
29
const time2 = bigintStats [ key ] . getTime ( ) ;
32
30
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` +
35
33
`Number version ${ time } , BigInt version ${ time2 } n` ) ;
36
34
} else if ( key === 'mode' ) {
37
35
assert . strictEqual ( bigintStats [ key ] , BigInt ( val ) ) ;
@@ -71,15 +69,16 @@ function verifyStats(bigintStats, numStats) {
71
69
const msFromNum = numStats [ key ] ;
72
70
73
71
assert (
74
- Math . abs ( msFromNum - Number ( msFromBigInt ) ) < allowableDelta ,
72
+ msFromNum - Number ( msFromBigInt ) <= allowableDelta ,
75
73
`Number version ${ key } = ${ msFromNum } , ` +
76
- `BigInt version ${ key } = ${ msFromBigInt } n` ) ;
74
+ `BigInt version ${ key } = ${ msFromBigInt } n, ` +
75
+ `Allowable delta = ${ allowableDelta } ` ) ;
77
76
78
77
assert (
79
- Math . abs ( msFromNum - Number ( msFromBigIntNs ) ) < allowableDelta ,
78
+ msFromNum - Number ( msFromBigIntNs ) <= allowableDelta ,
80
79
`Number version ${ key } = ${ msFromNum } , ` +
81
80
`BigInt version ${ nsKey } = ${ nsFromBigInt } n` +
82
- ` = ${ msFromBigIntNs } ms` ) ;
81
+ ` = ${ msFromBigIntNs } ms, Allowable delta = ${ allowableDelta } ` ) ;
83
82
} else if ( Number . isSafeInteger ( val ) ) {
84
83
assert . strictEqual (
85
84
bigintStats [ key ] , BigInt ( val ) ,
@@ -88,92 +87,102 @@ function verifyStats(bigintStats, numStats) {
88
87
) ;
89
88
} else {
90
89
assert (
91
- Math . abs ( Number ( bigintStats [ key ] ) - val ) < 1 ,
90
+ Number ( bigintStats [ key ] ) - val < 1 ,
92
91
`${ key } is not a safe integer, difference should < 1.\n` +
93
92
`Number version ${ val } , BigInt version ${ bigintStats [ key ] } n` ) ;
94
93
}
95
94
}
96
95
}
97
96
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
+
98
106
{
99
107
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 ) ;
103
109
}
104
110
105
111
if ( ! common . isWindows ) {
106
112
const filename = getFilename ( ) ;
107
113
const link = `${ filename } -link` ;
108
114
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 ) ;
112
116
}
113
117
114
118
{
115
119
const filename = getFilename ( ) ;
116
120
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 ) ;
120
122
fs . closeSync ( fd ) ;
121
123
}
122
124
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
+
123
139
{
124
140
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 ) ;
130
142
}
131
143
132
144
if ( ! common . isWindows ) {
133
145
const filename = getFilename ( ) ;
134
146
const link = `${ filename } -link` ;
135
147
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 ) ;
141
149
}
142
150
143
151
{
144
152
const filename = getFilename ( ) ;
145
153
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 ) ; } ) ;
152
155
}
153
156
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
+ {
155
167
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
+ }
160
170
161
171
if ( ! 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 ) ;
170
176
}
171
177
172
178
( async function ( ) {
173
179
const filename = getFilename ( ) ;
174
180
const handle = await promiseFs . open ( filename , 'r' ) ;
181
+ const startTime = process . hrtime . bigint ( ) ;
175
182
const bigintStats = await handle . stat ( { bigint : true } ) ;
176
183
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 ) ;
178
187
await handle . close ( ) ;
179
188
} ) ( ) ;
0 commit comments