|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import tmpdir from '../common/tmpdir.js'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import { describe, it, mock } from 'node:test'; |
| 6 | +import { finished } from 'node:stream/promises'; |
| 7 | + |
| 8 | +const file = tmpdir.resolve('writeStreamEAGAIN.txt'); |
| 9 | +const errorWithEAGAIN = (fd, buffer, offset, length, position, callback) => { |
| 10 | + callback(Object.assign(new Error(), { code: 'EAGAIN' }), 0, buffer); |
| 11 | +} |
| 12 | + |
| 13 | +describe('WriteStream EAGAIN', { concurrency: true }, () => { |
| 14 | + it('_write', async () => { |
| 15 | + const mockWrite = mock.fn(fs.write); |
| 16 | + mockWrite.mock.mockImplementationOnce(errorWithEAGAIN); |
| 17 | + const stream = fs.createWriteStream(file, { |
| 18 | + fs: { |
| 19 | + open: common.mustCall(fs.open), |
| 20 | + write: mockWrite, |
| 21 | + close: common.mustCall(fs.close), |
| 22 | + } |
| 23 | + }); |
| 24 | + stream.end('foo'); |
| 25 | + stream.on('close', common.mustCall()); |
| 26 | + stream.on('error', common.mustNotCall()); |
| 27 | + await finished(stream); |
| 28 | + assert.strictEqual(mockWrite.mock.callCount(), 2); |
| 29 | + assert.strictEqual(fs.readFileSync(file, 'utf8'), 'foo'); |
| 30 | + }); |
| 31 | +}); |
0 commit comments