• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const stream = require('stream');
6
7{
8  // Invoke end callback on failure.
9  const writable = new stream.Writable();
10
11  writable._write = (chunk, encoding, cb) => {
12    process.nextTick(cb, new Error('kaboom'));
13  };
14
15  writable.on('error', common.mustCall((err) => {
16    assert.strictEqual(err.message, 'kaboom');
17  }));
18  writable.write('asd');
19  writable.end(common.mustCall((err) => {
20    assert.strictEqual(err.message, 'kaboom');
21  }));
22  writable.end(common.mustCall((err) => {
23    assert.strictEqual(err.message, 'kaboom');
24  }));
25}
26
27{
28  // Don't invoke end callback twice
29  const writable = new stream.Writable();
30
31  writable._write = (chunk, encoding, cb) => {
32    process.nextTick(cb);
33  };
34
35  let called = false;
36  writable.end('asd', common.mustCall((err) => {
37    called = true;
38    assert.strictEqual(err, undefined);
39  }));
40
41  writable.on('error', common.mustCall((err) => {
42    assert.strictEqual(err.message, 'kaboom');
43  }));
44  writable.on('finish', common.mustCall(() => {
45    assert.strictEqual(called, true);
46    writable.emit('error', new Error('kaboom'));
47  }));
48}
49