11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst net = require('net');
61cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
71cb0ef41Sopenharmony_ciconst TCPWrap = internalBinding('tcp_wrap').TCP;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst echoServer = net.createServer((conn) => {
101cb0ef41Sopenharmony_ci  conn.end();
111cb0ef41Sopenharmony_ci});
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst ref = TCPWrap.prototype.ref;
141cb0ef41Sopenharmony_ciconst unref = TCPWrap.prototype.unref;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet refCount = 0;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciTCPWrap.prototype.ref = function() {
191cb0ef41Sopenharmony_ci  ref.call(this);
201cb0ef41Sopenharmony_ci  refCount++;
211cb0ef41Sopenharmony_ci  assert.strictEqual(refCount, 0);
221cb0ef41Sopenharmony_ci};
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciTCPWrap.prototype.unref = function() {
251cb0ef41Sopenharmony_ci  unref.call(this);
261cb0ef41Sopenharmony_ci  refCount--;
271cb0ef41Sopenharmony_ci  assert.strictEqual(refCount, -1);
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciechoServer.listen(0);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciechoServer.on('listening', function() {
331cb0ef41Sopenharmony_ci  const sock = new net.Socket();
341cb0ef41Sopenharmony_ci  sock.unref();
351cb0ef41Sopenharmony_ci  sock.ref();
361cb0ef41Sopenharmony_ci  sock.connect(this.address().port);
371cb0ef41Sopenharmony_ci  sock.on('end', () => {
381cb0ef41Sopenharmony_ci    assert.strictEqual(refCount, 0);
391cb0ef41Sopenharmony_ci    echoServer.close();
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci});
42