Skip to content

Commit 8097bd8

Browse files
committed
test: refactor net.connect ipv6 options test
Use common.skipIfNoIpv6Localhost
1 parent c3c6540 commit 8097bd8

File tree

1 file changed

+34
-61
lines changed

1 file changed

+34
-61
lines changed

test/parallel/test-net-connect-options-ipv6.js

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,44 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// This tests that the family option of net.connect is hornored.
23+
2224
'use strict';
2325
const common = require('../common');
24-
if (!common.hasIPv6)
25-
common.skip('no IPv6 support');
26-
27-
const assert = require('assert');
28-
const net = require('net');
2926

30-
const hosts = common.localIPv6Hosts;
31-
let hostIdx = 0;
32-
let host = hosts[hostIdx];
33-
let localhostTries = 10;
27+
common.skipIfNoIpv6Localhost((ipv6Host) => {
28+
const assert = require('assert');
29+
const net = require('net');
3430

35-
const server = net.createServer({ allowHalfOpen: true }, function(socket) {
36-
socket.resume();
37-
socket.on('end', common.mustCall());
38-
socket.end();
39-
});
31+
const server = net.createServer({
32+
allowHalfOpen: true
33+
}, common.mustCall((socket) => {
34+
assert.strictEqual('::1', socket.remoteAddress);
35+
socket.resume();
36+
socket.on('end', common.mustCall());
37+
socket.end();
38+
}));
4039

41-
server.listen(0, '::1', tryConnect);
40+
server.listen(0, '::1', common.mustCall(tryConnect));
4241

43-
function tryConnect() {
44-
const client = net.connect({
45-
host: host,
46-
port: server.address().port,
47-
family: 6,
48-
allowHalfOpen: true
49-
}, function() {
50-
console.error('client connect cb');
51-
client.resume();
52-
client.on('end', common.mustCall(function() {
53-
setTimeout(function() {
54-
assert(client.writable);
55-
client.end();
56-
}, 10);
57-
}));
58-
client.on('close', function() {
59-
server.close();
60-
});
61-
}).on('error', function(err) {
62-
// ENOTFOUND means we don't have the requested address. In this
63-
// case we try the next one in the list and if we run out of
64-
// candidates we assume IPv6 is not supported on the
65-
// machine and skip the test.
66-
// EAI_AGAIN means we tried to remotely resolve the address and
67-
// timed out or hit some intermittent connectivity issue with the
68-
// dns server. Although we are looking for local loopback addresses
69-
// we may go remote since the list we search includes addresses that
70-
// cover more than is available on any one distribution. The
71-
// net is that if we get an EAI_AGAIN we were looking for an
72-
// address which does not exist in this distribution so the error
73-
// is not significant and we should just move on and try the
74-
// next address in the list.
75-
if ((err.syscall === 'getaddrinfo') && ((err.code === 'ENOTFOUND') ||
76-
(err.code === 'EAI_AGAIN'))) {
77-
if (host !== 'localhost' || --localhostTries === 0)
78-
host = hosts[++hostIdx];
79-
if (host)
80-
tryConnect();
81-
else {
42+
function tryConnect() {
43+
const client = net.connect({
44+
host: ipv6Host,
45+
port: server.address().port,
46+
family: 6,
47+
allowHalfOpen: true
48+
}, common.mustCall(() => {
49+
console.error('client connect cb');
50+
client.resume();
51+
client.on('end', common.mustCall(function() {
52+
setTimeout(function() {
53+
assert(client.writable);
54+
client.end();
55+
}, 10);
56+
}));
57+
client.on('close', function() {
8258
server.close();
83-
common.skip('no IPv6 localhost support');
84-
}
85-
return;
86-
}
87-
throw err;
88-
});
89-
}
59+
});
60+
}));
61+
}
62+
});

0 commit comments

Comments
 (0)