mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
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
4 changed files with 80 additions and 47 deletions
|
@ -49,8 +49,8 @@ fs.open(__filename, 'r', 0, common.mustSucceed());
|
||||||
fs.open(__filename, 'r', null, common.mustSucceed());
|
fs.open(__filename, 'r', null, common.mustSucceed());
|
||||||
|
|
||||||
async function promise() {
|
async function promise() {
|
||||||
await fs.promises.open(__filename);
|
await (await fs.promises.open(__filename)).close();
|
||||||
await fs.promises.open(__filename, 'r');
|
await (await fs.promises.open(__filename, 'r')).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
promise().then(common.mustCall()).catch(common.mustNotCall());
|
promise().then(common.mustCall()).catch(common.mustNotCall());
|
||||||
|
|
|
@ -54,18 +54,26 @@ async function validateLargeRead(options) {
|
||||||
// from the current position in the file.
|
// from the current position in the file.
|
||||||
const filePath = fixtures.path('x.txt');
|
const filePath = fixtures.path('x.txt');
|
||||||
const fileHandle = await open(filePath, 'r');
|
const fileHandle = await open(filePath, 'r');
|
||||||
const pos = 0xffffffff + 1; // max-uint32 + 1
|
try {
|
||||||
const readHandle =
|
const pos = 0xffffffff + 1; // max-uint32 + 1
|
||||||
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);
|
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() {
|
async function validateReadNoParams() {
|
||||||
const filePath = fixtures.path('x.txt');
|
const filePath = fixtures.path('x.txt');
|
||||||
const fileHandle = await open(filePath, 'r');
|
const fileHandle = await open(filePath, 'r');
|
||||||
// Should not throw
|
// 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
|
// Validates that the zero position is respected after the position has been
|
||||||
|
@ -75,15 +83,19 @@ async function validateReadWithPositionZero() {
|
||||||
const opts = { useConf: true };
|
const opts = { useConf: true };
|
||||||
const filePath = fixtures.path('x.txt');
|
const filePath = fixtures.path('x.txt');
|
||||||
const fileHandle = await open(filePath, 'r');
|
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++) {
|
for (let i = 0; i < expectedSequence.length * 2; i++) {
|
||||||
const len = 1;
|
const len = 1;
|
||||||
const pos = i % 3;
|
const pos = i % 3;
|
||||||
const buf = Buffer.alloc(len);
|
const buf = Buffer.alloc(len);
|
||||||
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
|
const { bytesRead } = await read(fileHandle, buf, 0, len, pos, opts);
|
||||||
assert.strictEqual(bytesRead, len);
|
assert.strictEqual(bytesRead, len);
|
||||||
assert.strictEqual(buf.toString(), expectedSequence[pos]);
|
assert.strictEqual(buf.toString(), expectedSequence[pos]);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await fileHandle.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,24 +104,32 @@ async function validateReadLength(len) {
|
||||||
const opts = { useConf: true };
|
const opts = { useConf: true };
|
||||||
const filePath = fixtures.path('x.txt');
|
const filePath = fixtures.path('x.txt');
|
||||||
const fileHandle = await open(filePath, 'r');
|
const fileHandle = await open(filePath, 'r');
|
||||||
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
|
try {
|
||||||
assert.strictEqual(bytesRead, len);
|
const { bytesRead } = await read(fileHandle, buf, 0, len, 0, opts);
|
||||||
|
assert.strictEqual(bytesRead, len);
|
||||||
|
} finally {
|
||||||
|
await fileHandle.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateReadWithNoOptions(byte) {
|
async function validateReadWithNoOptions(byte) {
|
||||||
const buf = Buffer.alloc(byte);
|
const buf = Buffer.alloc(byte);
|
||||||
const filePath = fixtures.path('x.txt');
|
const filePath = fixtures.path('x.txt');
|
||||||
const fileHandle = await open(filePath, 'r');
|
const fileHandle = await open(filePath, 'r');
|
||||||
let response = await fileHandle.read(buf);
|
try {
|
||||||
assert.strictEqual(response.bytesRead, byte);
|
let response = await fileHandle.read(buf);
|
||||||
response = await read(fileHandle, buf, 0, undefined, 0);
|
assert.strictEqual(response.bytesRead, byte);
|
||||||
assert.strictEqual(response.bytesRead, byte);
|
response = await read(fileHandle, buf, 0, undefined, 0);
|
||||||
response = await read(fileHandle, buf, 0, null, 0);
|
assert.strictEqual(response.bytesRead, byte);
|
||||||
assert.strictEqual(response.bytesRead, byte);
|
response = await read(fileHandle, buf, 0, null, 0);
|
||||||
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
|
assert.strictEqual(response.bytesRead, byte);
|
||||||
assert.strictEqual(response.bytesRead, byte);
|
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
|
||||||
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
|
assert.strictEqual(response.bytesRead, byte);
|
||||||
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() {
|
(async function() {
|
||||||
|
|
|
@ -47,8 +47,12 @@ async function validateReadFileProc() {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
|
const fileHandle = await open('/proc/sys/kernel/hostname', 'r');
|
||||||
const hostname = await fileHandle.readFile();
|
try {
|
||||||
assert.ok(hostname.length > 0);
|
const hostname = await fileHandle.readFile();
|
||||||
|
assert.ok(hostname.length > 0);
|
||||||
|
} finally {
|
||||||
|
await fileHandle.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function doReadAndCancel() {
|
async function doReadAndCancel() {
|
||||||
|
@ -72,15 +76,18 @@ async function doReadAndCancel() {
|
||||||
{
|
{
|
||||||
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
|
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
|
||||||
const fileHandle = await open(filePathForHandle, 'w+');
|
const fileHandle = await open(filePathForHandle, 'w+');
|
||||||
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
|
try {
|
||||||
fs.writeFileSync(filePathForHandle, buffer);
|
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
|
||||||
const controller = new AbortController();
|
fs.writeFileSync(filePathForHandle, buffer);
|
||||||
const { signal } = controller;
|
const controller = new AbortController();
|
||||||
process.nextTick(() => controller.abort());
|
const { signal } = controller;
|
||||||
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
|
process.nextTick(() => controller.abort());
|
||||||
name: 'AbortError'
|
await assert.rejects(readFile(fileHandle, common.mustNotMutateObjectDeep({ signal })), {
|
||||||
}, 'tick-0');
|
name: 'AbortError'
|
||||||
await fileHandle.close();
|
}, 'tick-0');
|
||||||
|
} finally {
|
||||||
|
await fileHandle.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signal aborted right before buffer read
|
// Signal aborted right before buffer read
|
||||||
|
@ -90,15 +97,17 @@ async function doReadAndCancel() {
|
||||||
fs.writeFileSync(newFile, buffer);
|
fs.writeFileSync(newFile, buffer);
|
||||||
|
|
||||||
const fileHandle = await open(newFile, 'r');
|
const fileHandle = await open(newFile, 'r');
|
||||||
|
try {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const { signal } = controller;
|
const { signal } = controller;
|
||||||
tick(1, () => controller.abort());
|
tick(1, () => controller.abort());
|
||||||
await assert.rejects(fileHandle.readFile(common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
|
await assert.rejects(fileHandle.readFile(
|
||||||
name: 'AbortError'
|
common.mustNotMutateObjectDeep({ signal, encoding: 'utf8' })), {
|
||||||
}, 'tick-1');
|
name: 'AbortError'
|
||||||
|
}, 'tick-1');
|
||||||
await fileHandle.close();
|
} finally {
|
||||||
|
await fileHandle.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate file size is within range for reading
|
// Validate file size is within range for reading
|
||||||
|
|
|
@ -80,6 +80,10 @@ class Source {
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async cancel() {
|
||||||
|
await this.file.close();
|
||||||
|
}
|
||||||
|
|
||||||
async pull(controller) {
|
async pull(controller) {
|
||||||
const byobRequest = controller.byobRequest;
|
const byobRequest = controller.byobRequest;
|
||||||
assert.match(inspect(byobRequest), /ReadableStreamBYOBRequest/);
|
assert.match(inspect(byobRequest), /ReadableStreamBYOBRequest/);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue