From 33918ed5a0cd2029c9dbfef00659b8c43787c4c9 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Fri, 5 May 2017 14:49:45 +0200 Subject: [PATCH] test: test net.connect monkey patching This is important for people who monkey-patches `Socket.prototype.connect` since it's not possible to monkey-patch `net.connect` directly (as the core `connect` function is called internally in Node instead of calling the `exports.connect` function). Monkey-patching of `Socket.prototype.connect` is done by - among others - most APM vendors, the async-listener module and the continuation-local-storage module. --- .../test-net-connect-call-socket-connect.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/parallel/test-net-connect-call-socket-connect.js diff --git a/test/parallel/test-net-connect-call-socket-connect.js b/test/parallel/test-net-connect-call-socket-connect.js new file mode 100644 index 00000000000000..4c8ac94efcc3a6 --- /dev/null +++ b/test/parallel/test-net-connect-call-socket-connect.js @@ -0,0 +1,37 @@ +'use strict'; + +// This test checks that calling `net.connect` internally calls +// `Socket.prototype.connect`. +// +// This is important for people who monkey-patch `Socket.prototype.connect` +// since it's not possible to monkey-patch `net.connect` directly (as the core +// `connect` function is called internally in Node instead of calling the +// `exports.connect` function). +// +// Monkey-patching of `Socket.prototype.connect` is done by - among others - +// most APM vendors, the async-listener module and the +// continuation-local-storage module. +// +// See https://github.com/nodejs/node/pull/12852 for details. + +const common = require('../common'); +const net = require('net'); +const Socket = net.Socket; + +// monkey patch Socket.prototype.connect to check that it's called +const orig = Socket.prototype.connect; +Socket.prototype.connect = common.mustCall(function() { + return orig.apply(this, arguments); +}); + +const server = net.createServer(); + +server.listen(common.mustCall(function() { + const port = server.address().port; + const client = net.connect({port}, common.mustCall(function() { + client.end(); + })); + client.on('end', common.mustCall(function() { + server.close(); + })); +}));