1 var assert = require("chai").assert;
3 var stream = require("readable-stream");
5 var duplexer2 = require("../");
7 describe("duplexer2", function() {
8 var writable, readable;
10 beforeEach(function() {
11 writable = new stream.Writable({objectMode: true});
12 readable = new stream.Readable({objectMode: true});
14 writable._write = function _write(input, encoding, done) {
18 readable._read = function _read(n) {
22 it("should interact with the writable stream properly for writing", function(done) {
23 var duplex = duplexer2(writable, readable);
25 writable._write = function _write(input, encoding, _done) {
26 assert.strictEqual(input, "well hello there");
31 duplex.write("well hello there");
34 it("should interact with the readable stream properly for reading", function(done) {
35 var duplex = duplexer2(writable, readable);
37 duplex.on("data", function(e) {
38 assert.strictEqual(e, "well hello there");
43 readable.push("well hello there");
46 it("should end the writable stream, causing it to finish", function(done) {
47 var duplex = duplexer2(writable, readable);
49 writable.once("finish", done);
54 it("should finish when the writable stream finishes", function(done) {
55 var duplex = duplexer2(writable, readable);
57 duplex.once("finish", done);
62 it("should end when the readable stream ends", function(done) {
63 var duplex = duplexer2(writable, readable);
65 // required to let "end" fire without reading
67 duplex.once("end", done);
72 it("should bubble errors from the writable stream when no behaviour is specified", function(done) {
73 var duplex = duplexer2(writable, readable);
75 var originalErr = Error("testing");
77 duplex.on("error", function(err) {
78 assert.strictEqual(err, originalErr);
83 writable.emit("error", originalErr);
86 it("should bubble errors from the readable stream when no behaviour is specified", function(done) {
87 var duplex = duplexer2(writable, readable);
89 var originalErr = Error("testing");
91 duplex.on("error", function(err) {
92 assert.strictEqual(err, originalErr);
97 readable.emit("error", originalErr);
100 it("should bubble errors from the writable stream when bubbleErrors is true", function(done) {
101 var duplex = duplexer2({bubbleErrors: true}, writable, readable);
103 var originalErr = Error("testing");
105 duplex.on("error", function(err) {
106 assert.strictEqual(err, originalErr);
111 writable.emit("error", originalErr);
114 it("should bubble errors from the readable stream when bubbleErrors is true", function(done) {
115 var duplex = duplexer2({bubbleErrors: true}, writable, readable);
117 var originalErr = Error("testing");
119 duplex.on("error", function(err) {
120 assert.strictEqual(err, originalErr);
125 readable.emit("error", originalErr);
128 it("should not bubble errors from the writable stream when bubbleErrors is false", function(done) {
129 var duplex = duplexer2({bubbleErrors: false}, writable, readable);
131 var timeout = setTimeout(done, 25);
133 duplex.on("error", function(err) {
134 clearTimeout(timeout);
136 return done(Error("shouldn't bubble error"));
139 // prevent uncaught error exception
140 writable.on("error", function() {});
142 writable.emit("error", Error("testing"));
145 it("should not bubble errors from the readable stream when bubbleErrors is false", function(done) {
146 var duplex = duplexer2({bubbleErrors: false}, writable, readable);
148 var timeout = setTimeout(done, 25);
150 duplex.on("error", function(err) {
151 clearTimeout(timeout);
153 return done(Error("shouldn't bubble error"));
156 // prevent uncaught error exception
157 readable.on("error", function() {});
159 readable.emit("error", Error("testing"));