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 w.on('drain', () => drains++); 24 25 function onWrite(err) { 26 if (err) { 27 assert.strictEqual(w.destroyed, true); 28 assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED'); 29 } else { 30 chunksWritten++; 31 } 32 } 33 34 w.write('abc', onWrite); 35 assert.strictEqual(chunksWritten, 0); 36 assert.strictEqual(drains, 0); 37 callbacks.shift()(); 38 assert.strictEqual(chunksWritten, 1); 39 assert.strictEqual(drains, 1); 40 41 if (withPendingData) { 42 // Test 2 cases: There either is or is not data still in the write queue. 43 // (The second write will never actually get executed either way.) 44 w.write('def', onWrite); 45 } 46 if (useEnd) { 47 // Again, test 2 cases: Either we indicate that we want to end the 48 // writable or not. 49 w.end('ghi', onWrite); 50 } else { 51 w.write('ghi', onWrite); 52 } 53 54 assert.strictEqual(chunksWritten, 1); 55 w.destroy(); 56 assert.strictEqual(chunksWritten, 1); 57 callbacks.shift()(); 58 assert.strictEqual(chunksWritten, useEnd && !withPendingData ? 1 : 2); 59 assert.strictEqual(callbacks.length, 0); 60 assert.strictEqual(drains, 1); 61 } 62} 63