1 | var should = require('should');
|
2 | var through = require('through2');
|
3 | var OrderedStreams = require('../');
|
4 |
|
5 | describe('ordered-read-streams', function () {
|
6 | it('should end if no streams are given', function (done) {
|
7 | var streams = new OrderedStreams();
|
8 | streams.on('data', function () {
|
9 | done('error');
|
10 | });
|
11 | streams.on('end', done);
|
12 | });
|
13 |
|
14 | it('should throw error if one or more streams are not readable', function (done) {
|
15 | var writable = { readable: false };
|
16 |
|
17 | try {
|
18 | new OrderedStreams(writable);
|
19 | } catch (e) {
|
20 | e.message.should.equal('All input streams must be readable');
|
21 | done();
|
22 | }
|
23 | });
|
24 |
|
25 | it('should emit data from all streams', function(done) {
|
26 | var s1 = through.obj(function (data, enc, next) {
|
27 | this.push(data);
|
28 | next();
|
29 | });
|
30 | var s2 = through.obj(function (data, enc, next) {
|
31 | this.push(data);
|
32 | next();
|
33 | });
|
34 | var s3 = through.obj(function (data, enc, next) {
|
35 | this.push(data);
|
36 | next();
|
37 | });
|
38 |
|
39 | var streams = new OrderedStreams([s1, s2, s3]);
|
40 | var results = [];
|
41 | streams.on('data', function (data) {
|
42 | results.push(data);
|
43 | });
|
44 | streams.on('end', function () {
|
45 | results.length.should.be.exactly(3);
|
46 | results[0].should.equal('stream 1');
|
47 | results[1].should.equal('stream 2');
|
48 | results[2].should.equal('stream 3');
|
49 | done();
|
50 | });
|
51 |
|
52 | s1.write('stream 1');
|
53 | s1.end();
|
54 |
|
55 | s2.write('stream 2');
|
56 | s2.end();
|
57 |
|
58 | s3.write('stream 3');
|
59 | s3.end();
|
60 | });
|
61 |
|
62 | it('should emit all data event from each stream', function (done) {
|
63 | var s = through.obj(function (data, enc, next) {
|
64 | this.push(data);
|
65 | next();
|
66 | });
|
67 |
|
68 | var streams = new OrderedStreams(s);
|
69 | var results = [];
|
70 | streams.on('data', function (data) {
|
71 | results.push(data);
|
72 | });
|
73 | streams.on('end', function () {
|
74 | results.length.should.be.exactly(3);
|
75 | done();
|
76 | });
|
77 |
|
78 | s.write('data1');
|
79 | s.write('data2');
|
80 | s.write('data3');
|
81 | s.end();
|
82 | });
|
83 |
|
84 | it('should preserve streams order', function(done) {
|
85 | var s1 = through.obj(function (data, enc, next) {
|
86 | var self = this;
|
87 | setTimeout(function () {
|
88 | self.push(data);
|
89 | next();
|
90 | }, 200);
|
91 | });
|
92 | var s2 = through.obj(function (data, enc, next) {
|
93 | var self = this;
|
94 | setTimeout(function () {
|
95 | self.push(data);
|
96 | next();
|
97 | }, 30);
|
98 | });
|
99 | var s3 = through.obj(function (data, enc, next) {
|
100 | var self = this;
|
101 | setTimeout(function () {
|
102 | self.push(data);
|
103 | next();
|
104 | }, 100);
|
105 | });
|
106 |
|
107 | var streams = new OrderedStreams([s1, s2, s3]);
|
108 | var results = [];
|
109 | streams.on('data', function (data) {
|
110 | results.push(data);
|
111 | });
|
112 | streams.on('end', function () {
|
113 | results.length.should.equal(3);
|
114 | results[0].should.equal('stream 1');
|
115 | results[1].should.equal('stream 2');
|
116 | results[2].should.equal('stream 3');
|
117 | done();
|
118 | });
|
119 |
|
120 | s1.write('stream 1');
|
121 | s1.end();
|
122 |
|
123 | s2.write('stream 2');
|
124 | s2.end();
|
125 |
|
126 | s3.write('stream 3');
|
127 | s3.end();
|
128 | });
|
129 |
|
130 | it('should emit stream errors downstream', function (done) {
|
131 | var s = through.obj(function (data, enc, next) {
|
132 | this.emit('error', new Error('stahp!'));
|
133 | next();
|
134 | });
|
135 | var s2 = through.obj(function (data, enc, next) {
|
136 | this.push(data);
|
137 | next();
|
138 | });
|
139 |
|
140 | var errMsg;
|
141 | var streamData;
|
142 | var streams = new OrderedStreams([s, s2]);
|
143 | streams.on('data', function (data) {
|
144 | streamData = data;
|
145 | });
|
146 | streams.on('error', function (err) {
|
147 | errMsg = err.message;
|
148 | });
|
149 | streams.on('end', function () {
|
150 | errMsg.should.equal('stahp!');
|
151 | streamData.should.equal('Im ok!');
|
152 | done();
|
153 | });
|
154 |
|
155 | s.write('go');
|
156 | s.end();
|
157 | s2.write('Im ok!');
|
158 | s2.end();
|
159 | });
|
160 | });
|