Skip to content

Commit 5e781a3

Browse files
edsadritaloacasas
authored andcommitted
test: refactor the code in test-dns-ipv6
* 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 <[email protected]>
1 parent 8b367c5 commit 5e781a3

File tree

1 file changed

+61
-68
lines changed

1 file changed

+61
-68
lines changed

test/internet/test-dns-ipv6.js

+61-68
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const dns = require('dns');
55
const net = require('net');
66
const isIPv6 = net.isIPv6;
77

8-
let expected = 0;
9-
let completed = 0;
108
let running = false;
119
const queue = [];
1210

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

1816
function TEST(f) {
1917
function next() {
20-
var f = queue.shift();
18+
const f = queue.shift();
2119
if (f) {
2220
running = true;
2321
console.log(f.name);
@@ -27,11 +25,9 @@ function TEST(f) {
2725

2826
function done() {
2927
running = false;
30-
completed++;
3128
process.nextTick(next);
3229
}
3330

34-
expected++;
3531
queue.push(f);
3632

3733
if (!running) {
@@ -44,46 +40,46 @@ function checkWrap(req) {
4440
}
4541

4642
TEST(function test_resolve6(done) {
47-
var req = dns.resolve6('ipv6.google.com', function(err, ips) {
48-
if (err) throw err;
43+
const req = dns.resolve6('ipv6.google.com',
44+
common.mustCall((err, ips) => {
45+
assert.ifError(err);
4946

50-
assert.ok(ips.length > 0);
47+
assert.ok(ips.length > 0);
5148

52-
for (var i = 0; i < ips.length; i++) {
53-
assert.ok(isIPv6(ips[i]));
54-
}
49+
for (let i = 0; i < ips.length; i++)
50+
assert.ok(isIPv6(ips[i]));
5551

56-
done();
57-
});
52+
done();
53+
}));
5854

5955
checkWrap(req);
6056
});
6157

6258
TEST(function test_reverse_ipv6(done) {
63-
var req = dns.reverse('2001:4860:4860::8888', function(err, domains) {
64-
if (err) throw err;
59+
const req = dns.reverse('2001:4860:4860::8888',
60+
common.mustCall((err, domains) => {
61+
assert.ifError(err);
6562

66-
assert.ok(domains.length > 0);
63+
assert.ok(domains.length > 0);
6764

68-
for (var i = 0; i < domains.length; i++) {
69-
assert.ok(domains[i]);
70-
assert.ok(typeof domains[i] === 'string');
71-
}
65+
for (let i = 0; i < domains.length; i++)
66+
assert.ok(typeof domains[i] === 'string');
7267

73-
done();
74-
});
68+
done();
69+
}));
7570

7671
checkWrap(req);
7772
});
7873

7974
TEST(function test_lookup_ipv6_explicit(done) {
80-
var req = dns.lookup('ipv6.google.com', 6, function(err, ip, family) {
81-
if (err) throw err;
82-
assert.ok(net.isIPv6(ip));
83-
assert.strictEqual(family, 6);
75+
const req = dns.lookup('ipv6.google.com', 6,
76+
common.mustCall((err, ip, family) => {
77+
assert.ifError(err);
78+
assert.ok(isIPv6(ip));
79+
assert.strictEqual(family, 6);
8480

85-
done();
86-
});
81+
done();
82+
}));
8783

8884
checkWrap(req);
8985
});
@@ -103,24 +99,24 @@ TEST(function test_lookup_ipv6_implicit(done) {
10399
*/
104100

105101
TEST(function test_lookup_ipv6_explicit_object(done) {
106-
var req = dns.lookup('ipv6.google.com', {
102+
const req = dns.lookup('ipv6.google.com', {
107103
family: 6
108-
}, function(err, ip, family) {
109-
if (err) throw err;
110-
assert.ok(net.isIPv6(ip));
104+
}, common.mustCall((err, ip, family) => {
105+
assert.ifError(err);
106+
assert.ok(isIPv6(ip));
111107
assert.strictEqual(family, 6);
112108

113109
done();
114-
});
110+
}));
115111

116112
checkWrap(req);
117113
});
118114

119115
TEST(function test_lookup_ipv6_hint(done) {
120-
var req = dns.lookup('www.google.com', {
116+
const req = dns.lookup('www.google.com', {
121117
family: 6,
122118
hints: dns.V4MAPPED
123-
}, function(err, ip, family) {
119+
}, common.mustCall((err, ip, family) => {
124120
if (err) {
125121
// FreeBSD does not support V4MAPPED
126122
if (common.isFreeBSD) {
@@ -130,66 +126,69 @@ TEST(function test_lookup_ipv6_hint(done) {
130126
assert.ok(/getaddrinfo EAI_BADFLAGS/.test(err.message));
131127
done();
132128
return;
133-
} else {
134-
throw err;
135129
}
130+
131+
assert.ifError(err);
136132
}
137-
assert.ok(net.isIPv6(ip));
133+
134+
assert.ok(isIPv6(ip));
138135
assert.strictEqual(family, 6);
139136

140137
done();
141-
});
138+
}));
142139

143140
checkWrap(req);
144141
});
145142

146143
TEST(function test_lookup_ip_ipv6(done) {
147-
var req = dns.lookup('::1', function(err, ip, family) {
148-
if (err) throw err;
149-
assert.ok(net.isIPv6(ip));
150-
assert.strictEqual(family, 6);
144+
const req = dns.lookup('::1',
145+
common.mustCall((err, ip, family) => {
146+
assert.ifError(err);
147+
assert.ok(isIPv6(ip));
148+
assert.strictEqual(family, 6);
151149

152-
done();
153-
});
150+
done();
151+
}));
154152

155153
checkWrap(req);
156154
});
157155

158156
TEST(function test_lookup_all_ipv6(done) {
159-
var req = dns.lookup(
157+
const req = dns.lookup(
160158
'www.google.com',
161159
{all: true, family: 6},
162-
function(err, ips) {
163-
if (err) throw err;
160+
common.mustCall((err, ips) => {
161+
assert.ifError(err);
164162
assert.ok(Array.isArray(ips));
165163
assert.ok(ips.length > 0);
166164

167-
ips.forEach(function(ip) {
165+
ips.forEach((ip) => {
168166
assert.ok(isIPv6(ip.address),
169167
'Invalid IPv6: ' + ip.address.toString());
170168
assert.strictEqual(ip.family, 6);
171169
});
172170

173171
done();
174172
}
175-
);
173+
));
176174

177175
checkWrap(req);
178176
});
179177

180178
TEST(function test_lookupservice_ip_ipv6(done) {
181-
var req = dns.lookupService('::1', 80, function(err, host, service) {
182-
if (err) {
183-
// Not skipping the test, rather checking an alternative result,
184-
// i.e. that ::1 may not be configured (e.g. in /etc/hosts)
185-
assert.strictEqual(err.code, 'ENOTFOUND');
186-
return done();
187-
}
188-
assert.equal(typeof host, 'string');
189-
assert(host);
190-
assert(['http', 'www', '80'].includes(service));
191-
done();
192-
});
179+
const req = dns.lookupService('::1', 80,
180+
common.mustCall((err, host, service) => {
181+
if (err) {
182+
// Not skipping the test, rather checking an alternative result,
183+
// i.e. that ::1 may not be configured (e.g. in /etc/hosts)
184+
assert.strictEqual(err.code, 'ENOTFOUND');
185+
return done();
186+
}
187+
assert.strictEqual(typeof host, 'string');
188+
assert(host);
189+
assert(['http', 'www', '80'].includes(service));
190+
done();
191+
}));
193192

194193
checkWrap(req);
195194
});
@@ -206,9 +205,3 @@ TEST(function test_lookupservice_ip_ipv6(done) {
206205
207206
checkWrap(req);
208207
}); */
209-
210-
process.on('exit', function() {
211-
console.log(completed + ' tests completed');
212-
assert.equal(running, false);
213-
assert.strictEqual(expected, completed);
214-
});

0 commit comments

Comments
 (0)