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{
81cb0ef41Sopenharmony_ci  // Invoke end callback on failure.
91cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  const _err = new Error('kaboom');
121cb0ef41Sopenharmony_ci  writable._write = (chunk, encoding, cb) => {
131cb0ef41Sopenharmony_ci    process.nextTick(cb, _err);
141cb0ef41Sopenharmony_ci  };
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((err) => {
171cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
181cb0ef41Sopenharmony_ci  }));
191cb0ef41Sopenharmony_ci  writable.write('asd');
201cb0ef41Sopenharmony_ci  writable.end(common.mustCall((err) => {
211cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
221cb0ef41Sopenharmony_ci  }));
231cb0ef41Sopenharmony_ci  writable.end(common.mustCall((err) => {
241cb0ef41Sopenharmony_ci    assert.strictEqual(err, _err);
251cb0ef41Sopenharmony_ci  }));
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  // Don't invoke end callback twice
301cb0ef41Sopenharmony_ci  const writable = new stream.Writable();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  writable._write = (chunk, encoding, cb) => {
331cb0ef41Sopenharmony_ci    process.nextTick(cb);
341cb0ef41Sopenharmony_ci  };
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  let called = false;
371cb0ef41Sopenharmony_ci  writable.end('asd', common.mustCall((err) => {
381cb0ef41Sopenharmony_ci    called = true;
391cb0ef41Sopenharmony_ci    assert.strictEqual(err, undefined);
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  writable.on('error', common.mustCall((err) => {
431cb0ef41Sopenharmony_ci    assert.strictEqual(err.message, 'kaboom');
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci  writable.on('finish', common.mustCall(() => {
461cb0ef41Sopenharmony_ci    assert.strictEqual(called, true);
471cb0ef41Sopenharmony_ci    writable.emit('error', new Error('kaboom'));
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  const w = new stream.Writable({
531cb0ef41Sopenharmony_ci    write(chunk, encoding, callback) {
541cb0ef41Sopenharmony_ci      setImmediate(callback);
551cb0ef41Sopenharmony_ci    },
561cb0ef41Sopenharmony_ci    finish(callback) {
571cb0ef41Sopenharmony_ci      setImmediate(callback);
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci  w.end('testing ended state', common.mustCall((err) => {
611cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci  assert.strictEqual(w.destroyed, false);
641cb0ef41Sopenharmony_ci  assert.strictEqual(w.writableEnded, true);
651cb0ef41Sopenharmony_ci  w.end(common.mustCall((err) => {
661cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci  assert.strictEqual(w.destroyed, false);
691cb0ef41Sopenharmony_ci  assert.strictEqual(w.writableEnded, true);
701cb0ef41Sopenharmony_ci  w.end('end', common.mustCall((err) => {
711cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
721cb0ef41Sopenharmony_ci  }));
731cb0ef41Sopenharmony_ci  assert.strictEqual(w.destroyed, true);
741cb0ef41Sopenharmony_ci  w.on('error', common.mustCall((err) => {
751cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
761cb0ef41Sopenharmony_ci  }));
771cb0ef41Sopenharmony_ci  w.on('finish', common.mustNotCall());
781cb0ef41Sopenharmony_ci}
79