@@ -24,8 +24,10 @@ const common = require('../common');
24
24
const assert = require ( 'assert' ) ;
25
25
const fs = require ( 'fs' ) ;
26
26
const join = require ( 'path' ) . join ;
27
-
27
+ const cp = require ( 'child_process' ) ;
28
+ const os = require ( 'os' ) ;
28
29
const tmpdir = require ( '../common/tmpdir' ) ;
30
+
29
31
tmpdir . refresh ( ) ;
30
32
31
33
const filename = join ( tmpdir . path , 'test.txt' ) ;
@@ -67,31 +69,60 @@ fs.open(filename4, 'w+', common.mustSucceed((fd) => {
67
69
} ) ) ;
68
70
} ) ) ;
69
71
72
+ function checkIfFileIsOpen ( fileName ) {
73
+ const platform = os . platform ( ) ;
74
+ if ( platform === 'linux' || platform === 'darwin' ) {
75
+ // Tried other ways like rm/stat, but that didn't work.
76
+ cp . exec ( `lsof -p ${ process . pid } ` , common . mustCall ( ( err , value ) => {
77
+ if ( err ) {
78
+ assert . ifError ( err ) ;
79
+ return ;
80
+ }
81
+ assert . ok ( ! value . toString ( ) . includes ( fileName ) ,
82
+ `${ fileName } is still open, but should be closed` ) ;
83
+ } ) ) ;
84
+ }
85
+ }
70
86
71
87
{
72
88
// Test that writeFile is cancellable with an AbortSignal.
73
- // Before the operation has started
89
+ // abort sync after the operation has started
74
90
const controller = new AbortController ( ) ;
75
91
const signal = controller . signal ;
76
92
const filename3 = join ( tmpdir . path , 'test3.txt' ) ;
77
93
78
94
fs . writeFile ( filename3 , s , { signal } , common . mustCall ( ( err ) => {
79
95
assert . strictEqual ( err . name , 'AbortError' ) ;
96
+ checkIfFileIsOpen ( filename3 ) ;
80
97
} ) ) ;
81
98
82
99
controller . abort ( ) ;
83
100
}
84
101
85
102
{
86
103
// Test that writeFile is cancellable with an AbortSignal.
87
- // After the operation has started
104
+ // abort async after the operation has started
88
105
const controller = new AbortController ( ) ;
89
106
const signal = controller . signal ;
90
- const filename4 = join ( tmpdir . path , 'test5.txt' ) ;
107
+ const filename5 = join ( tmpdir . path , 'test5.txt' ) ;
91
108
92
- fs . writeFile ( filename4 , s , { signal } , common . mustCall ( ( err ) => {
109
+ fs . writeFile ( filename5 , s , { signal } , common . mustCall ( ( err ) => {
93
110
assert . strictEqual ( err . name , 'AbortError' ) ;
111
+ checkIfFileIsOpen ( filename5 ) ;
94
112
} ) ) ;
95
113
96
114
process . nextTick ( ( ) => controller . abort ( ) ) ;
97
115
}
116
+
117
+ {
118
+ // Test that writeFile is cancellable with an AbortSignal.
119
+ // abort before the operation has started
120
+ const controller = new AbortController ( ) ;
121
+ const signal = controller . signal ;
122
+ const filename6 = join ( tmpdir . path , 'test6.txt' ) ;
123
+ controller . abort ( ) ;
124
+ fs . writeFile ( filename6 , s , { signal } , common . mustCall ( ( err ) => {
125
+ assert . strictEqual ( err . name , 'AbortError' ) ;
126
+ checkIfFileIsOpen ( filename6 ) ;
127
+ } ) ) ;
128
+ }
0 commit comments