mirror of
https://github.com/nodejs/node.git
synced 2025-05-15 00:52:29 +00:00

fs.write(v) is not guaranteed to write everything in a single call. Make sure we don't assume so. PR-URL: https://github.com/nodejs/node/pull/49211 Co-authored-by: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
import * as common from '../common/index.mjs';
|
|
import tmpdir from '../common/tmpdir.js';
|
|
import assert from 'node:assert';
|
|
import fs from 'node:fs';
|
|
import { describe, it, mock } from 'node:test';
|
|
import { finished } from 'node:stream/promises';
|
|
|
|
tmpdir.refresh();
|
|
const file = tmpdir.resolve('writeStreamEAGAIN.txt');
|
|
const errorWithEAGAIN = (fd, buffer, offset, length, position, callback) => {
|
|
callback(Object.assign(new Error(), { code: 'EAGAIN' }), 0, buffer);
|
|
};
|
|
|
|
describe('WriteStream EAGAIN', { concurrency: true }, () => {
|
|
it('_write', async () => {
|
|
const mockWrite = mock.fn(fs.write);
|
|
mockWrite.mock.mockImplementationOnce(errorWithEAGAIN);
|
|
const stream = fs.createWriteStream(file, {
|
|
fs: {
|
|
open: common.mustCall(fs.open),
|
|
write: mockWrite,
|
|
close: common.mustCall(fs.close),
|
|
}
|
|
});
|
|
stream.end('foo');
|
|
stream.on('close', common.mustCall());
|
|
stream.on('error', common.mustNotCall());
|
|
await finished(stream);
|
|
assert.strictEqual(mockWrite.mock.callCount(), 2);
|
|
assert.strictEqual(fs.readFileSync(file, 'utf8'), 'foo');
|
|
});
|
|
|
|
it('_write', async () => {
|
|
const stream = fs.createWriteStream(file);
|
|
mock.getter(stream, 'destroyed', () => true);
|
|
stream.end('foo');
|
|
await finished(stream).catch(common.mustCall());
|
|
});
|
|
});
|