11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  TCP,
81cb0ef41Sopenharmony_ci  constants: TCPConstants,
91cb0ef41Sopenharmony_ci  TCPConnectWrap
101cb0ef41Sopenharmony_ci} = internalBinding('tcp_wrap');
111cb0ef41Sopenharmony_ciconst { ShutdownWrap } = internalBinding('stream_wrap');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction makeConnection() {
141cb0ef41Sopenharmony_ci  const client = new TCP(TCPConstants.SOCKET);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  const req = new TCPConnectWrap();
171cb0ef41Sopenharmony_ci  const err = client.connect(req, '127.0.0.1', this.address().port);
181cb0ef41Sopenharmony_ci  assert.strictEqual(err, 0);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  req.oncomplete = function(status, client_, req_, readable, writable) {
211cb0ef41Sopenharmony_ci    assert.strictEqual(status, 0);
221cb0ef41Sopenharmony_ci    assert.strictEqual(client_, client);
231cb0ef41Sopenharmony_ci    assert.strictEqual(req_, req);
241cb0ef41Sopenharmony_ci    assert.strictEqual(readable, true);
251cb0ef41Sopenharmony_ci    assert.strictEqual(writable, true);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    const shutdownReq = new ShutdownWrap();
281cb0ef41Sopenharmony_ci    const err = client.shutdown(shutdownReq);
291cb0ef41Sopenharmony_ci    assert.strictEqual(err, 0);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    shutdownReq.oncomplete = function(status, client_, error) {
321cb0ef41Sopenharmony_ci      assert.strictEqual(status, 0);
331cb0ef41Sopenharmony_ci      assert.strictEqual(client_, client);
341cb0ef41Sopenharmony_ci      assert.strictEqual(error, undefined);
351cb0ef41Sopenharmony_ci      shutdownCount++;
361cb0ef41Sopenharmony_ci      client.close();
371cb0ef41Sopenharmony_ci    };
381cb0ef41Sopenharmony_ci  };
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cilet connectCount = 0;
421cb0ef41Sopenharmony_cilet endCount = 0;
431cb0ef41Sopenharmony_cilet shutdownCount = 0;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst server = require('net').Server(function(s) {
461cb0ef41Sopenharmony_ci  connectCount++;
471cb0ef41Sopenharmony_ci  s.resume();
481cb0ef41Sopenharmony_ci  s.on('end', function() {
491cb0ef41Sopenharmony_ci    endCount++;
501cb0ef41Sopenharmony_ci    s.destroy();
511cb0ef41Sopenharmony_ci    server.close();
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci});
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciserver.listen(0, makeConnection);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciprocess.on('exit', function() {
581cb0ef41Sopenharmony_ci  assert.strictEqual(shutdownCount, 1);
591cb0ef41Sopenharmony_ci  assert.strictEqual(connectCount, 1);
601cb0ef41Sopenharmony_ci  assert.strictEqual(endCount, 1);
611cb0ef41Sopenharmony_ci});
62