Skip to content

Commit

Permalink
test: refactor net.connect ipv6 options test
Browse files Browse the repository at this point in the history
Use common.skipIfNoIpv6Localhost
  • Loading branch information
joyeecheung committed Nov 12, 2017
1 parent c3c6540 commit 8097bd8
Showing 1 changed file with 34 additions and 61 deletions.
95 changes: 34 additions & 61 deletions test/parallel/test-net-connect-options-ipv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,44 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// This tests that the family option of net.connect is hornored.

'use strict';
const common = require('../common');
if (!common.hasIPv6)
common.skip('no IPv6 support');

const assert = require('assert');
const net = require('net');

const hosts = common.localIPv6Hosts;
let hostIdx = 0;
let host = hosts[hostIdx];
let localhostTries = 10;
common.skipIfNoIpv6Localhost((ipv6Host) => {
const assert = require('assert');
const net = require('net');

const server = net.createServer({ allowHalfOpen: true }, function(socket) {
socket.resume();
socket.on('end', common.mustCall());
socket.end();
});
const server = net.createServer({
allowHalfOpen: true
}, common.mustCall((socket) => {
assert.strictEqual('::1', socket.remoteAddress);
socket.resume();
socket.on('end', common.mustCall());
socket.end();
}));

server.listen(0, '::1', tryConnect);
server.listen(0, '::1', common.mustCall(tryConnect));

function tryConnect() {
const client = net.connect({
host: host,
port: server.address().port,
family: 6,
allowHalfOpen: true
}, function() {
console.error('client connect cb');
client.resume();
client.on('end', common.mustCall(function() {
setTimeout(function() {
assert(client.writable);
client.end();
}, 10);
}));
client.on('close', function() {
server.close();
});
}).on('error', function(err) {
// ENOTFOUND means we don't have the requested address. In this
// case we try the next one in the list and if we run out of
// candidates we assume IPv6 is not supported on the
// machine and skip the test.
// EAI_AGAIN means we tried to remotely resolve the address and
// timed out or hit some intermittent connectivity issue with the
// dns server. Although we are looking for local loopback addresses
// we may go remote since the list we search includes addresses that
// cover more than is available on any one distribution. The
// net is that if we get an EAI_AGAIN we were looking for an
// address which does not exist in this distribution so the error
// is not significant and we should just move on and try the
// next address in the list.
if ((err.syscall === 'getaddrinfo') && ((err.code === 'ENOTFOUND') ||
(err.code === 'EAI_AGAIN'))) {
if (host !== 'localhost' || --localhostTries === 0)
host = hosts[++hostIdx];
if (host)
tryConnect();
else {
function tryConnect() {
const client = net.connect({
host: ipv6Host,
port: server.address().port,
family: 6,
allowHalfOpen: true
}, common.mustCall(() => {
console.error('client connect cb');
client.resume();
client.on('end', common.mustCall(function() {
setTimeout(function() {
assert(client.writable);
client.end();
}, 10);
}));
client.on('close', function() {
server.close();
common.skip('no IPv6 localhost support');
}
return;
}
throw err;
});
}
});
}));
}
});

0 comments on commit 8097bd8

Please sign in to comment.