11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { StreamWrap } = require('internal/js_stream_socket');
61cb0ef41Sopenharmony_ciconst { Duplex } = require('stream');
71cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
81cb0ef41Sopenharmony_ciconst { ShutdownWrap } = internalBinding('stream_wrap');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// This test makes sure that when a wrapped stream is waiting for
111cb0ef41Sopenharmony_ci// a "drain" event to `doShutdown`, the instance will work correctly when a
121cb0ef41Sopenharmony_ci// "drain" event emitted.
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  let resolve = null;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  class TestDuplex extends Duplex {
171cb0ef41Sopenharmony_ci    _write(chunk, encoding, callback) {
181cb0ef41Sopenharmony_ci      // We will resolve the write later.
191cb0ef41Sopenharmony_ci      resolve = () => {
201cb0ef41Sopenharmony_ci        callback();
211cb0ef41Sopenharmony_ci      };
221cb0ef41Sopenharmony_ci    }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    _read() {}
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  const testDuplex = new TestDuplex();
281cb0ef41Sopenharmony_ci  const socket = new StreamWrap(testDuplex);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  socket.write(
311cb0ef41Sopenharmony_ci    // Make the buffer long enough so that the `Writable` will emit "drain".
321cb0ef41Sopenharmony_ci    Buffer.allocUnsafe(socket.writableHighWaterMark * 2),
331cb0ef41Sopenharmony_ci    common.mustCall()
341cb0ef41Sopenharmony_ci  );
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Make sure that the 'drain' events will be emitted.
371cb0ef41Sopenharmony_ci  testDuplex.on('drain', common.mustCall(() => {
381cb0ef41Sopenharmony_ci    console.log('testDuplex drain');
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  assert.strictEqual(typeof resolve, 'function');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const req = new ShutdownWrap();
441cb0ef41Sopenharmony_ci  req.oncomplete = common.mustCall();
451cb0ef41Sopenharmony_ci  req.handle = socket._handle;
461cb0ef41Sopenharmony_ci  // Should not throw.
471cb0ef41Sopenharmony_ci  socket._handle.shutdown(req);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  resolve();
501cb0ef41Sopenharmony_ci}
51