Skip to content

Commit

Permalink
test: reduce fs-write-optional-params flakiness
Browse files Browse the repository at this point in the history
PR-URL: #46238
Fixes: #46144
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros authored and juanarbol committed Mar 5, 2023
1 parent 91ece41 commit 75b8db4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
37 changes: 22 additions & 15 deletions test/parallel/test-fs-promises-write-optional-params.js
Expand Up @@ -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 () => {
Expand Down
32 changes: 18 additions & 14 deletions test/parallel/test-fs-write-optional-params.js
Expand Up @@ -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.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));
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);

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));
}));
}));
}));
}));
}));
Expand Down
24 changes: 16 additions & 8 deletions test/parallel/test-fs-write-sync-optional-params.js
Expand Up @@ -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);
}
}

{
Expand Down

0 comments on commit 75b8db4

Please sign in to comment.