Skip to content
Permalink
Browse files

test: add more and refactor test cases to net.connect

PR-URL: #11847
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
joyeecheung authored and jasnell committed Mar 13, 2017
1 parent db39273 commit 7b830f4e4acdfd469e4a62ed63039132ffa99032
@@ -0,0 +1,124 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

function testClients(getSocketOpt, getConnectOpt, getConnectCb) {
const cloneOptions = (index) =>
Object.assign({}, getSocketOpt(index), getConnectOpt(index));
return [
net.connect(cloneOptions(0), getConnectCb(0)),
net.connect(cloneOptions(1))
.on('connect', getConnectCb(1)),
net.createConnection(cloneOptions(2), getConnectCb(2)),
net.createConnection(cloneOptions(3))
.on('connect', getConnectCb(3)),
new net.Socket(getSocketOpt(4)).connect(getConnectOpt(4), getConnectCb(4)),
new net.Socket(getSocketOpt(5)).connect(getConnectOpt(5))
.on('connect', getConnectCb(5))
];
}

const CLIENT_VARIANTS = 6; // Same length as array above
const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);

// Test allowHalfOpen
{
let clientReceivedFIN = 0;
let serverConnections = 0;
let clientSentFIN = 0;
let serverReceivedFIN = 0;
const server = net.createServer({
allowHalfOpen: true
})
.on('connection', forAllClients(function serverOnConnection(socket) {
const serverConnection = ++serverConnections;
let clientId;
console.error(`${serverConnections} 'connection' emitted on server`);
socket.resume();
// 'end' on each socket must not be emitted twice
socket.on('data', common.mustCall(function(data) {
clientId = data.toString();
console.error(`${serverConnection} server connection is started ` +
`by client No. ${clientId}`);
}));
socket.on('end', common.mustCall(function() {
serverReceivedFIN++;
console.error(`Server recieved FIN sent by No. ${clientId}`);
if (serverReceivedFIN === CLIENT_VARIANTS) {
setTimeout(() => {
server.close();
console.error(`No. ${clientId} connection is closing server: ` +
`${serverReceivedFIN} FIN received by server, ` +
`${clientReceivedFIN} FIN received by client, ` +
`${clientSentFIN} FIN sent by client, ` +
`${serverConnections} FIN sent by server`);
}, 50);
}
}, 1));
socket.end();
console.error(`Server has sent ${serverConnections} FIN`);
}))
.on('close', common.mustCall(function serverOnClose() {
console.error('Server has been closed: ' +
`${serverReceivedFIN} FIN received by server, ` +
`${clientReceivedFIN} FIN received by client, ` +
`${clientSentFIN} FIN sent by client, ` +
`${serverConnections} FIN sent by server`);
}))
.listen(0, 'localhost', common.mustCall(function serverOnListen() {
const host = 'localhost';
const port = server.address().port;

console.error(`Server starts at ${host}:${port}`);
const getSocketOpt = () => ({ allowHalfOpen: true });
const getConnectOpt = () => ({ host, port });
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
const client = this;
console.error(`'connect' emitted on Client ${index}`);
client.resume();
client.on('end', common.mustCall(function clientOnEnd() {
setTimeout(function() {
// when allowHalfOpen is true, client must still be writable
// after the server closes the connections, but not readable
console.error(`No. ${index} client received FIN`);
assert(!client.readable);
assert(client.writable);
assert(client.write(index + ''));
client.end();
clientSentFIN++;
console.error(`No. ${index} client sent FIN, ` +
`${clientSentFIN} have been sent`);
}, 50);
}));
client.on('close', common.mustCall(function clientOnClose() {
clientReceivedFIN++;
console.error(`No. ${index} connection has been closed by both ` +
`sides, ${clientReceivedFIN} clients have closed`);
}));
});

testClients(getSocketOpt, getConnectOpt, getConnectCb);
}));
}
@@ -0,0 +1,100 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const Pipe = process.binding('pipe_wrap').Pipe;

if (common.isWindows) {
common.skip('Does not support wrapping sockets with fd on Windows');
return;
}

common.refreshTmpDir();

function testClients(getSocketOpt, getConnectOpt, getConnectCb) {
const cloneOptions = (index) =>
Object.assign({}, getSocketOpt(index), getConnectOpt(index));
return [
net.connect(cloneOptions(0), getConnectCb(0)),
net.connect(cloneOptions(1))
.on('connect', getConnectCb(1)),
net.createConnection(cloneOptions(2), getConnectCb(2)),
net.createConnection(cloneOptions(3))
.on('connect', getConnectCb(3)),
new net.Socket(getSocketOpt(4)).connect(getConnectOpt(4), getConnectCb(4)),
new net.Socket(getSocketOpt(5)).connect(getConnectOpt(5))
.on('connect', getConnectCb(5))
];
}

