@@ -292,3 +292,98 @@ const assert = require('assert');
292292 } ) ) ;
293293 write . uncork ( ) ;
294294}
295+
296+ {
297+ // Call end(cb) after error & destroy
298+
299+ const write = new Writable ( {
300+ write ( chunk , enc , cb ) { cb ( new Error ( 'asd' ) ) ; }
301+ } ) ;
302+ write . on ( 'error' , common . mustCall ( ( ) => {
303+ write . destroy ( ) ;
304+ let ticked = false ;
305+ write . end ( common . mustCall ( ( err ) => {
306+ assert . strictEqual ( ticked , true ) ;
307+ assert . strictEqual ( err . code , 'ERR_STREAM_DESTROYED' ) ;
308+ } ) ) ;
309+ ticked = true ;
310+ } ) ) ;
311+ write . write ( 'asd' ) ;
312+ }
313+
314+ {
315+ // Call end(cb) after finish & destroy
316+
317+ const write = new Writable ( {
318+ write ( chunk , enc , cb ) { cb ( ) ; }
319+ } ) ;
320+ write . on ( 'finish' , common . mustCall ( ( ) => {
321+ write . destroy ( ) ;
322+ let ticked = false ;
323+ write . end ( common . mustCall ( ( err ) => {
324+ assert . strictEqual ( ticked , false ) ;
325+ assert . strictEqual ( err . code , 'ERR_STREAM_ALREADY_FINISHED' ) ;
326+ } ) ) ;
327+ ticked = true ;
328+ } ) ) ;
329+ write . end ( ) ;
330+ }
331+
332+ {
333+ // Call end(cb) after error & destroy and don't trigger
334+ // unhandled exception.
335+
336+ const write = new Writable ( {
337+ write ( chunk , enc , cb ) { process . nextTick ( cb ) ; }
338+ } ) ;
339+ write . once ( 'error' , common . mustCall ( ( err ) => {
340+ assert . strictEqual ( err . message , 'asd' ) ;
341+ } ) ) ;
342+ write . end ( 'asd' , common . mustCall ( ( err ) => {
343+ assert . strictEqual ( err . message , 'asd' ) ;
344+ } ) ) ;
345+ write . destroy ( new Error ( 'asd' ) ) ;
346+ }
347+
348+ {
349+ // Call buffered write callback with error
350+
351+ const write = new Writable ( {
352+ write ( chunk , enc , cb ) {
353+ process . nextTick ( cb , new Error ( 'asd' ) ) ;
354+ } ,
355+ autoDestroy : false
356+ } ) ;
357+ write . cork ( ) ;
358+ write . write ( 'asd' , common . mustCall ( ( err ) => {
359+ assert . strictEqual ( err . message , 'asd' ) ;
360+ } ) ) ;
361+ write . write ( 'asd' , common . mustCall ( ( err ) => {
362+ assert . strictEqual ( err . code , 'ERR_STREAM_DESTROYED' ) ;
363+ } ) ) ;
364+ write . on ( 'error' , common . mustCall ( ( err ) => {
365+ assert . strictEqual ( err . message , 'asd' ) ;
366+ } ) ) ;
367+ write . uncork ( ) ;
368+ }
369+
370+ {
371+ // Ensure callback order.
372+
373+ let state = 0 ;
374+ const write = new Writable ( {
375+ write ( chunk , enc , cb ) {
376+ // `setImmediate()` is used on purpose to ensure the callback is called
377+ // after `process.nextTick()` callbacks.
378+ setImmediate ( cb ) ;
379+ }
380+ } ) ;
381+ write . write ( 'asd' , common . mustCall ( ( ) => {
382+ assert . strictEqual ( state ++ , 0 ) ;
383+ } ) ) ;
384+ write . write ( 'asd' , common . mustCall ( ( err ) => {
385+ assert . strictEqual ( err . code , 'ERR_STREAM_DESTROYED' ) ;
386+ assert . strictEqual ( state ++ , 1 ) ;
387+ } ) ) ;
388+ write . destroy ( ) ;
389+ }
0 commit comments