11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst stream = require('stream');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Ensure consistency between the finish event when using cork()
81cb0ef41Sopenharmony_ci// and writev and when not using them
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  writable._write = (chunks, encoding, cb) => {
141cb0ef41Sopenharmony_ci    cb(new Error('write test error'));
151cb0ef41Sopenharmony_ci  };
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  writable.on('finish', common.mustNotCall());
181cb0ef41Sopenharmony_ci  writable.on('prefinish', common.mustNotCall());
191cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((er) => {
201cb0ef41Sopenharmony_ci    assert.strictEqual(er.message, 'write test error');
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  writable.end('test');
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci{
271cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  writable._write = (chunks, encoding, cb) => {
301cb0ef41Sopenharmony_ci    setImmediate(cb, new Error('write test error'));
311cb0ef41Sopenharmony_ci  };
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  writable.on('finish', common.mustNotCall());
341cb0ef41Sopenharmony_ci  writable.on('prefinish', common.mustNotCall());
351cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((er) => {
361cb0ef41Sopenharmony_ci    assert.strictEqual(er.message, 'write test error');
371cb0ef41Sopenharmony_ci  }));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  writable.end('test');
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  writable._write = (chunks, encoding, cb) => {
461cb0ef41Sopenharmony_ci    cb(new Error('write test error'));
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  writable._writev = (chunks, cb) => {
501cb0ef41Sopenharmony_ci    cb(new Error('writev test error'));
511cb0ef41Sopenharmony_ci  };
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  writable.on('finish', common.mustNotCall());
541cb0ef41Sopenharmony_ci  writable.on('prefinish', common.mustNotCall());
551cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((er) => {
561cb0ef41Sopenharmony_ci    assert.strictEqual(er.message, 'writev test error');
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  writable.cork();
601cb0ef41Sopenharmony_ci  writable.write('test');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  setImmediate(function() {
631cb0ef41Sopenharmony_ci    writable.end('test');
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci{
681cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  writable._write = (chunks, encoding, cb) => {
711cb0ef41Sopenharmony_ci    setImmediate(cb, new Error('write test error'));
721cb0ef41Sopenharmony_ci  };
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  writable._writev = (chunks, cb) => {
751cb0ef41Sopenharmony_ci    setImmediate(cb, new Error('writev test error'));
761cb0ef41Sopenharmony_ci  };
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  writable.on('finish', common.mustNotCall());
791cb0ef41Sopenharmony_ci  writable.on('prefinish', common.mustNotCall());
801cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((er) => {
811cb0ef41Sopenharmony_ci    assert.strictEqual(er.message, 'writev test error');
821cb0ef41Sopenharmony_ci  }));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  writable.cork();
851cb0ef41Sopenharmony_ci  writable.write('test');
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  setImmediate(function() {
881cb0ef41Sopenharmony_ci    writable.end('test');
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci// Regression test for
931cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/13812
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci{
961cb0ef41Sopenharmony_ci  const rs = new stream.Readable();
971cb0ef41Sopenharmony_ci  rs.push('ok');
981cb0ef41Sopenharmony_ci  rs.push(null);
991cb0ef41Sopenharmony_ci  rs._read = () => {};
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  const ws = new stream.Writable();
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  ws.on('finish', common.mustNotCall());
1041cb0ef41Sopenharmony_ci  ws.on('error', common.mustCall());
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  ws._write = (chunk, encoding, done) => {
1071cb0ef41Sopenharmony_ci    setImmediate(done, new Error());
1081cb0ef41Sopenharmony_ci  };
1091cb0ef41Sopenharmony_ci  rs.pipe(ws);
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci{
1131cb0ef41Sopenharmony_ci  const rs = new stream.Readable();
1141cb0ef41Sopenharmony_ci  rs.push('ok');
1151cb0ef41Sopenharmony_ci  rs.push(null);
1161cb0ef41Sopenharmony_ci  rs._read = () => {};
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  const ws = new stream.Writable();
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  ws.on('finish', common.mustNotCall());
1211cb0ef41Sopenharmony_ci  ws.on('error', common.mustCall());
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  ws._write = (chunk, encoding, done) => {
1241cb0ef41Sopenharmony_ci    done(new Error());
1251cb0ef41Sopenharmony_ci  };
1261cb0ef41Sopenharmony_ci  rs.pipe(ws);
1271cb0ef41Sopenharmony_ci}
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci{
1301cb0ef41Sopenharmony_ci  const w = new stream.Writable();
1311cb0ef41Sopenharmony_ci  w._write = (chunk, encoding, cb) => {
1321cb0ef41Sopenharmony_ci    process.nextTick(cb);
1331cb0ef41Sopenharmony_ci  };
1341cb0ef41Sopenharmony_ci  w.on('error', common.mustCall());
1351cb0ef41Sopenharmony_ci  w.on('finish', common.mustNotCall());
1361cb0ef41Sopenharmony_ci  w.on('prefinish', () => {
1371cb0ef41Sopenharmony_ci    w.write("shouldn't write in prefinish listener");
1381cb0ef41Sopenharmony_ci  });
1391cb0ef41Sopenharmony_ci  w.end();
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  const w = new stream.Writable();
1441cb0ef41Sopenharmony_ci  w._write = (chunk, encoding, cb) => {
1451cb0ef41Sopenharmony_ci    process.nextTick(cb);
1461cb0ef41Sopenharmony_ci  };
1471cb0ef41Sopenharmony_ci  w.on('error', common.mustCall());
1481cb0ef41Sopenharmony_ci  w.on('finish', () => {
1491cb0ef41Sopenharmony_ci    w.write("shouldn't write in finish listener");
1501cb0ef41Sopenharmony_ci  });
1511cb0ef41Sopenharmony_ci  w.end();
1521cb0ef41Sopenharmony_ci}
153