1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23// This test asserts that Stream.prototype.pipe does not leave listeners 24// hanging on the source or dest. 25require('../common'); 26const stream = require('stream'); 27const assert = require('assert'); 28 29function Writable() { 30 this.writable = true; 31 this.endCalls = 0; 32 stream.Stream.call(this); 33} 34Object.setPrototypeOf(Writable.prototype, stream.Stream.prototype); 35Object.setPrototypeOf(Writable, stream.Stream); 36Writable.prototype.end = function() { 37 this.endCalls++; 38}; 39 40Writable.prototype.destroy = function() { 41 this.endCalls++; 42}; 43 44function Readable() { 45 this.readable = true; 46 stream.Stream.call(this); 47} 48Object.setPrototypeOf(Readable.prototype, stream.Stream.prototype); 49Object.setPrototypeOf(Readable, stream.Stream); 50 51function Duplex() { 52 this.readable = true; 53 Writable.call(this); 54} 55Object.setPrototypeOf(Duplex.prototype, Writable.prototype); 56Object.setPrototypeOf(Duplex, Writable); 57 58let i = 0; 59const limit = 100; 60 61let w = new Writable(); 62 63let r; 64 65for (i = 0; i < limit; i++) { 66 r = new Readable(); 67 r.pipe(w); 68 r.emit('end'); 69} 70assert.strictEqual(r.listeners('end').length, 0); 71assert.strictEqual(w.endCalls, limit); 72 73w.endCalls = 0; 74 75for (i = 0; i < limit; i++) { 76 r = new Readable(); 77 r.pipe(w); 78 r.emit('close'); 79} 80assert.strictEqual(r.listeners('close').length, 0); 81assert.strictEqual(w.endCalls, limit); 82 83w.endCalls = 0; 84 85r = new Readable(); 86 87for (i = 0; i < limit; i++) { 88 w = new Writable(); 89 r.pipe(w); 90 w.emit('close'); 91} 92assert.strictEqual(w.listeners('close').length, 0); 93 94r = new Readable(); 95w = new Writable(); 96const d = new Duplex(); 97r.pipe(d); // pipeline A 98d.pipe(w); // pipeline B 99assert.strictEqual(r.listeners('end').length, 2); // A.onend, A.cleanup 100assert.strictEqual(r.listeners('close').length, 2); // A.onclose, A.cleanup 101assert.strictEqual(d.listeners('end').length, 2); // B.onend, B.cleanup 102// A.cleanup, B.onclose, B.cleanup 103assert.strictEqual(d.listeners('close').length, 3); 104assert.strictEqual(w.listeners('end').length, 0); 105assert.strictEqual(w.listeners('close').length, 1); // B.cleanup 106 107r.emit('end'); 108assert.strictEqual(d.endCalls, 1); 109assert.strictEqual(w.endCalls, 0); 110assert.strictEqual(r.listeners('end').length, 0); 111assert.strictEqual(r.listeners('close').length, 0); 112assert.strictEqual(d.listeners('end').length, 2); // B.onend, B.cleanup 113assert.strictEqual(d.listeners('close').length, 2); // B.onclose, B.cleanup 114assert.strictEqual(w.listeners('end').length, 0); 115assert.strictEqual(w.listeners('close').length, 1); // B.cleanup 116 117d.emit('end'); 118assert.strictEqual(d.endCalls, 1); 119assert.strictEqual(w.endCalls, 1); 120assert.strictEqual(r.listeners('end').length, 0); 121assert.strictEqual(r.listeners('close').length, 0); 122assert.strictEqual(d.listeners('end').length, 0); 123assert.strictEqual(d.listeners('close').length, 0); 124assert.strictEqual(w.listeners('end').length, 0); 125assert.strictEqual(w.listeners('close').length, 0); 126