Skip to content

Commit 75b8db4

Browse files
LiviaMedeirosjuanarbol
authored andcommitted
test: reduce fs-write-optional-params flakiness
PR-URL: #46238 Fixes: #46144 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 91ece41 commit 75b8db4

3 files changed

+56
-37
lines changed

test/parallel/test-fs-promises-write-optional-params.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,33 @@ async function testInvalid(dest, expectedCode, ...params) {
3333
async function testValid(dest, buffer, options) {
3434
const length = options?.length;
3535
const offset = options?.offset;
36-
let fh;
37-
try {
38-
fh = await fsPromises.open(dest, 'w+');
39-
const writeResult = await fh.write(buffer, options);
40-
const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer);
36+
let fh, writeResult, writeBufCopy, readResult, readBufCopy;
4137

42-
const readResult = await fh.read(buffer, options);
43-
const readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer);
38+
try {
39+
fh = await fsPromises.open(dest, 'w');
40+
writeResult = await fh.write(buffer, options);
41+
writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer);
42+
} finally {
43+
await fh?.close();
44+
}
4445

45-
assert.ok(writeResult.bytesWritten >= readResult.bytesRead);
46-
if (length !== undefined && length !== null) {
47-
assert.strictEqual(writeResult.bytesWritten, length);
48-
}
49-
if (offset === undefined || offset === 0) {
50-
assert.deepStrictEqual(writeBufCopy, readBufCopy);
51-
}
52-
assert.deepStrictEqual(writeResult.buffer, readResult.buffer);
46+
try {
47+
fh = await fsPromises.open(dest, 'r');
48+
readResult = await fh.read(buffer, options);
49+
readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer);
5350
} finally {
5451
await fh?.close();
5552
}
53+
54+
assert.ok(writeResult.bytesWritten >= readResult.bytesRead);
55+
if (length !== undefined && length !== null) {
56+
assert.strictEqual(writeResult.bytesWritten, length);
57+
assert.strictEqual(readResult.bytesRead, length);
58+
}
59+
if (offset === undefined || offset === 0) {
60+
assert.deepStrictEqual(writeBufCopy, readBufCopy);
61+
}
62+
assert.deepStrictEqual(writeResult.buffer, readResult.buffer);
5663
}
5764

5865
(async () => {

test/parallel/test-fs-write-optional-params.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,26 @@ function testValidCb(buffer, options, index, callback) {
2929
const length = options?.length;
3030
const offset = options?.offset;
3131
const dest = path.resolve(tmpdir.path, `rwopt_valid_${index}`);
32-
fs.open(dest, 'w+', common.mustSucceed((fd) => {
32+
fs.open(dest, 'w', common.mustSucceed((fd) => {
3333
fs.write(fd, buffer, options, common.mustSucceed((bytesWritten, bufferWritten) => {
3434
const writeBufCopy = Uint8Array.prototype.slice.call(bufferWritten);
35-
36-
fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => {
37-
const readBufCopy = Uint8Array.prototype.slice.call(bufferRead);
38-
39-
assert.ok(bytesWritten >= bytesRead);
40-
if (length !== undefined && length !== null) {
41-
assert.strictEqual(bytesWritten, length);
42-
}
43-
if (offset === undefined || offset === 0) {
44-
assert.deepStrictEqual(writeBufCopy, readBufCopy);
45-
}
46-
assert.deepStrictEqual(bufferWritten, bufferRead);
47-
fs.close(fd, common.mustSucceed(callback));
35+
fs.close(fd, common.mustSucceed(() => {
36+
fs.open(dest, 'r', common.mustSucceed((fd) => {
37+
fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => {
38+
const readBufCopy = Uint8Array.prototype.slice.call(bufferRead);
39+
40+
assert.ok(bytesWritten >= bytesRead);
41+
if (length !== undefined && length !== null) {
42+
assert.strictEqual(bytesWritten, length);
43+
assert.strictEqual(bytesRead, length);
44+
}
45+
if (offset === undefined || offset === 0) {
46+
assert.deepStrictEqual(writeBufCopy, readBufCopy);
47+
}
48+
assert.deepStrictEqual(bufferWritten, bufferRead);
49+
fs.close(fd, common.mustSucceed(callback));
50+
}));
51+
}));
4852
}));
4953
}));
5054
}));

test/parallel/test-fs-write-sync-optional-params.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,27 @@ function testInvalid(dest, expectedCode, ...bufferAndOptions) {
3232

3333
function testValid(dest, buffer, options) {
3434
const length = options?.length;
35-
let fd;
35+
let fd, bytesWritten, bytesRead;
36+
3637
try {
37-
fd = fs.openSync(dest, 'w+');
38-
const bytesWritten = fs.writeSync(fd, buffer, options);
39-
const bytesRead = fs.readSync(fd, buffer, options);
38+
fd = fs.openSync(dest, 'w');
39+
bytesWritten = fs.writeSync(fd, buffer, options);
40+
} finally {
41+
if (fd != null) fs.closeSync(fd);
42+
}
4043

41-
assert.ok(bytesWritten >= bytesRead);
42-
if (length !== undefined && length !== null) {
43-
assert.strictEqual(bytesWritten, length);
44-
}
44+
try {
45+
fd = fs.openSync(dest, 'r');
46+
bytesRead = fs.readSync(fd, buffer, options);
4547
} finally {
4648
if (fd != null) fs.closeSync(fd);
4749
}
50+
51+
assert.ok(bytesWritten >= bytesRead);
52+
if (length !== undefined && length !== null) {
53+
assert.strictEqual(bytesWritten, length);
54+
assert.strictEqual(bytesRead, length);
55+
}
4856
}
4957

5058
{

0 commit comments

Comments
 (0)