• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { StreamWrap } = require('internal/js_stream_socket');
6const { Duplex } = require('stream');
7const { internalBinding } = require('internal/test/binding');
8const { ShutdownWrap } = internalBinding('stream_wrap');
9
10// This test makes sure that when a wrapped stream is waiting for
11// a "drain" event to `doShutdown`, the instance will work correctly when a
12// "drain" event emitted.
13{
14  let resolve = null;
15
16  class TestDuplex extends Duplex {
17    _write(chunk, encoding, callback) {
18      // We will resolve the write later.
19      resolve = () => {
20        callback();
21      };
22    }
23
24    _read() {}
25  }
26
27  const testDuplex = new TestDuplex();
28  const socket = new StreamWrap(testDuplex);
29
30  socket.write(
31    // Make the buffer long enough so that the `Writable` will emit "drain".
32    Buffer.allocUnsafe(socket.writableHighWaterMark * 2),
33    common.mustCall()
34  );
35
36  // Make sure that the 'drain' events will be emitted.
37  testDuplex.on('drain', common.mustCall(() => {
38    console.log('testDuplex drain');
39  }));
40
41  assert.strictEqual(typeof resolve, 'function');
42
43  const req = new ShutdownWrap();
44  req.oncomplete = common.mustCall();
45  req.handle = socket._handle;
46  // Should not throw.
47  socket._handle.shutdown(req);
48
49  resolve();
50}
51