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: fix flaky test-fs-promises-file-handle-read #37371

Merged
merged 1 commit into from
Feb 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());