1'use strict'; 2require('../common'); 3const assert = require('assert'); 4const { Writable } = require('stream'); 5 6// Test interaction between calling .destroy() on a writable and pending 7// writes. 8 9for (const withPendingData of [ false, true ]) { 10 for (const useEnd of [ false, true ]) { 11 const callbacks = []; 12 13 const w = new Writable({ 14 write(data, enc, cb) { 15 callbacks.push(cb); 16 }, 17 // Effectively disable the HWM to observe 'drain' events more easily. 18 highWaterMark: 1 19 }); 20 21 let chunksWritten = 0; 22 let drains = 0; 23 let finished = false; 24 w.on('drain', () => drains++); 25 w.on('finish', () => finished = true); 26 27 w.write('abc', () => chunksWritten++); 28 assert.strictEqual(chunksWritten, 0); 29 assert.strictEqual(drains, 0); 30 callbacks.shift()(); 31 assert.strictEqual(chunksWritten, 1); 32 assert.strictEqual(drains, 1); 33 34 if (withPendingData) { 35 // Test 2 cases: There either is or is not data still in the write queue. 36 // (The second write will never actually get executed either way.) 37 w.write('def', () => chunksWritten++); 38 } 39 if (useEnd) { 40 // Again, test 2 cases: Either we indicate that we want to end the 41 // writable or not. 42 w.end('ghi', () => chunksWritten++); 43 } else { 44 w.write('ghi', () => chunksWritten++); 45 } 46 47 assert.strictEqual(chunksWritten, 1); 48 w.destroy(); 49 assert.strictEqual(chunksWritten, 1); 50 callbacks.shift()(); 51 assert.strictEqual(chunksWritten, 2); 52 assert.strictEqual(callbacks.length, 0); 53 assert.strictEqual(drains, 1); 54 55 // When we used `.end()`, we see the 'finished' event if and only if 56 // we actually finished processing the write queue. 57 assert.strictEqual(finished, !withPendingData && useEnd); 58 } 59} 60