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