11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst { Writable } = require('stream'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Ensure callback is always invoked before 71cb0ef41Sopenharmony_ci// error is emitted. Regardless if error was 81cb0ef41Sopenharmony_ci// sync or async. 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci{ 111cb0ef41Sopenharmony_ci let callbackCalled = false; 121cb0ef41Sopenharmony_ci // Sync Error 131cb0ef41Sopenharmony_ci const writable = new Writable({ 141cb0ef41Sopenharmony_ci write: common.mustCall((buf, enc, cb) => { 151cb0ef41Sopenharmony_ci cb(new Error()); 161cb0ef41Sopenharmony_ci }) 171cb0ef41Sopenharmony_ci }); 181cb0ef41Sopenharmony_ci writable.on('error', common.mustCall(() => { 191cb0ef41Sopenharmony_ci assert.strictEqual(callbackCalled, true); 201cb0ef41Sopenharmony_ci })); 211cb0ef41Sopenharmony_ci writable.write('hi', common.mustCall(() => { 221cb0ef41Sopenharmony_ci callbackCalled = true; 231cb0ef41Sopenharmony_ci })); 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci{ 271cb0ef41Sopenharmony_ci let callbackCalled = false; 281cb0ef41Sopenharmony_ci // Async Error 291cb0ef41Sopenharmony_ci const writable = new Writable({ 301cb0ef41Sopenharmony_ci write: common.mustCall((buf, enc, cb) => { 311cb0ef41Sopenharmony_ci process.nextTick(cb, new Error()); 321cb0ef41Sopenharmony_ci }) 331cb0ef41Sopenharmony_ci }); 341cb0ef41Sopenharmony_ci writable.on('error', common.mustCall(() => { 351cb0ef41Sopenharmony_ci assert.strictEqual(callbackCalled, true); 361cb0ef41Sopenharmony_ci })); 371cb0ef41Sopenharmony_ci writable.write('hi', common.mustCall(() => { 381cb0ef41Sopenharmony_ci callbackCalled = true; 391cb0ef41Sopenharmony_ci })); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci{ 431cb0ef41Sopenharmony_ci // Sync Error 441cb0ef41Sopenharmony_ci const writable = new Writable({ 451cb0ef41Sopenharmony_ci write: common.mustCall((buf, enc, cb) => { 461cb0ef41Sopenharmony_ci cb(new Error()); 471cb0ef41Sopenharmony_ci }) 481cb0ef41Sopenharmony_ci }); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci writable.on('error', common.mustCall()); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci let cnt = 0; 531cb0ef41Sopenharmony_ci // Ensure we don't live lock on sync error 541cb0ef41Sopenharmony_ci while (writable.write('a')) 551cb0ef41Sopenharmony_ci cnt++; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci assert.strictEqual(cnt, 0); 581cb0ef41Sopenharmony_ci} 59