-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: make sure to write entire buffer
fs.write(v) is not guaranteed to write everything in a single call. Make sure we don't assume so. PR-URL: #49211 Co-authored-by: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
- Loading branch information
1 parent
59e66a1
commit 88ba79b
Showing
2 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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()); | ||
}); | ||
}); |