node/test/parallel/test-net-dns-custom-lookup.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

50 lines
1.1 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var net = require('net');
var ok = false;
function check(addressType, cb) {
var server = net.createServer(function(client) {
client.end();
server.close();
cb && cb();
});
var address = addressType === 4 ? common.localhostIPv4 : '::1';
server.listen(0, address, function() {
net.connect({
port: this.address().port,
host: 'localhost',
family: addressType,
lookup: lookup
}).on('lookup', function(err, ip, type) {
assert.equal(err, null);
assert.equal(address, ip);
assert.equal(type, addressType);
ok = true;
});
});
function lookup(host, dnsopts, cb) {
dnsopts.family = addressType;
if (addressType === 4) {
process.nextTick(function() {
cb(null, common.localhostIPv4, 4);
});
} else {
process.nextTick(function() {
cb(null, '::1', 6);
});
}
}
}
check(4, function() {
common.hasIPv6 && check(6);
});
process.on('exit', function() {
assert.ok(ok);
});