const CLIENT_VARIANTS = 6; // Same length as array above
const forAllClients = (cb) => common.mustCall(cb, CLIENT_VARIANTS);

// Test Pipe fd is wrapped correctly
{
const prefix = `${common.PIPE}-net-connect-options-fd`;
const serverPath = `${prefix}-server`;
let counter = 0;
let socketCounter = 0;
const handleMap = new Map();
const server = net.createServer()
.on('connection', forAllClients(function serverOnConnection(socket) {
let clientFd;
socket.on('data', common.mustCall(function(data) {
clientFd = data.toString();
console.error(`[Pipe]Received data from fd ${clientFd}`);
socket.end();
}));
socket.on('end', common.mustCall(function() {
counter++;
console.error(`[Pipe]Received end from fd ${clientFd}, total ${counter}`);
if (counter === CLIENT_VARIANTS) {
setTimeout(() => {
console.error(`[Pipe]Server closed by fd ${clientFd}`);
server.close();
}, 10);
}
}, 1));
}))
.on('close', function() {
setTimeout(() => {
for (const pair of handleMap) {
console.error(`[Pipe]Clean up handle with fd ${pair[1].fd}`);
pair[1].close(); // clean up handles
}
}, 10);
})
.on('error', function(err) {
console.error(err);
assert.fail(null, null, '[Pipe server]' + err);
})
.listen({path: serverPath}, common.mustCall(function serverOnListen() {
const getSocketOpt = (index) => {
const handle = new Pipe();
const err = handle.bind(`${prefix}-client-${socketCounter++}`);
assert(err >= 0, '' + err);
assert.notStrictEqual(handle.fd, -1);
handleMap.set(index, handle);
console.error(`[Pipe]Bound handle with Pipe ${handle.fd}`);
return { fd: handle.fd, readable: true, writable: true };
};
const getConnectOpt = () => ({
path: serverPath
});
const getConnectCb = (index) => common.mustCall(function clientOnConnect() {
const client = this;
// Test if it's wrapping an existing fd
assert(handleMap.has(index));
const oldHandle = handleMap.get(index);
assert.strictEqual(oldHandle.fd, this._handle.fd);
client.write(oldHandle.fd + '');
console.error(`[Pipe]Sending data through fd ${oldHandle.fd}`);
client.on('error', function(err) {
console.error(err);
assert.fail(null, null, '[Pipe Client]' + err);
});
});

testClients(getSocketOpt, getConnectOpt, getConnectCb);
}));
}
@@ -0,0 +1,53 @@
'use strict';
const common = require('../common');
const net = require('net');

// This file tests the option handling of net.connect,
// net.createConnect, and new Socket().connect

common.refreshTmpDir();

const CLIENT_VARIANTS = 12;

// Test connect(path)
{
const prefix = `${common.PIPE}-net-connect-options-path`;
const serverPath = `${prefix}-server`;
let counter = 0;
const server = net.createServer()
.on('connection', common.mustCall(function(socket) {
socket.end('ok');
}, CLIENT_VARIANTS))
.listen(serverPath, common.mustCall(function() {
const getConnectCb = () => common.mustCall(function() {
const client = this;
client.end();
client.on('close', common.mustCall(function() {
counter++;
if (counter === CLIENT_VARIANTS) {
server.close();
}
}));
});

// CLIENT_VARIANTS depends on the following code
net.connect(serverPath, getConnectCb());
net.connect(serverPath)
.on('connect', getConnectCb());
net.createConnection(serverPath, getConnectCb());
net.createConnection(serverPath)
.on('connect', getConnectCb());
new net.Socket().connect(serverPath, getConnectCb());
new net.Socket().connect(serverPath)
.on('connect', getConnectCb());
net.connect({path: serverPath}, getConnectCb());
net.connect({path: serverPath})
.on('connect', getConnectCb());
net.createConnection({path: serverPath}, getConnectCb());
net.createConnection({path: serverPath})
.on('connect', getConnectCb());
new net.Socket().connect({path: serverPath}, getConnectCb());
new net.Socket().connect({path: serverPath})
.on('connect', getConnectCb());
}));
}

0 comments on commit 7b830f4

Please sign in to comment.
You can’t perform that action at this time.