Skip to content

Commit a4d7560

Browse files
jasnelltargos
authored andcommitted
test: close FileHandle objects in tests explicitly
PR-URL: #58615 Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 893999e commit a4d7560

File tree

4 files changed

+82
-49
lines changed

4 files changed

+82
-49
lines changed

test/parallel/test-fs-open.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fs.open(__filename, 'r', 0, common.mustSucceed());
4949
fs.open(__filename, 'r', null, common.mustSucceed());
5050

5151
async function promise() {
52-
await fs.promises.open(__filename);
53-
await fs.promises.open(__filename, 'r');
52+
await (await fs.promises.open(__filename)).close();
53+
await (await fs.promises.open(__filename, 'r')).close();
5454
}
5555

5656
promise().then(common.mustCall()).catch(common.mustNotCall());

test/parallel/test-fs-promises-file-handle-read.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,26 @@ async function validateLargeRead(options) {
5454
// from the current position in the file.
5555
const filePath = fixtures.path('x.txt');
5656
const fileHandle = await open(filePath, 'r');
57-
const pos = 0xffffffff + 1; // max-uint32 + 1
58-
const readHandle =
59-
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
60-
61-
assert.strictEqual(readHandle.bytesRead, 0);
57+
try {
58+
const pos = 0xffffffff + 1; // max-uint32 + 1
59+
const readHandle =
60+
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
61+
62+
assert.strictEqual(readHandle.bytesRead, 0);
63+
} finally {
64+
await fileHandle.close();
65+
}
6266
}
6367

6468
async function validateReadNoParams() {
6569
const filePath = fixtures.path('x.txt');
6670
const fileHandle = await open(filePath, 'r');
6771
// Should not throw
68-
await fileHandle.read();
72+
try {
73+
await fileHandle.read();
74+
} finally {
75+
await fileHandle.close();
76+
}
6977
}
7078

7179
// Validates that the zero position is respected after the position has been
@@ -75,15 +83,19 @@ async function validateReadWithPositionZero() {
7583
const opts = { useConf: true };
7684
const filePath = fixtures.path('x.txt');
7785
const fileHandle = await open(filePath, 'r');
78-
const expectedSequence = ['x', 'y', 'z'];
79-
80-
for (let i = 0; i < expectedSequence.length * 2; i++) {
81-
const len = 1;
82-
const pos = i % 3;
83-
const buf = Buffer.alloc(len);
84-
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
85-
assert.strictEqual(bytesRead, len);
86-
assert.strictEqual(buf.toString(), expectedSequence[pos]);
86+
try {
87+
const expectedSequence = ['x', 'y', 'z'];
88+
89+
for (let i = 0; i < expectedSequence.length * 2; i++) {
90+
const len = 1;
91+
const pos = i % 3;
92+
const buf = Buffer.alloc(len);
93+
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
94+
assert.strictEqual(bytesRead, len);
95+
assert.strictEqual(buf.toString(), expectedSequence[pos]);
96+
}
97+
} finally {
98+
await fileHandle.close();
8799
}
88100
}
89101

@@ -92,24 +104,32 @@ async function validateReadLength(len) {
92104
const opts = { useConf: true };
93105
const filePath = fixtures.path('x.txt');
94106
const fileHandle = await open(filePath, 'r');
95-
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
96-
assert.strictEqual(bytesRead, len);
107+
try {
108+
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
109+
assert.strictEqual(bytesRead, len);
110+
} finally {
111+
await fileHandle.close();
112+
}
97113
}
98114

99115
async function validateReadWithNoOptions(byte) {
100116
const buf = Buffer.alloc(byte);
101117
const filePath = fixtures.path('x.txt');
102118
const fileHandle = await open(filePath, 'r');
103-
let response = await fileHandle.read(buf);
104-
assert.strictEqual(response.bytesRead, byte);
105-
response = await read(fileHandle, buf, 0, undefined, 0);
106-
assert.strictEqual(response.bytesRead, byte);
107-
response = await read(fileHandle, buf, 0, null, 0);
108-
assert.strictEqual(response.bytesRead, byte);
109-
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
110-
assert.strictEqual(response.bytesRead, byte);
111-
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
112-
assert.strictEqual(response.bytesRead, byte);
119+
try {
120+
let response = await fileHandle.read(buf);
121+
assert.strictEqual(response.bytesRead, byte);
122+
response = await read(fileHandle, buf, 0, undefined, 0);
123+
assert.strictEqual(response.bytesRead, byte);
124+
response = await read(fileHandle, buf, 0, null, 0);
125+
assert.strictEqual(response.bytesRead, byte);
126+
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
127+
assert.strictEqual(response.bytesRead, byte);
128+
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
129+
assert.strictEqual(response.bytesRead, byte);
130+
} finally {
131+
await fileHandle.close();
132+
}
113133
}
114134

115135
(async function() {

test/parallel/test-fs-promises-file-handle-readFile.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ async function validateReadFileProc() {
4747
return;
4848

4949
const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
50-
const hostname = await fileHandle.readFile();
51-
assert.ok(hostname.length > 0);
50+
try {
51+
const hostname = await fileHandle.readFile();
52+
assert.ok(hostname.length > 0);
53+
} finally {
54+
await fileHandle.close();
55+
}
5256
}
5357

5458
async function doReadAndCancel() {
@@ -72,15 +76,18 @@ async function doReadAndCancel() {
7276
{
7377
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
7478
const fileHandle = await open(filePathForHandle, 'w+');
75-
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
76-
fs.writeFileSync(filePathForHandle, buffer);
77-
const controller = new AbortController();
78-
const { signal } = controller;
79-
process.nextTick(() => controller.abort());
80-
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
81-
name: 'AbortError'
82-
}, 'tick-0');
83-
await fileHandle.close();
79+
try {
80+
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
81+
fs.writeFileSync(filePathForHandle, buffer);
82+
const controller = new AbortController();
83+
const { signal } = controller;
84+
process.nextTick(() => controller.abort());
85+
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
86+
name: 'AbortError'
87+
}, 'tick-0');
88+
} finally {
89+
await fileHandle.close();
90+
}
8491
}
8592

8693
// Signal aborted right before buffer read
@@ -90,15 +97,17 @@ async function doReadAndCancel() {
9097
fs.writeFileSync(newFile, buffer);
9198

9299
const fileHandle = await open(newFile, 'r');
93-
94-
const controller = new AbortController();
95-
const { signal } = controller;
96-
tick(1, () => controller.abort());
97-
await assert.rejects(fileHandle.readFile(common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
98-
name: 'AbortError'
99-
}, 'tick-1');
100-
101-
await fileHandle.close();
100+
try {
101+
const controller = new AbortController();
102+
const { signal } = controller;
103+
tick(1, () => controller.abort());
104+
await assert.rejects(fileHandle.readFile(
105+
common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
106+
name: 'AbortError'
107+
}, 'tick-1');
108+
} finally {
109+
await fileHandle.close();
110+
}
102111
}
103112

104113
// Validate file size is within range for reading

test/parallel/test-whatwg-readablebytestream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class Source {
8080
this.controller = controller;
8181
}
8282

83+
async cancel() {
84+
await this.file.close();
85+
}
86+
8387
async pull(controller) {
8488
const byobRequest = controller.byobRequest;
8589
assert.match(inspect(byobRequest), /ReadableStreamBYOBRequest/);

0 commit comments

Comments
 (0)