Skip to content

test: close FileHandle objects in tests explicitly #58615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/parallel/test-fs-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
74 changes: 47 additions & 27 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

assert.strictEqual(readHandle.bytesRead, 0);
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);
} 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
Expand All @@ -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'];

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]);
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]);
}
} finally {
await fileHandle.close();
}
}

Expand All @@ -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() {
Expand Down
49 changes: 29 additions & 20 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-whatwg-readablebytestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down
Loading