1// Flags: --expose-internals 2'use strict'; 3require('../common'); 4const assert = require('assert'); 5const net = require('net'); 6const { internalBinding } = require('internal/test/binding'); 7const TCPWrap = internalBinding('tcp_wrap').TCP; 8 9const echoServer = net.createServer((conn) => { 10 conn.end(); 11}); 12 13const ref = TCPWrap.prototype.ref; 14const unref = TCPWrap.prototype.unref; 15 16let refCount = 0; 17 18TCPWrap.prototype.ref = function() { 19 ref.call(this); 20 refCount++; 21 assert.strictEqual(refCount, 0); 22}; 23 24TCPWrap.prototype.unref = function() { 25 unref.call(this); 26 refCount--; 27 assert.strictEqual(refCount, -1); 28}; 29 30echoServer.listen(0); 31 32echoServer.on('listening', function() { 33 const sock = new net.Socket(); 34 sock.unref(); 35 sock.ref(); 36 sock.connect(this.address().port); 37 sock.on('end', () => { 38 assert.strictEqual(refCount, 0); 39 echoServer.close(); 40 }); 41}); 42