Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var assert = require("chai").assert;
2
3
var stream = require("readable-stream");
4
5
var duplexer2 = require("../");
6
7
describe("duplexer2", function() {
8
var writable, readable;
9
10
beforeEach(function() {
11
writable = new stream.Writable({objectMode: true});
12
readable = new stream.Readable({objectMode: true});
13
14
writable._write = function _write(input, encoding, done) {
15
return done();
16
};
17
18
readable._read = function _read(n) {
19
};
20
});
21
22
it("should interact with the writable stream properly for writing", function(done) {
23
var duplex = duplexer2(writable, readable);
24
25
writable._write = function _write(input, encoding, _done) {
26
assert.strictEqual(input, "well hello there");
27
28
return done();
29
};
30
31
duplex.write("well hello there");
32
});
33
34
it("should interact with the readable stream properly for reading", function(done) {
35
var duplex = duplexer2(writable, readable);
36
37
duplex.on("data", function(e) {
38
assert.strictEqual(e, "well hello there");
39
40
return done();
41
});
42
43
readable.push("well hello there");
44
});
45
46
it("should end the writable stream, causing it to finish", function(done) {
47
var duplex = duplexer2(writable, readable);
48
49
writable.once("finish", done);
50
51
duplex.end();
52
});
53
54
it("should finish when the writable stream finishes", function(done) {
55
var duplex = duplexer2(writable, readable);
56
57
duplex.once("finish", done);
58
59
writable.end();
60
});
61
62
it("should end when the readable stream ends", function(done) {
63
var duplex = duplexer2(writable, readable);
64
65
// required to let "end" fire without reading
66
duplex.resume();
67
duplex.once("end", done);
68
69
readable.push(null);
70
});
71
72
it("should bubble errors from the writable stream when no behaviour is specified", function(done) {
73
var duplex = duplexer2(writable, readable);
74
75
var originalErr = Error("testing");
76
77
duplex.on("error", function(err) {
78
assert.strictEqual(err, originalErr);
79
80
return done();
81
});
82
83
writable.emit("error", originalErr);
84
});
85
86
it("should bubble errors from the readable stream when no behaviour is specified", function(done) {
87
var duplex = duplexer2(writable, readable);
88
89
var originalErr = Error("testing");
90
91
duplex.on("error", function(err) {
92
assert.strictEqual(err, originalErr);
93
94
return done();
95
});
96
97
readable.emit("error", originalErr);
98
});
99
100
it("should bubble errors from the writable stream when bubbleErrors is true", function(done) {
101
var duplex = duplexer2({bubbleErrors: true}, writable, readable);
102
103
var originalErr = Error("testing");
104
105
duplex.on("error", function(err) {
106
assert.strictEqual(err, originalErr);
107
108
return done();
109
});
110
111
writable.emit("error", originalErr);
112
});
113
114
it("should bubble errors from the readable stream when bubbleErrors is true", function(done) {
115
var duplex = duplexer2({bubbleErrors: true}, writable, readable);
116
117
var originalErr = Error("testing");
118
119
duplex.on("error", function(err) {
120
assert.strictEqual(err, originalErr);
121
122
return done();
123
});
124
125
readable.emit("error", originalErr);
126
});
127
128
it("should not bubble errors from the writable stream when bubbleErrors is false", function(done) {
129
var duplex = duplexer2({bubbleErrors: false}, writable, readable);
130
131
var timeout = setTimeout(done, 25);
132
133
duplex.on("error", function(err) {
134
clearTimeout(timeout);
135
136
return done(Error("shouldn't bubble error"));
137
});
138
139
// prevent uncaught error exception
140
writable.on("error", function() {});
141
142
writable.emit("error", Error("testing"));
143
});
144
145
it("should not bubble errors from the readable stream when bubbleErrors is false", function(done) {
146
var duplex = duplexer2({bubbleErrors: false}, writable, readable);
147
148
var timeout = setTimeout(done, 25);
149
150
duplex.on("error", function(err) {
151
clearTimeout(timeout);
152
153
return done(Error("shouldn't bubble error"));
154
});
155
156
// prevent uncaught error exception
157
readable.on("error", function() {});
158
159
readable.emit("error", Error("testing"));
160
});
161
});
162
163