2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2011-04-23 02:31:14 +00:00
|
|
|
// Test that having a bunch of streams piping in parallel
|
|
|
|
// doesn't break anything.
|
|
|
|
|
2015-12-24 00:02:12 +00:00
|
|
|
require('../common');
|
2011-10-04 22:08:18 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var Stream = require('stream').Stream;
|
2011-04-23 02:31:14 +00:00
|
|
|
var rr = [];
|
|
|
|
var ww = [];
|
|
|
|
var cnt = 100;
|
|
|
|
var chunks = 1000;
|
|
|
|
var chunkSize = 250;
|
|
|
|
var data = new Buffer(chunkSize);
|
|
|
|
var wclosed = 0;
|
|
|
|
var rclosed = 0;
|
|
|
|
|
|
|
|
function FakeStream() {
|
|
|
|
Stream.apply(this);
|
|
|
|
this.wait = false;
|
|
|
|
this.writable = true;
|
|
|
|
this.readable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeStream.prototype = Object.create(Stream.prototype);
|
|
|
|
|
|
|
|
FakeStream.prototype.write = function(chunk) {
|
2011-10-04 22:08:18 +00:00
|
|
|
console.error(this.ID, 'write', this.wait);
|
2011-04-23 02:31:14 +00:00
|
|
|
if (this.wait) {
|
2011-10-04 22:08:18 +00:00
|
|
|
process.nextTick(this.emit.bind(this, 'drain'));
|
2011-04-23 02:31:14 +00:00
|
|
|
}
|
|
|
|
this.wait = !this.wait;
|
|
|
|
return this.wait;
|
|
|
|
};
|
|
|
|
|
|
|
|
FakeStream.prototype.end = function() {
|
2011-10-04 22:08:18 +00:00
|
|
|
this.emit('end');
|
2011-04-23 02:31:14 +00:00
|
|
|
process.nextTick(this.close.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
// noop - closes happen automatically on end.
|
|
|
|
FakeStream.prototype.close = function() {
|
2011-10-04 22:08:18 +00:00
|
|
|
this.emit('close');
|
2011-04-23 02:31:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// expect all streams to close properly.
|
2011-10-04 22:08:18 +00:00
|
|
|
process.on('exit', function() {
|
|
|
|
assert.equal(cnt, wclosed, 'writable streams closed');
|
|
|
|
assert.equal(cnt, rclosed, 'readable streams closed');
|
2011-04-23 02:31:14 +00:00
|
|
|
});
|
|
|
|
|
2011-10-04 22:08:18 +00:00
|
|
|
for (var i = 0; i < chunkSize; i++) {
|
2011-04-23 02:31:14 +00:00
|
|
|
chunkSize[i] = i % 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < cnt; i++) {
|
|
|
|
var r = new FakeStream();
|
2011-10-04 22:08:18 +00:00
|
|
|
r.on('close', function() {
|
|
|
|
console.error(this.ID, 'read close');
|
2011-04-23 02:31:14 +00:00
|
|
|
rclosed++;
|
|
|
|
});
|
|
|
|
rr.push(r);
|
|
|
|
|
|
|
|
var w = new FakeStream();
|
2011-10-04 22:08:18 +00:00
|
|
|
w.on('close', function() {
|
|
|
|
console.error(this.ID, 'write close');
|
2011-04-23 02:31:14 +00:00
|
|
|
wclosed++;
|
|
|
|
});
|
|
|
|
ww.push(w);
|
|
|
|
|
|
|
|
r.ID = w.ID = i;
|
|
|
|
r.pipe(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now start passing through data
|
|
|
|
// simulate a relatively fast async stream.
|
2011-10-04 22:08:18 +00:00
|
|
|
rr.forEach(function(r) {
|
2011-04-23 02:31:14 +00:00
|
|
|
var cnt = chunks;
|
|
|
|
var paused = false;
|
|
|
|
|
2011-10-04 22:08:18 +00:00
|
|
|
r.on('pause', function() {
|
2011-04-23 02:31:14 +00:00
|
|
|
paused = true;
|
|
|
|
});
|
|
|
|
|
2011-10-04 22:08:18 +00:00
|
|
|
r.on('resume', function() {
|
2011-04-23 02:31:14 +00:00
|
|
|
paused = false;
|
|
|
|
step();
|
|
|
|
});
|
|
|
|
|
|
|
|
function step() {
|
2011-10-04 22:08:18 +00:00
|
|
|
r.emit('data', data);
|
2011-04-23 02:31:14 +00:00
|
|
|
if (--cnt === 0) {
|
|
|
|
r.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (paused) return;
|
|
|
|
process.nextTick(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(step);
|
|
|
|
});
|