|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | +// Regression test for https://github.com/nodejs/node/issues/12718. |
| 5 | +// Tests that piping a source stream twice to the same destination stream |
| 6 | +// works, and that a subsequent unpipe() call only removes the pipe *once*. |
| 7 | +const assert = require('assert'); |
| 8 | +const { PassThrough, Writable } = require('stream'); |
| 9 | + |
| 10 | +{ |
| 11 | + const passThrough = new PassThrough(); |
| 12 | + const dest = new Writable({ |
| 13 | + write: common.mustCall((chunk, encoding, cb) => { |
| 14 | + assert.strictEqual(`${chunk}`, 'foobar'); |
| 15 | + cb(); |
| 16 | + }) |
| 17 | + }); |
| 18 | + |
| 19 | + passThrough.pipe(dest); |
| 20 | + passThrough.pipe(dest); |
| 21 | + |
| 22 | + assert.strictEqual(passThrough._events.data.length, 2); |
| 23 | + assert.strictEqual(passThrough._readableState.pipesCount, 2); |
| 24 | + assert.strictEqual(passThrough._readableState.pipes[0], dest); |
| 25 | + assert.strictEqual(passThrough._readableState.pipes[1], dest); |
| 26 | + |
| 27 | + passThrough.unpipe(dest); |
| 28 | + |
| 29 | + assert.strictEqual(passThrough._events.data.length, 1); |
| 30 | + assert.strictEqual(passThrough._readableState.pipesCount, 1); |
| 31 | + assert.strictEqual(passThrough._readableState.pipes, dest); |
| 32 | + |
| 33 | + passThrough.write('foobar'); |
| 34 | + passThrough.pipe(dest); |
| 35 | +} |
| 36 | + |
| 37 | +{ |
| 38 | + const passThrough = new PassThrough(); |
| 39 | + const dest = new Writable({ |
| 40 | + write: common.mustCall((chunk, encoding, cb) => { |
| 41 | + assert.strictEqual(`${chunk}`, 'foobar'); |
| 42 | + cb(); |
| 43 | + }, 2) |
| 44 | + }); |
| 45 | + |
| 46 | + passThrough.pipe(dest); |
| 47 | + passThrough.pipe(dest); |
| 48 | + |
| 49 | + assert.strictEqual(passThrough._events.data.length, 2); |
| 50 | + assert.strictEqual(passThrough._readableState.pipesCount, 2); |
| 51 | + assert.strictEqual(passThrough._readableState.pipes[0], dest); |
| 52 | + assert.strictEqual(passThrough._readableState.pipes[1], dest); |
| 53 | + |
| 54 | + passThrough.write('foobar'); |
| 55 | +} |
| 56 | + |
| 57 | +{ |
| 58 | + const passThrough = new PassThrough(); |
| 59 | + const dest = new Writable({ |
| 60 | + write: common.mustNotCall() |
| 61 | + }); |
| 62 | + |
| 63 | + passThrough.pipe(dest); |
| 64 | + passThrough.pipe(dest); |
| 65 | + |
| 66 | + assert.strictEqual(passThrough._events.data.length, 2); |
| 67 | + assert.strictEqual(passThrough._readableState.pipesCount, 2); |
| 68 | + assert.strictEqual(passThrough._readableState.pipes[0], dest); |
| 69 | + assert.strictEqual(passThrough._readableState.pipes[1], dest); |
| 70 | + |
| 71 | + passThrough.unpipe(dest); |
| 72 | + passThrough.unpipe(dest); |
| 73 | + |
| 74 | + assert.strictEqual(passThrough._events.data, undefined); |
| 75 | + assert.strictEqual(passThrough._readableState.pipesCount, 0); |
| 76 | + |
| 77 | + passThrough.write('foobar'); |
| 78 | +} |
0 commit comments