Skip to content

Commit

Permalink
test: fix flaky test-fs-promises-file-handle-read
Browse files Browse the repository at this point in the history
tmpdir.refresh() cannot be called multiple times reliably on Raspberry
Pi in CI because NFS might optimistically report a path as
removed before it actually is. At least, that's what I think is going
on. Anyway, tmpdir.refresh() is generally designed to be called once,
so let's just call it once.

PR-URL: #37371
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Trott authored and targos committed Feb 28, 2021
1 parent 28cbad3 commit 19d6eb9
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions test/parallel/test-fs-promises-file-handle-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;

async function read(fileHandle, buffer, offset, length, position) {
return useConf ?
async function read(fileHandle, buffer, offset, length, position, options) {
return options.useConf ?
fileHandle.read({ buffer, offset, length, position }) :
fileHandle.read(buffer, offset, length, position);
}

async function validateRead(data, file) {
async function validateRead(data, file, options) {
const filePath = path.resolve(tmpDir, file);
const buffer = Buffer.from(data, 'utf8');

Expand All @@ -31,7 +31,8 @@ async function validateRead(data, file) {
fs.closeSync(fd);

fileHandle.on('close', common.mustCall());
const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
const readAsyncHandle =
await read(fileHandle, Buffer.alloc(11), 0, 11, 0, options);
assert.deepStrictEqual(data.length, readAsyncHandle.bytesRead);
if (data.length)
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
Expand All @@ -47,28 +48,26 @@ async function validateRead(data, file) {
await streamFileHandle.close();
}

async function validateLargeRead() {
async function validateLargeRead(options) {
// Reading beyond file length (3 in this case) should return no data.
// This is a test for a bug where reads > uint32 would return data
// 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);
const readHandle =
await read(fileHandle, Buffer.alloc(1), 0, 1, pos, options);

assert.strictEqual(readHandle.bytesRead, 0);
}

let useConf = false;

(async function() {
for (const value of [false, true]) {
tmpdir.refresh();
useConf = value;

await validateRead('Hello world', 'tmp-read-file.txt')
.then(validateRead('', 'tmp-read-empty-file.txt'))
.then(validateLargeRead)
.then(common.mustCall());
}
tmpdir.refresh();
await validateRead('Hello world', 'read-file', { useConf: false });
await validateRead('', 'read-empty-file', { useConf: false });
await validateRead('Hello world', 'read-file-conf', { useConf: true });
await validateRead('', 'read-empty-file-conf', { useConf: true });
await validateLargeRead({ useConf: false });
await validateLargeRead({ useConf: true });
})().then(common.mustCall());

0 comments on commit 19d6eb9

Please sign in to comment.