Skip to content

Commit

Permalink
test: test net.connect monkey patching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
watson authored and cjihrig committed May 5, 2017
1 parent 44de1fc commit 33918ed
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/parallel/test-net-connect-call-socket-connect.js
Original file line number Diff line number Diff line change
@@ -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();
}));
}));

0 comments on commit 33918ed

Please sign in to comment.