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