Repository: nodejs/node
Pull request: test: close FileHandle objects in tests explicitly

Separated out from #58536. Specifically *not* using `using` so the test changes can be backported.

This is in preparation for moving the close-filehandle-on-gc to EOL.

Changed files:
filename: test/parallel/test-fs-open.js
patch: @@ -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());

filename: test/parallel/test-fs-promises-file-handle-read.js
patch: @@ -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
@@ -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();
   }
 }
 
@@ -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() {

filename: test/parallel/test-fs-promises-file-handle-readFile.js
patch: @@ -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

filename: test/parallel/test-whatwg-readablebytestream.js
patch: @@ -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/);