1717const { assert } = require ( 'chai' ) ;
1818const events = require ( 'events' ) ;
1919const proxyquire = require ( 'proxyquire' ) ;
20- const dns = require ( 'dns' ) ;
2120const util = require ( 'util' ) ;
2221
2322const helper = require ( '../test-helper.js' ) ;
@@ -40,32 +39,17 @@ describe('ControlConnection', function () {
4039 } ) ;
4140 describe ( '#init()' , function ( ) {
4241 this . timeout ( 20000 ) ;
43- let useLocalhost ;
44- let useIp6 ;
4542
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' ;
5644
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 ) {
6346 if ( ! expectedResolved ) {
6447 expectedResolved = expectedHosts ;
6548 }
6649
50+ const contactPointHostName = ( hostName || localhost ) ;
6751 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 ( {
6953 failBorrow : 10 , state
7054 } ) ) ;
7155
@@ -81,44 +65,53 @@ describe('ControlConnection', function () {
8165 assert . instanceOf ( err , errors . NoHostAvailableError ) ;
8266 assert . deepStrictEqual ( state . connectionAttempts . sort ( ) , expectedHosts . sort ( ) ) ;
8367 const resolvedContactPoints = cc . getResolvedContactPoints ( ) ;
84- assert . deepStrictEqual ( resolvedContactPoints . get ( 'my-host-name' ) , expectedResolved ) ;
68+ assert . deepStrictEqual ( resolvedContactPoints . get ( contactPointHostName ) , expectedResolved ) ;
8569 }
8670
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 ;
9076 }
77+ return [ ] ;
78+ }
9179
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+ } } ) ;
10192
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' ] ) ;
10596 } ) ;
10697
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+ } } ) ;
115110
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' ) ;
122115 } ) ;
123116
124117 it ( 'should resolve all IPv4 and IPv6 addresses provided by dns.resolve()' , ( ) => {
0 commit comments