@@ -9,6 +9,9 @@ const fs = require('fs');
9
9
const baseOpened = nsolid . metrics ( ) . fsHandlesOpenedCount ;
10
10
const baseClosed = nsolid . metrics ( ) . fsHandlesClosedCount ;
11
11
12
+ let oCntr = 0 ;
13
+ let cCntr = 0 ;
14
+
12
15
function getOpened ( ) {
13
16
return nsolid . metrics ( ) . fsHandlesOpenedCount - baseOpened ;
14
17
}
@@ -23,54 +26,52 @@ try {
23
26
// It's meant to throw and the exception is to be ignored.
24
27
}
25
28
26
- assert . strictEqual ( getOpened ( ) , 0 ) ;
27
- assert . strictEqual ( getClosed ( ) , 0 ) ;
29
+ assert . strictEqual ( getOpened ( ) , oCntr ) ;
30
+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
28
31
29
32
const fd = fs . openSync ( __filename ) ;
30
- assert . strictEqual ( getOpened ( ) , 1 ) ;
31
- assert . strictEqual ( getClosed ( ) , 0 ) ;
33
+ assert . strictEqual ( getOpened ( ) , ++ oCntr ) ;
34
+ assert . strictEqual ( getClosed ( ) , cCntr ) ;
32
35
33
36
fs . 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 ) ;
36
43
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 ) ;
41
47
42
- fs . close ( fd , common . mustCall ( ( err ) => {
48
+ fs . open ( __filename , common . mustCall ( ( err , fd ) => {
43
49
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
+ } ) ) ;
58
62
} ) ) ;
59
- } ) ) ;
63
+ } ) ;
60
64
61
65
async 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 ;
69
70
}
70
71
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 ) ;
76
77
}
0 commit comments