11'use strict' ;
2- var common = require ( '../common' ) ;
3- var assert = require ( 'assert' ) ;
4- var net = require ( 'net' ) ;
5- var dns = require ( 'dns' ) ;
2+ const common = require ( '../common' ) ;
3+ const assert = require ( 'assert' ) ;
4+ const net = require ( 'net' ) ;
5+ const dns = require ( 'dns' ) ;
66
77if ( ! common . hasIPv6 ) {
88 console . log ( '1..0 # Skipped: no IPv6 support' ) ;
@@ -12,46 +12,60 @@ if (!common.hasIPv6) {
1212var serverGotEnd = false ;
1313var clientGotEnd = false ;
1414
15- dns . lookup ( 'localhost' , 6 , function ( err ) {
16- if ( err ) {
17- console . error ( 'Looks like IPv6 is not really supported' ) ;
18- console . error ( err ) ;
19- return ;
20- }
15+ const hosts = common . localIPv6Hosts ;
16+ var hostIdx = 0 ;
17+ var host = hosts [ hostIdx ] ;
18+ var localhostTries = 10 ;
2119
22- var server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
23- socket . resume ( ) ;
24- socket . on ( 'end' , function ( ) {
25- serverGotEnd = true ;
26- } ) ;
27- socket . end ( ) ;
20+ const server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
21+ socket . resume ( ) ;
22+ socket . on ( 'end' , function ( ) {
23+ serverGotEnd = true ;
2824 } ) ;
25+ socket . end ( ) ;
26+ } ) ;
2927
30- server . listen ( common . PORT , '::1' , function ( ) {
31- var client = net . connect ( {
32- host : 'localhost' ,
33- port : common . PORT ,
34- family : 6 ,
35- allowHalfOpen : true
36- } , function ( ) {
37- console . error ( 'client connect cb' ) ;
38- client . resume ( ) ;
39- client . on ( 'end' , function ( ) {
40- clientGotEnd = true ;
41- setTimeout ( function ( ) {
42- assert ( client . writable ) ;
43- client . end ( ) ;
44- } , 10 ) ;
45- } ) ;
46- client . on ( 'close' , function ( ) {
47- server . close ( ) ;
48- } ) ;
28+ server . listen ( common . PORT , '::1' , tryConnect ) ;
29+
30+ function tryConnect ( ) {
31+ const client = net . connect ( {
32+ host : host ,
33+ port : common . PORT ,
34+ family : 6 ,
35+ allowHalfOpen : true
36+ } , function ( ) {
37+ console . error ( 'client connect cb' ) ;
38+ client . resume ( ) ;
39+ client . on ( 'end' , function ( ) {
40+ clientGotEnd = true ;
41+ setTimeout ( function ( ) {
42+ assert ( client . writable ) ;
43+ client . end ( ) ;
44+ } , 10 ) ;
45+ } ) ;
46+ client . on ( 'close' , function ( ) {
47+ server . close ( ) ;
4948 } ) ;
49+ } ) . on ( 'error' , function ( err ) {
50+ if ( err . syscall === 'getaddrinfo' && err . code === 'ENOTFOUND' ) {
51+ if ( host !== 'localhost' || -- localhostTries === 0 )
52+ host = hosts [ ++ hostIdx ] ;
53+ if ( host )
54+ tryConnect ( ) ;
55+ else {
56+ console . log ( '1..0 # Skipped: no IPv6 localhost support' ) ;
57+ process . removeListener ( 'exit' , onExit ) ;
58+ server . close ( ) ;
59+ }
60+ return ;
61+ }
62+ throw err ;
5063 } ) ;
64+ }
5165
52- process . on ( 'exit' , function ( ) {
53- console . error ( 'exit' , serverGotEnd , clientGotEnd ) ;
54- assert ( serverGotEnd ) ;
55- assert ( clientGotEnd ) ;
56- } ) ;
57- } ) ;
66+ process . on ( 'exit' , onExit ) ;
67+ function onExit ( ) {
68+ console . error ( 'exit' , serverGotEnd , clientGotEnd ) ;
69+ assert ( serverGotEnd ) ;
70+ assert ( clientGotEnd ) ;
71+ }
0 commit comments