@@ -9,6 +9,9 @@ const fs = require('fs');
99const baseOpened = nsolid . metrics ( ) . fsHandlesOpenedCount ;
1010const baseClosed = nsolid . metrics ( ) . fsHandlesClosedCount ;
1111
12+ let oCntr = 0 ;
13+ let cCntr = 0 ;
14+
1215function getOpened ( ) {
1316 return nsolid . metrics ( ) . fsHandlesOpenedCount - baseOpened ;
1417}
@@ -23,54 +26,52 @@ try {
2326 // It's meant to throw and the exception is to be ignored.
2427}
2528
26- assert . strictEqual ( getOpened ( ) , 0 ) ;
27- assert . strictEqual ( getClosed ( ) , 0 ) ;
29+ assert . strictEqual ( getOpened ( ) , oCntr ) ;
30+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
2831
2932const fd = fs . openSync ( __filename ) ;
30- assert . strictEqual ( getOpened ( ) , 1 ) ;
31- assert . strictEqual ( getClosed ( ) , 0 ) ;
33+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
34+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
3235
3336fs . closeSync ( fd ) ;
34- assert . strictEqual ( getOpened ( ) , 1 ) ;
35- assert . strictEqual ( getClosed ( ) , 1 ) ;
37+ assert . strictEqual ( getOpened ( ) , oCntr ) ;
38+ assert . strictEqual ( getClosed ( ) , ++ cCntr ) ;
39+
40+ fs . readFileSync ( __filename ) ;
41+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
42+ assert . strictEqual ( getClosed ( ) , ++ cCntr ) ;
3643
37- fs . open ( __filename , common . mustCall ( ( err , fd ) => {
38- assert . ok ( ! err ) ;
39- assert . strictEqual ( getOpened ( ) , 2 ) ;
40- assert . strictEqual ( getClosed ( ) , 1 ) ;
44+ fs . readFile ( __filename , ( ) => {
45+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
46+ assert . strictEqual ( getClosed ( ) , ++ cCntr ) ;
4147
42- fs . close ( fd , common . mustCall ( ( err ) => {
48+ fs . open ( __filename , common . mustCall ( ( err , fd ) => {
4349 assert . ok ( ! err ) ;
44- assert . strictEqual ( getOpened ( ) , 2 ) ;
45- assert . strictEqual ( getClosed ( ) , 2 ) ;
46-
47- checkPromise ( ) . then ( common . mustCall ( ( ) => {
48- openFileHandle ( ) ;
49- setTimeout ( ( ) => {
50- // The FileHandle should be out-of-scope and no longer accessed now.
51- global . gc ( ) ;
52- setImmediate ( ( ) => {
53- assert . strictEqual ( getOpened ( ) , 4 ) ;
54- assert . strictEqual ( getClosed ( ) , 4 ) ;
55- } ) ;
56- } , 100 ) ;
57- } ) ) . catch ( common . mustNotCall ( ) ) ;
50+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
51+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
52+
53+ fs . close ( fd , common . mustCall ( ( err ) => {
54+ assert . ok ( ! err ) ;
55+ assert . strictEqual ( getOpened ( ) , oCntr ) ;
56+ assert . strictEqual ( getClosed ( ) , ++ cCntr ) ;
57+
58+ checkPromise ( )
59+ . then ( common . mustCall ( closePromiseFd ) )
60+ . catch ( common . mustNotCall ( ) ) ;
61+ } ) ) ;
5862 } ) ) ;
59- } ) ) ;
63+ } ) ;
6064
6165async function checkPromise ( ) {
62- let fh = await fs . promises . open ( __filename ) ;
63- assert . strictEqual ( getOpened ( ) , 3 ) ;
64- assert . strictEqual ( getClosed ( ) , 2 ) ;
65- fh = await fh . close ( ) ;
66- assert . strictEqual ( fh , undefined ) ;
67- assert . strictEqual ( getOpened ( ) , 3 ) ;
68- assert . strictEqual ( getClosed ( ) , 3 ) ;
66+ const fh = await fs . promises . open ( __filename ) ;
67+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
68+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
69+ return fh ;
6970}
7071
71- async function openFileHandle ( ) {
72- const fh = await fs . promises . open ( __filename ) ;
73- console . log ( fh ) ;
74- assert . strictEqual ( getOpened ( ) , 4 ) ;
75- assert . strictEqual ( getClosed ( ) , 3 ) ;
72+ async function closePromiseFd ( fh ) {
73+ fh = await fh . close ( ) ;
74+ assert . strictEqual ( fh , undefined ) ;
75+ assert . strictEqual ( getOpened ( ) , oCntr ) ;
76+ assert . strictEqual ( getClosed ( ) , ++ cCntr ) ;
7677}
0 commit comments