Skip to content

Commit

Permalink
test: refactor the code in test-dns-ipv6
Browse files Browse the repository at this point in the history
* remove the manual control for functions execution
* use common.mustCall to control the functions execution automatically
* use let and const instead of var
* use assert.strictEqual instead assert.equal

PR-URL: #10219
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
edsadr authored and italoacasas committed Dec 19, 2016
1 parent 8b367c5 commit 5e781a3
Showing 1 changed file with 61 additions and 68 deletions.
129 changes: 61 additions & 68 deletions test/internet/test-dns-ipv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const dns = require('dns');
const net = require('net');
const isIPv6 = net.isIPv6;

let expected = 0;
let completed = 0;
let running = false;
const queue = [];

Expand All @@ -17,7 +15,7 @@ if (!common.hasIPv6) {

function TEST(f) {
function next() {
var f = queue.shift();
const f = queue.shift();
if (f) {
running = true;
console.log(f.name);
Expand All @@ -27,11 +25,9 @@ function TEST(f) {

function done() {
running = false;
completed++;
process.nextTick(next);
}

expected++;
queue.push(f);

if (!running) {
Expand All @@ -44,46 +40,46 @@ function checkWrap(req) {
}

TEST(function test_resolve6(done) {
var req = dns.resolve6('ipv6.google.com', function(err, ips) {
if (err) throw err;
const req = dns.resolve6('ipv6.google.com',
common.mustCall((err, ips) => {
assert.ifError(err);

assert.ok(ips.length > 0);
assert.ok(ips.length > 0);

for (var i = 0; i < ips.length; i++) {
assert.ok(isIPv6(ips[i]));
}
for (let i = 0; i < ips.length; i++)
assert.ok(isIPv6(ips[i]));

done();
});
done();
}));

checkWrap(req);
});

TEST(function test_reverse_ipv6(done) {
var req = dns.reverse('2001:4860:4860::8888', function(err, domains) {
if (err) throw err;
const req = dns.reverse('2001:4860:4860::8888',
common.mustCall((err, domains) => {
assert.ifError(err);

assert.ok(domains.length > 0);
assert.ok(domains.length > 0);

for (var i = 0; i < domains.length; i++) {
assert.ok(domains[i]);
assert.ok(typeof domains[i] === 'string');
}
for (let i = 0; i < domains.length; i++)
assert.ok(typeof domains[i] === 'string');

done();
});
done();
}));

checkWrap(req);
});

TEST(function test_lookup_ipv6_explicit(done) {
var req = dns.lookup('ipv6.google.com', 6, function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv6(ip));
assert.strictEqual(family, 6);
const req = dns.lookup('ipv6.google.com', 6,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

done();
});
done();
}));

checkWrap(req);
});
Expand All @@ -103,24 +99,24 @@ TEST(function test_lookup_ipv6_implicit(done) {
*/

TEST(function test_lookup_ipv6_explicit_object(done) {
var req = dns.lookup('ipv6.google.com', {
const req = dns.lookup('ipv6.google.com', {
family: 6
}, function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv6(ip));
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

done();
});
}));

checkWrap(req);
});

TEST(function test_lookup_ipv6_hint(done) {
var req = dns.lookup('www.google.com', {
const req = dns.lookup('www.google.com', {
family: 6,
hints: dns.V4MAPPED
}, function(err, ip, family) {
}, common.mustCall((err, ip, family) => {
if (err) {
// FreeBSD does not support V4MAPPED
if (common.isFreeBSD) {
Expand All @@ -130,66 +126,69 @@ TEST(function test_lookup_ipv6_hint(done) {
assert.ok(/getaddrinfo EAI_BADFLAGS/.test(err.message));
done();
return;
} else {
throw err;
}

assert.ifError(err);
}
assert.ok(net.isIPv6(ip));

assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

done();
});
}));

checkWrap(req);
});

TEST(function test_lookup_ip_ipv6(done) {
var req = dns.lookup('::1', function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv6(ip));
assert.strictEqual(family, 6);
const req = dns.lookup('::1',
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

done();
});
done();
}));

checkWrap(req);
});

TEST(function test_lookup_all_ipv6(done) {
var req = dns.lookup(
const req = dns.lookup(
'www.google.com',
{all: true, family: 6},
function(err, ips) {
if (err) throw err;
common.mustCall((err, ips) => {
assert.ifError(err);
assert.ok(Array.isArray(ips));
assert.ok(ips.length > 0);

ips.forEach(function(ip) {
ips.forEach((ip) => {
assert.ok(isIPv6(ip.address),
'Invalid IPv6: ' + ip.address.toString());
assert.strictEqual(ip.family, 6);
});

done();
}
);
));

checkWrap(req);
});

TEST(function test_lookupservice_ip_ipv6(done) {
var req = dns.lookupService('::1', 80, function(err, host, service) {
if (err) {
// Not skipping the test, rather checking an alternative result,
// i.e. that ::1 may not be configured (e.g. in /etc/hosts)
assert.strictEqual(err.code, 'ENOTFOUND');
return done();
}
assert.equal(typeof host, 'string');
assert(host);
assert(['http', 'www', '80'].includes(service));
done();
});
const req = dns.lookupService('::1', 80,
common.mustCall((err, host, service) => {
if (err) {
// Not skipping the test, rather checking an alternative result,
// i.e. that ::1 may not be configured (e.g. in /etc/hosts)
assert.strictEqual(err.code, 'ENOTFOUND');
return done();
}
assert.strictEqual(typeof host, 'string');
assert(host);
assert(['http', 'www', '80'].includes(service));
done();
}));

checkWrap(req);
});
Expand All @@ -206,9 +205,3 @@ TEST(function test_lookupservice_ip_ipv6(done) {
checkWrap(req);
}); */

process.on('exit', function() {
console.log(completed + ' tests completed');
assert.equal(running, false);
assert.strictEqual(expected, completed);
});

0 comments on commit 5e781a3

Please sign in to comment.