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:
parent
3596ee006e
commit
2eeb65fa81
@ -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());
|
||||
|
@ -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() {
|
||||
|
@ -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
|
||||
|
@ -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/);
|
||||
|
Loading…
x
Reference in New Issue
Block a user