Skip to content
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

test: reduce fs-write-optional-params flakiness #46238

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 22 additions & 15 deletions test/parallel/test-fs-promises-write-optional-params.js
Original file line number Diff line number Diff line change
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
30 changes: 17 additions & 13 deletions test/parallel/test-fs-write-optional-params.js
Original file line number Diff line number Diff line change
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.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));
}));
}));
}));
}));
}));
Expand Down
24 changes: 16 additions & 8 deletions test/parallel/test-fs-write-sync-optional-params.js
Original file line number Diff line number Diff line change
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