• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const stream = require('stream');
6
7// Ensure consistency between the finish event when using cork()
8// and writev and when not using them
9
10{
11  const writable = new stream.Writable();
12
13  writable._write = (chunks, encoding, cb) => {
14    cb(new Error('write test error'));
15  };
16
17  writable.on('finish', common.mustNotCall());
18  writable.on('prefinish', common.mustNotCall());
19  writable.on('error', common.mustCall((er) => {
20    assert.strictEqual(er.message, 'write test error');
21  }));
22
23  writable.end('test');
24}
25
26{
27  const writable = new stream.Writable();
28
29  writable._write = (chunks, encoding, cb) => {
30    setImmediate(cb, new Error('write test error'));
31  };
32
33  writable.on('finish', common.mustNotCall());
34  writable.on('prefinish', common.mustNotCall());
35  writable.on('error', common.mustCall((er) => {
36    assert.strictEqual(er.message, 'write test error');
37  }));
38
39  writable.end('test');
40}
41
42{
43  const writable = new stream.Writable();
44
45  writable._write = (chunks, encoding, cb) => {
46    cb(new Error('write test error'));
47  };
48
49  writable._writev = (chunks, cb) => {
50    cb(new Error('writev test error'));
51  };
52
53  writable.on('finish', common.mustNotCall());
54  writable.on('prefinish', common.mustNotCall());
55  writable.on('error', common.mustCall((er) => {
56    assert.strictEqual(er.message, 'writev test error');
57  }));
58
59  writable.cork();
60  writable.write('test');
61
62  setImmediate(function() {
63    writable.end('test');
64  });
65}
66
67{
68  const writable = new stream.Writable();
69
70  writable._write = (chunks, encoding, cb) => {
71    setImmediate(cb, new Error('write test error'));
72  };
73
74  writable._writev = (chunks, cb) => {
75    setImmediate(cb, new Error('writev test error'));
76  };
77
78  writable.on('finish', common.mustNotCall());
79  writable.on('prefinish', common.mustNotCall());
80  writable.on('error', common.mustCall((er) => {
81    assert.strictEqual(er.message, 'writev test error');
82  }));
83
84  writable.cork();
85  writable.write('test');
86
87  setImmediate(function() {
88    writable.end('test');
89  });
90}
91
92// Regression test for
93// https://github.com/nodejs/node/issues/13812
94
95{
96  const rs = new stream.Readable();
97  rs.push('ok');
98  rs.push(null);
99  rs._read = () => {};
100
101  const ws = new stream.Writable();
102
103  ws.on('finish', common.mustNotCall());
104  ws.on('error', common.mustCall());
105
106  ws._write = (chunk, encoding, done) => {
107    setImmediate(done, new Error());
108  };
109  rs.pipe(ws);
110}
111
112{
113  const rs = new stream.Readable();
114  rs.push('ok');
115  rs.push(null);
116  rs._read = () => {};
117
118  const ws = new stream.Writable();
119
120  ws.on('finish', common.mustNotCall());
121  ws.on('error', common.mustCall());
122
123  ws._write = (chunk, encoding, done) => {
124    done(new Error());
125  };
126  rs.pipe(ws);
127}
128
129{
130  const w = new stream.Writable();
131  w._write = (chunk, encoding, cb) => {
132    process.nextTick(cb);
133  };
134  w.on('error', common.mustCall());
135  w.on('finish', common.mustNotCall());
136  w.on('prefinish', () => {
137    w.write("shouldn't write in prefinish listener");
138  });
139  w.end();
140}
141
142{
143  const w = new stream.Writable();
144  w._write = (chunk, encoding, cb) => {
145    process.nextTick(cb);
146  };
147  w.on('error', common.mustCall());
148  w.on('finish', () => {
149    w.write("shouldn't write in finish listener");
150  });
151  w.end();
152}
153