test: refactor the code in test-dns-ipv4
* 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: https://github.com/nodejs/node/pull/10200 Reviewed-By: Italo A. Casas <me@italoacasas.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
1b2d3f7ae7
commit
4d3b487b79
@ -1,18 +1,16 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const dns = require('dns');
|
const dns = require('dns');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const isIPv4 = net.isIPv4;
|
const isIPv4 = net.isIPv4;
|
||||||
|
|
||||||
let expected = 0;
|
|
||||||
let completed = 0;
|
|
||||||
let running = false;
|
let running = false;
|
||||||
const queue = [];
|
const queue = [];
|
||||||
|
|
||||||
function TEST(f) {
|
function TEST(f) {
|
||||||
function next() {
|
function next() {
|
||||||
var f = queue.shift();
|
const f = queue.shift();
|
||||||
if (f) {
|
if (f) {
|
||||||
running = true;
|
running = true;
|
||||||
console.log(f.name);
|
console.log(f.name);
|
||||||
@ -22,11 +20,9 @@ function TEST(f) {
|
|||||||
|
|
||||||
function done() {
|
function done() {
|
||||||
running = false;
|
running = false;
|
||||||
completed++;
|
|
||||||
process.nextTick(next);
|
process.nextTick(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
expected++;
|
|
||||||
queue.push(f);
|
queue.push(f);
|
||||||
|
|
||||||
if (!running) {
|
if (!running) {
|
||||||
@ -39,149 +35,150 @@ function checkWrap(req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(function test_resolve4(done) {
|
TEST(function test_resolve4(done) {
|
||||||
var req = dns.resolve4('www.google.com', function(err, ips) {
|
const req = dns.resolve4('www.google.com',
|
||||||
if (err) throw err;
|
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++) {
|
for (let i = 0; i < ips.length; i++) {
|
||||||
assert.ok(isIPv4(ips[i]));
|
assert.ok(isIPv4(ips[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_reverse_ipv4(done) {
|
TEST(function test_reverse_ipv4(done) {
|
||||||
var req = dns.reverse('8.8.8.8', function(err, domains) {
|
const req = dns.reverse('8.8.8.8',
|
||||||
if (err) throw err;
|
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++) {
|
for (let i = 0; i < domains.length; i++) {
|
||||||
assert.ok(domains[i]);
|
assert.ok(domains[i]);
|
||||||
assert.ok(typeof domains[i] === 'string');
|
assert.ok(typeof domains[i] === 'string');
|
||||||
}
|
}
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_ipv4_explicit(done) {
|
TEST(function test_lookup_ipv4_explicit(done) {
|
||||||
var req = dns.lookup('www.google.com', 4, function(err, ip, family) {
|
const req = dns.lookup('www.google.com', 4,
|
||||||
if (err) throw err;
|
common.mustCall((err, ip, family) => {
|
||||||
assert.ok(net.isIPv4(ip));
|
assert.ifError(err);
|
||||||
assert.strictEqual(family, 4);
|
assert.ok(net.isIPv4(ip));
|
||||||
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_ipv4_implicit(done) {
|
TEST(function test_lookup_ipv4_implicit(done) {
|
||||||
var req = dns.lookup('www.google.com', function(err, ip, family) {
|
const req = dns.lookup('www.google.com',
|
||||||
if (err) throw err;
|
common.mustCall((err, ip, family) => {
|
||||||
assert.ok(net.isIPv4(ip));
|
assert.ifError(err);
|
||||||
assert.strictEqual(family, 4);
|
assert.ok(net.isIPv4(ip));
|
||||||
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_ipv4_explicit_object(done) {
|
TEST(function test_lookup_ipv4_explicit_object(done) {
|
||||||
var req = dns.lookup('www.google.com', {
|
const req = dns.lookup('www.google.com', {
|
||||||
family: 4
|
family: 4
|
||||||
}, function(err, ip, family) {
|
}, common.mustCall((err, ip, family) => {
|
||||||
if (err) throw err;
|
assert.ifError(err);
|
||||||
assert.ok(net.isIPv4(ip));
|
assert.ok(net.isIPv4(ip));
|
||||||
assert.strictEqual(family, 4);
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_ipv4_hint_addrconfig(done) {
|
TEST(function test_lookup_ipv4_hint_addrconfig(done) {
|
||||||
var req = dns.lookup('www.google.com', {
|
const req = dns.lookup('www.google.com', {
|
||||||
hints: dns.ADDRCONFIG
|
hints: dns.ADDRCONFIG
|
||||||
}, function(err, ip, family) {
|
}, common.mustCall((err, ip, family) => {
|
||||||
if (err) throw err;
|
assert.ifError(err);
|
||||||
assert.ok(net.isIPv4(ip));
|
assert.ok(net.isIPv4(ip));
|
||||||
assert.strictEqual(family, 4);
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_ip_ipv4(done) {
|
TEST(function test_lookup_ip_ipv4(done) {
|
||||||
var req = dns.lookup('127.0.0.1', function(err, ip, family) {
|
const req = dns.lookup('127.0.0.1',
|
||||||
if (err) throw err;
|
common.mustCall((err, ip, family) => {
|
||||||
assert.strictEqual(ip, '127.0.0.1');
|
assert.ifError(err);
|
||||||
assert.strictEqual(family, 4);
|
assert.strictEqual(ip, '127.0.0.1');
|
||||||
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_localhost_ipv4(done) {
|
TEST(function test_lookup_localhost_ipv4(done) {
|
||||||
var req = dns.lookup('localhost', 4, function(err, ip, family) {
|
const req = dns.lookup('localhost', 4,
|
||||||
if (err) throw err;
|
common.mustCall((err, ip, family) => {
|
||||||
assert.strictEqual(ip, '127.0.0.1');
|
assert.ifError(err);
|
||||||
assert.strictEqual(family, 4);
|
assert.strictEqual(ip, '127.0.0.1');
|
||||||
|
assert.strictEqual(family, 4);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookup_all_ipv4(done) {
|
TEST(function test_lookup_all_ipv4(done) {
|
||||||
var req = dns.lookup(
|
const req = dns.lookup(
|
||||||
'www.google.com',
|
'www.google.com',
|
||||||
{all: true, family: 4},
|
{all: true, family: 4},
|
||||||
function(err, ips) {
|
common.mustCall((err, ips) => {
|
||||||
if (err) throw err;
|
assert.ifError(err);
|
||||||
assert.ok(Array.isArray(ips));
|
assert.ok(Array.isArray(ips));
|
||||||
assert.ok(ips.length > 0);
|
assert.ok(ips.length > 0);
|
||||||
|
|
||||||
ips.forEach(function(ip) {
|
ips.forEach((ip) => {
|
||||||
assert.ok(isIPv4(ip.address));
|
assert.ok(isIPv4(ip.address));
|
||||||
assert.strictEqual(ip.family, 4);
|
assert.strictEqual(ip.family, 4);
|
||||||
});
|
});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
);
|
));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
TEST(function test_lookupservice_ip_ipv4(done) {
|
TEST(function test_lookupservice_ip_ipv4(done) {
|
||||||
var req = dns.lookupService('127.0.0.1', 80, function(err, host, service) {
|
const req = dns.lookupService('127.0.0.1', 80,
|
||||||
if (err) throw err;
|
common.mustCall((err, host, service) => {
|
||||||
assert.equal(typeof host, 'string');
|
assert.ifError(err);
|
||||||
assert(host);
|
assert.strictEqual(typeof host, 'string');
|
||||||
assert(['http', 'www', '80'].includes(service));
|
assert(host);
|
||||||
done();
|
assert(['http', 'www', '80'].includes(service));
|
||||||
});
|
done();
|
||||||
|
}));
|
||||||
|
|
||||||
checkWrap(req);
|
checkWrap(req);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('exit', function() {
|
|
||||||
console.log(completed + ' tests completed');
|
|
||||||
assert.equal(running, false);
|
|
||||||
assert.strictEqual(expected, completed);
|
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user