Skip to content

Commit 90e0dc0

Browse files
authored
Merge pull request #503 from johanneswuerbach/mux-readable
Fix stream becoming readable again
2 parents a051869 + eaebbd9 commit 90e0dc0

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

lib/mux.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,11 @@ Mux.prototype._readIncoming = function() {
5555
// are empty, or we exhaust our ability to accept chunks.
5656
function roundrobin(streams) {
5757
var s;
58-
// if there's just one incoming stream we don't have to
59-
// go through all the dequeue/enqueueing
60-
if (streams.length === 1) {
61-
s = streams.shift();
62-
while (accepting) {
63-
var chunk = s.read();
64-
if (chunk !== null) {
65-
accepting = out.write(chunk);
66-
}
67-
else break;
68-
}
69-
if (!accepting) streams.push(s);
70-
}
71-
else {
72-
while (accepting && (s = streams.shift())) {
73-
var chunk = s.read();
74-
if (chunk !== null) {
75-
accepting = out.write(chunk);
76-
streams.push(s);
77-
}
58+
while (accepting && (s = streams.shift())) {
59+
var chunk = s.read();
60+
if (chunk !== null) {
61+
accepting = out.write(chunk);
62+
streams.push(s);
7863
}
7964
}
8065
}
@@ -105,7 +90,7 @@ Mux.prototype._readIncoming = function() {
10590

10691
Mux.prototype._scheduleRead = function() {
10792
var self = this;
108-
93+
10994
if (!self.scheduledRead) {
11095
schedule(function() {
11196
self.scheduledRead = false;

test/mux.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,40 @@ test("single input", function(done) {
5151
input.end();
5252
});
5353

54+
test("single input, resuming stream", function(done) {
55+
var input = stream();
56+
var output = stream();
57+
input.on('end', function() { output.end() });
58+
59+
var mux = new Mux(output);
60+
mux.pipeFrom(input);
61+
62+
// Streams might be blocked and become readable again, simulate this
63+
// using a special read function and a marker
64+
var data = [1,2,3,4,'skip',6,7,8,9];
65+
66+
var oldRead = input.read;
67+
input.read = function(size) {
68+
var val = oldRead.call(input, size)
69+
70+
if (val === 'skip') {
71+
input.emit('readable');
72+
return null
73+
}
74+
75+
return val;
76+
}
77+
78+
data.forEach(input.write.bind(input));
79+
80+
readAllObjects(output, function(vals) {
81+
assert.deepEqual([1,2,3,4,6,7,8,9], vals);
82+
done();
83+
});
84+
85+
input.end();
86+
});
87+
5488
test("two sequential inputs", function(done) {
5589
var input1 = stream();
5690
var input2 = stream();

0 commit comments

Comments
 (0)