From 2f23f17f93b51d6c2227fb7232743a199fdc96a9 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Wed, 18 Jan 2023 01:33:55 +0800 Subject: [PATCH] test: reduce `fs-write-optional-params` flakiness PR-URL: https://github.com/nodejs/node/pull/46238 Fixes: https://github.com/nodejs/node/issues/46144 Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel --- .../test-fs-promises-write-optional-params.js | 37 +++++++++++-------- .../parallel/test-fs-write-optional-params.js | 30 ++++++++------- .../test-fs-write-sync-optional-params.js | 24 ++++++++---- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/test/parallel/test-fs-promises-write-optional-params.js b/test/parallel/test-fs-promises-write-optional-params.js index fb16af15e857d7..4dd87155153769 100644 --- a/test/parallel/test-fs-promises-write-optional-params.js +++ b/test/parallel/test-fs-promises-write-optional-params.js @@ -33,26 +33,33 @@ async function testInvalid(dest, expectedCode, ...params) { async function testValid(dest, buffer, options) { const length = options?.length; const offset = options?.offset; - let fh; - try { - fh = await fsPromises.open(dest, 'w+'); - const writeResult = await fh.write(buffer, options); - const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer); + let fh, writeResult, writeBufCopy, readResult, readBufCopy; - const readResult = await fh.read(buffer, options); - const readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer); + try { + fh = await fsPromises.open(dest, 'w'); + writeResult = await fh.write(buffer, options); + writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer); + } finally { + await fh?.close(); + } - assert.ok(writeResult.bytesWritten >= readResult.bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(writeResult.bytesWritten, length); - } - if (offset === undefined || offset === 0) { - assert.deepStrictEqual(writeBufCopy, readBufCopy); - } - assert.deepStrictEqual(writeResult.buffer, readResult.buffer); + try { + fh = await fsPromises.open(dest, 'r'); + readResult = await fh.read(buffer, options); + readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer); } finally { await fh?.close(); } + + assert.ok(writeResult.bytesWritten >= readResult.bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(writeResult.bytesWritten, length); + assert.strictEqual(readResult.bytesRead, length); + } + if (offset === undefined || offset === 0) { + assert.deepStrictEqual(writeBufCopy, readBufCopy); + } + assert.deepStrictEqual(writeResult.buffer, readResult.buffer); } (async () => { diff --git a/test/parallel/test-fs-write-optional-params.js b/test/parallel/test-fs-write-optional-params.js index 5072884b95c184..6f61f5b41864a2 100644 --- a/test/parallel/test-fs-write-optional-params.js +++ b/test/parallel/test-fs-write-optional-params.js @@ -29,22 +29,26 @@ function testValidCb(buffer, options, index, callback) { const length = options?.length; const offset = options?.offset; const dest = path.resolve(tmpdir.path, `rwopt_valid_${index}`); - fs.open(dest, 'w+', common.mustSucceed((fd) => { + fs.open(dest, 'w', common.mustSucceed((fd) => { fs.write(fd, buffer, options, common.mustSucceed((bytesWritten, bufferWritten) => { const writeBufCopy = Uint8Array.prototype.slice.call(bufferWritten); + fs.close(fd, common.mustSucceed(() => { + fs.open(dest, 'r', common.mustSucceed((fd) => { + fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => { + const readBufCopy = Uint8Array.prototype.slice.call(bufferRead); - fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => { - const readBufCopy = Uint8Array.prototype.slice.call(bufferRead); - - assert.ok(bytesWritten >= bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(bytesWritten, length); - } - if (offset === undefined || offset === 0) { - assert.deepStrictEqual(writeBufCopy, readBufCopy); - } - assert.deepStrictEqual(bufferWritten, bufferRead); - fs.close(fd, common.mustSucceed(callback)); + assert.ok(bytesWritten >= bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(bytesWritten, length); + assert.strictEqual(bytesRead, length); + } + if (offset === undefined || offset === 0) { + assert.deepStrictEqual(writeBufCopy, readBufCopy); + } + assert.deepStrictEqual(bufferWritten, bufferRead); + fs.close(fd, common.mustSucceed(callback)); + })); + })); })); })); })); diff --git a/test/parallel/test-fs-write-sync-optional-params.js b/test/parallel/test-fs-write-sync-optional-params.js index eeaa9c2bbd5595..ac2c501f682242 100644 --- a/test/parallel/test-fs-write-sync-optional-params.js +++ b/test/parallel/test-fs-write-sync-optional-params.js @@ -32,19 +32,27 @@ function testInvalid(dest, expectedCode, ...bufferAndOptions) { function testValid(dest, buffer, options) { const length = options?.length; - let fd; + let fd, bytesWritten, bytesRead; + try { - fd = fs.openSync(dest, 'w+'); - const bytesWritten = fs.writeSync(fd, buffer, options); - const bytesRead = fs.readSync(fd, buffer, options); + fd = fs.openSync(dest, 'w'); + bytesWritten = fs.writeSync(fd, buffer, options); + } finally { + if (fd != null) fs.closeSync(fd); + } - assert.ok(bytesWritten >= bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(bytesWritten, length); - } + try { + fd = fs.openSync(dest, 'r'); + bytesRead = fs.readSync(fd, buffer, options); } finally { if (fd != null) fs.closeSync(fd); } + + assert.ok(bytesWritten >= bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(bytesWritten, length); + assert.strictEqual(bytesRead, length); + } } {