1'use strict';
2const common = require('../common');
3const stream = require('stream');
4const assert = require('assert');
5
6{
7  const r = new stream.Readable({
8    autoDestroy: true,
9    read() {
10      this.push('hello');
11      this.push('world');
12      this.push(null);
13    },
14    destroy: common.mustCall((err, cb) => cb())
15  });
16
17  let ended = false;
18
19  r.resume();
20
21  r.on('end', common.mustCall(() => {
22    ended = true;
23  }));
24
25  r.on('close', common.mustCall(() => {
26    assert(ended);
27  }));
28}
29
30{
31  const w = new stream.Writable({
32    autoDestroy: true,
33    write(data, enc, cb) {
34      cb(null);
35    },
36    destroy: common.mustCall((err, cb) => cb())
37  });
38
39  let finished = false;
40
41  w.write('hello');
42  w.write('world');
43  w.end();
44
45  w.on('finish', common.mustCall(() => {
46    finished = true;
47  }));
48
49  w.on('close', common.mustCall(() => {
50    assert(finished);
51  }));
52}
53
54{
55  const t = new stream.Transform({
56    autoDestroy: true,
57    transform(data, enc, cb) {
58      cb(null, data);
59    },
60    destroy: common.mustCall((err, cb) => cb())
61  });
62
63  let ended = false;
64  let finished = false;
65
66  t.write('hello');
67  t.write('world');
68  t.end();
69
70  t.resume();
71
72  t.on('end', common.mustCall(() => {
73    ended = true;
74  }));
75
76  t.on('finish', common.mustCall(() => {
77    finished = true;
78  }));
79
80  t.on('close', common.mustCall(() => {
81    assert(ended);
82    assert(finished);
83  }));
84}
85
86{
87  const r = new stream.Readable({
88    read() {
89      r2.emit('error', new Error('fail'));
90    }
91  });
92  const r2 = new stream.Readable({
93    autoDestroy: true,
94    destroy: common.mustCall((err, cb) => cb())
95  });
96
97  r.pipe(r2);
98}
99
100{
101  const r = new stream.Readable({
102    read() {
103      w.emit('error', new Error('fail'));
104    }
105  });
106  const w = new stream.Writable({
107    autoDestroy: true,
108    destroy: common.mustCall((err, cb) => cb())
109  });
110
111  r.pipe(w);
112}
113