test: close FileHandle objects in tests explicitly

PR-URL: https://github.com/nodejs/node/pull/58615
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
James M Snell 2025-06-09 07:36:23 -07:00 committed by GitHub
parent 3596ee006e
commit 2eeb65fa81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 47 deletions

View File

@ -49,8 +49,8 @@ fs.open(__filename, 'r', 0, common.mustSucceed());
fs.open(__filename, 'r', null, common.mustSucceed());
async function promise() {
await fs.promises.open(__filename);
await fs.promises.open(__filename, 'r');
await (await fs.promises.open(__filename)).close();
await (await fs.promises.open(__filename, 'r')).close();
}
promise().then(common.mustCall()).catch(common.mustNotCall());

View File

@ -54,18 +54,26 @@ async function validateLargeRead(options) {
// from the current position in the file.
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const pos = 0xffffffff + 1; // max-uint32 + 1
const readHandle =
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
try {
const pos = 0xffffffff + 1; // max-uint32 + 1
const readHandle =
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
assert.strictEqual(readHandle.bytesRead, 0);
assert.strictEqual(readHandle.bytesRead, 0);
} finally {
await fileHandle.close();
}
}
async function validateReadNoParams() {
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
// Should not throw
await fileHandle.read();
try {
await fileHandle.read();
} finally {
await fileHandle.close();
}
}
// Validates that the zero position is respected after the position has been
@ -75,15 +83,19 @@ async function validateReadWithPositionZero() {
const opts = { useConf: true };
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const expectedSequence = ['x', 'y', 'z'];
try {
const expectedSequence = ['x', 'y', 'z'];
for (let i = 0; i < expectedSequence.length * 2; i++) {
const len = 1;
const pos = i % 3;
const buf = Buffer.alloc(len);
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
assert.strictEqual(bytesRead, len);
assert.strictEqual(buf.toString(), expectedSequence[pos]);
for (let i = 0; i < expectedSequence.length * 2; i++) {
const len = 1;
const pos = i % 3;
const buf = Buffer.alloc(len);
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
assert.strictEqual(bytesRead, len);
assert.strictEqual(buf.toString(), expectedSequence[pos]);
}
} finally {
await fileHandle.close();
}
}
@ -92,24 +104,32 @@ async function validateReadLength(len) {
const opts = { useConf: true };
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
assert.strictEqual(bytesRead, len);
try {
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
assert.strictEqual(bytesRead, len);
} finally {
await fileHandle.close();
}
}
async function validateReadWithNoOptions(byte) {
const buf = Buffer.alloc(byte);
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
let response = await fileHandle.read(buf);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, undefined, 0);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, null, 0);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
assert.strictEqual(response.bytesRead, byte);
try {
let response = await fileHandle.read(buf);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, undefined, 0);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, null, 0);
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
assert.strictEqual(response.bytesRead, byte);
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
assert.strictEqual(response.bytesRead, byte);
} finally {
await fileHandle.close();
}
}
(async function() {

View File

@ -47,8 +47,12 @@ async function validateReadFileProc() {
return;
const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
const hostname = await fileHandle.readFile();
assert.ok(hostname.length > 0);
try {
const hostname = await fileHandle.readFile();
assert.ok(hostname.length > 0);
} finally {
await fileHandle.close();
}
}
async function doReadAndCancel() {
@ -72,15 +76,18 @@ async function doReadAndCancel() {
{
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
name: 'AbortError'
}, 'tick-0');
await fileHandle.close();
try {
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
name: 'AbortError'
}, 'tick-0');
} finally {
await fileHandle.close();
}
}
// Signal aborted right before buffer read
@ -90,15 +97,17 @@ async function doReadAndCancel() {
fs.writeFileSync(newFile, buffer);
const fileHandle = await open(newFile, 'r');
const controller = new AbortController();
const { signal } = controller;
tick(1, () => controller.abort());
await assert.rejects(fileHandle.readFile(common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
name: 'AbortError'
}, 'tick-1');
await fileHandle.close();
try {
const controller = new AbortController();
const { signal } = controller;
tick(1, () => controller.abort());
await assert.rejects(fileHandle.readFile(
common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
name: 'AbortError'
}, 'tick-1');
} finally {
await fileHandle.close();
}
}
// Validate file size is within range for reading

View File

@ -80,6 +80,10 @@ class Source {
this.controller = controller;
}
async cancel() {
await this.file.close();
}
async pull(controller) {
const byobRequest = controller.byobRequest;
assert.match(inspect(byobRequest), /ReadableStreamBYOBRequest/);