node/test/parallel/test-net-socket-local-address.js
Sam Roberts 58e177cde1
doc: document and test that methods return this
Also, add tests to ensure they will always return this, and to confirm
they return this when these doc changes are back-ported to earlier
release lines.

PR-URL: https://github.com/nodejs/node/pull/13531
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-06-21 22:42:59 +02:00

44 lines
1001 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
// skip test in FreeBSD jails
if (common.inFreeBSDJail) {
common.skip('In a FreeBSD jail');
return;
}
let conns = 0;
const clientLocalPorts = [];
const serverRemotePorts = [];
const client = new net.Socket();
const server = net.createServer((socket) => {
serverRemotePorts.push(socket.remotePort);
socket.end();
});
server.on('close', common.mustCall(() => {
assert.deepStrictEqual(clientLocalPorts, serverRemotePorts,
'client and server should agree on the ports used');
assert.strictEqual(2, conns);
}));
server.listen(0, common.localhostIPv4, connect);
function connect() {
if (conns === 2) {
server.close();
return;
}
conns++;
client.once('close', connect);
assert.strictEqual(
client,
client.connect(server.address().port, common.localhostIPv4, () => {
clientLocalPorts.push(client.localPort);
})
);
}