17
17
const { assert } = require ( 'chai' ) ;
18
18
const events = require ( 'events' ) ;
19
19
const proxyquire = require ( 'proxyquire' ) ;
20
- const dns = require ( 'dns' ) ;
21
20
const util = require ( 'util' ) ;
22
21
23
22
const helper = require ( '../test-helper.js' ) ;
@@ -40,32 +39,17 @@ describe('ControlConnection', function () {
40
39
} ) ;
41
40
describe ( '#init()' , function ( ) {
42
41
this . timeout ( 20000 ) ;
43
- let useLocalhost ;
44
- let useIp6 ;
45
42
46
- before ( function ( done ) {
47
- dns . resolve ( 'localhost' , function ( err ) {
48
- if ( err ) {
49
- helper . trace ( 'localhost can not be resolved' ) ;
50
- }
51
- useLocalhost = ! err ;
52
-
53
- done ( ) ;
54
- } ) ;
55
- } ) ;
43
+ const localhost = 'localhost' ;
56
44
57
- before ( done => dns . resolve6 ( 'localhost' , ( err , addresses ) => {
58
- useIp6 = ! err && addresses . length > 0 ;
59
- done ( ) ;
60
- } ) ) ;
61
-
62
- async function testResolution ( CcMock , expectedHosts , expectedResolved ) {
45
+ async function testResolution ( CcMock , expectedHosts , expectedResolved , hostName ) {
63
46
if ( ! expectedResolved ) {
64
47
expectedResolved = expectedHosts ;
65
48
}
66
49
50
+ const contactPointHostName = ( hostName || localhost ) ;
67
51
const state = { } ;
68
- const cc = new CcMock ( clientOptions . extend ( { contactPoints : [ 'my-host-name' ] } ) , null , getContext ( {
52
+ const cc = new CcMock ( clientOptions . extend ( { contactPoints : [ contactPointHostName ] } ) , null , getContext ( {
69
53
failBorrow : 10 , state
70
54
} ) ) ;
71
55
@@ -81,44 +65,53 @@ describe('ControlConnection', function () {
81
65
assert . instanceOf ( err , errors . NoHostAvailableError ) ;
82
66
assert . deepStrictEqual ( state . connectionAttempts . sort ( ) , expectedHosts . sort ( ) ) ;
83
67
const resolvedContactPoints = cc . getResolvedContactPoints ( ) ;
84
- assert . deepStrictEqual ( resolvedContactPoints . get ( 'my-host-name' ) , expectedResolved ) ;
68
+ assert . deepStrictEqual ( resolvedContactPoints . get ( contactPointHostName ) , expectedResolved ) ;
85
69
}
86
70
87
- it ( 'should resolve IPv4 and IPv6 addresses' , async ( ) => {
88
- if ( ! useLocalhost || ! useIp6 ) {
89
- return ;
71
+ // Simple utility function to return a value only if we actually get a request for the name
72
+ // "localhost". Allows us to make our mocks a bit more stringent.
73
+ function ifLocalhost ( name , localhostVal ) {
74
+ if ( name === localhost ) {
75
+ return localhostVal ;
90
76
}
77
+ return [ ] ;
78
+ }
91
79
92
- const state = { } ;
93
- const cc = newInstance ( { contactPoints : [ 'localhost' ] } , getContext ( { state, failBorrow : [ 0 , 1 ] } ) ) ;
94
-
95
- let err ;
96
- try {
97
- await cc . init ( ) ;
98
- } catch ( e ) {
99
- err = e ;
100
- }
80
+ it ( 'should resolve IPv4 and IPv6 addresses, default host (localhost) and port' , ( ) => {
81
+ const ControlConnectionMock = proxyquire ( '../../lib/control-connection' , { dns : {
82
+ resolve4 : function ( name , cb ) {
83
+ cb ( null , ifLocalhost ( name , [ '127.0.0.1' ] ) ) ;
84
+ } ,
85
+ resolve6 : function ( name , cb ) {
86
+ cb ( null , ifLocalhost ( name , [ '::1' ] ) ) ;
87
+ } ,
88
+ lookup : function ( ) {
89
+ throw new Error ( 'dns.lookup() should not be used' ) ;
90
+ }
91
+ } } ) ;
101
92
102
- cc . shutdown ( ) ;
103
- helper . assertInstanceOf ( err , errors . NoHostAvailableError ) ;
104
- assert . deepEqual ( state . connectionAttempts . sort ( ) , [ '127.0.0.1:9042' , '::1:9042' ] ) ;
93
+ return testResolution ( ControlConnectionMock ,
94
+ [ '127.0.0.1:9042' , '::1:9042' ] ,
95
+ [ '127.0.0.1:9042' , '[ ::1] :9042' ] ) ;
105
96
} ) ;
106
97
107
- it ( 'should resolve IPv4 and IPv6 addresses with non default port' , async ( ) => {
108
- if ( ! useLocalhost ) {
109
- return ;
110
- }
111
-
112
- const cc = newInstance ( { contactPoints : [ 'localhost:9999' ] } , getContext ( ) ) ;
113
-
114
- await cc . init ( ) ;
98
+ it ( 'should resolve IPv4 and IPv6 addresses with non default port' , ( ) => {
99
+ const ControlConnectionMock = proxyquire ( '../../lib/control-connection' , { dns : {
100
+ resolve4 : function ( name , cb ) {
101
+ cb ( null , ifLocalhost ( name , [ '127.0.0.1' ] ) ) ;
102
+ } ,
103
+ resolve6 : function ( name , cb ) {
104
+ cb ( null , ifLocalhost ( name , [ '::1' ] ) ) ;
105
+ } ,
106
+ lookup : function ( ) {
107
+ throw new Error ( 'dns.lookup() should not be used' ) ;
108
+ }
109
+ } } ) ;
115
110
116
- cc . shutdown ( ) ;
117
- cc . hosts . values ( ) . forEach ( h => h . shutdown ( ) ) ;
118
- const hosts = cc . hosts . values ( ) ;
119
- assert . strictEqual ( hosts . length , 1 ) ;
120
- // Resolved to ::1 or 127.0.0.
121
- helper . assertContains ( hosts [ 0 ] . address , '1:9999' ) ;
111
+ return testResolution ( ControlConnectionMock ,
112
+ [ '127.0.0.1:9999' , '::1:9999' ] ,
113
+ [ '127.0.0.1:9999' , '[::1]:9999' ] ,
114
+ 'localhost:9999' ) ;
122
115
} ) ;
123
116
124
117
it ( 'should resolve all IPv4 and IPv6 addresses provided by dns.resolve()' , ( ) => {
0 commit comments