2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
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');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const Stream = require('stream').Stream;
|
2017-01-08 13:19:00 +00:00
|
|
|
const rr = [];
|
|
|
|
const ww = [];
|
|
|
|
const cnt = 100;
|
|
|
|
const chunks = 1000;
|
|
|
|
const chunkSize = 250;
|
|
|
|
const data = Buffer.allocUnsafe(chunkSize);
|
|
|
|
let wclosed = 0;
|
|
|
|
let rclosed = 0;
|
2011-04-23 02:31:14 +00:00
|
|
|
|
|
|
|
function FakeStream() {
|
|
|
|
Stream.apply(this);
|
|
|
|
this.wait = false;
|
|
|
|
this.writable = true;
|
|
|
|
this.readable = true;
|
|
|
|
}
|
|
|
|
|
2023-01-10 05:38:36 +00:00
|
|
|
FakeStream.prototype = { __proto__: Stream.prototype };
|
2011-04-23 02:31:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Expect all streams to close properly.
|
2011-10-04 22:08:18 +00:00
|
|
|
process.on('exit', function() {
|
2018-11-17 12:27:34 +00:00
|
|
|
assert.strictEqual(wclosed, cnt);
|
|
|
|
assert.strictEqual(rclosed, cnt);
|
2011-04-23 02:31:14 +00:00
|
|
|
});
|
|
|
|
|
2016-01-31 06:58:19 +00:00
|
|
|
for (let i = 0; i < chunkSize; i++) {
|
|
|
|
data[i] = i;
|
2011-04-23 02:31:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 06:58:19 +00:00
|
|
|
for (let i = 0; i < cnt; i++) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const 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);
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const 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);
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Now start passing through data.
|
|
|
|
// Simulate a relatively fast async stream.
|
2011-10-04 22:08:18 +00:00
|
|
|
rr.forEach(function(r) {
|
2017-01-08 13:19:00 +00:00
|
|
|
let cnt = chunks;
|
|
|
|
let paused = false;
|
2011-04-23 02:31:14 +00:00
|
|
|
|
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);
|
|
|
|
});
|