@@ -50,18 +50,12 @@ function createServers (callback) {
5050 } , callback ) ;
5151}
5252
53- function stopServers ( callback , index ) {
54- if ( index < servers . length ) {
55- servers [ index ] . close ( function ( err ) {
56- if ( err ) {
57- callback ( err , false ) ;
58- } else {
59- stopServers ( callback , index + 1 ) ;
60- }
53+ function stopServers ( callback ) {
54+ _async . each ( servers , function ( server , next ) {
55+ server . close ( function ( ) {
56+ next ( ) ;
6157 } ) ;
62- } else {
63- callback ( null , true ) ;
64- }
58+ } , callback ) ;
6559}
6660
6761function cleanup ( callback ) {
@@ -74,7 +68,7 @@ function cleanup(callback) {
7468 if ( fs . existsSync ( badDir ) ) {
7569 fs . rmdirSync ( badDir ) ;
7670 }
77- stopServers ( callback , 0 ) ;
71+ stopServers ( callback ) ;
7872}
7973
8074describe ( 'portfinder' , function ( ) {
@@ -101,47 +95,117 @@ describe('portfinder', function () {
10195 stopServers ( done ) ;
10296 } ) ;
10397
104- test ( 'the getSocket() method should respond with the first free socket (test5.sock)' , function ( done ) {
105- portfinder . getSocket ( {
106- path : path . join ( socketDir , 'test.sock' ) ,
107- } , function ( err , socket ) {
108- expect ( err ) . toBeNull ( ) ;
109- expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
110- done ( ) ;
98+ describe . each ( [
99+ [ 'getSocket()' , false , portfinder . getSocket ] ,
100+ [ 'getSocket()' , true , portfinder . getSocket ] ,
101+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
102+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
103+ test ( 'should respond with the first free socket (test5.sock)' , function ( done ) {
104+ if ( isPromise ) {
105+ method ( {
106+ path : path . join ( socketDir , 'test.sock' )
107+ } )
108+ . then ( function ( socket ) {
109+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
110+ done ( ) ;
111+ } )
112+ . catch ( function ( err ) {
113+ done ( err ) ;
114+ } ) ;
115+ } else {
116+ method ( {
117+ path : path . join ( socketDir , 'test.sock' ) ,
118+ } , function ( err , socket ) {
119+ if ( err ) {
120+ done ( err ) ;
121+ return ;
122+ }
123+ expect ( err ) . toBeNull ( ) ;
124+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
125+ done ( ) ;
126+ } ) ;
127+ }
111128 } ) ;
112129 } ) ;
113130 } ) ;
114131
115132 describe ( 'with no existing servers' , function ( ) {
116- describe ( 'the getSocket() method' , function ( ) {
133+ describe . each ( [
134+ [ 'getSocket()' , false , portfinder . getSocket ] ,
135+ [ 'getSocket()' , true , portfinder . getSocket ] ,
136+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
137+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
117138 test ( "with a directory that doesn't exist should respond with the first free socket (test.sock)" , function ( done ) {
118- portfinder . getSocket ( {
119- path : path . join ( badDir , 'test.sock' ) ,
120- } , function ( err , socket ) {
121- expect ( err ) . toBeNull ( ) ;
122- expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
123- done ( ) ;
124- } ) ;
139+ if ( isPromise ) {
140+ method ( {
141+ path : path . join ( badDir , 'test.sock' ) ,
142+ } )
143+ . then ( function ( socket ) {
144+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
145+ done ( ) ;
146+ } )
147+ . catch ( function ( err ) {
148+ done ( err ) ;
149+ } ) ;
150+ } else {
151+ method ( {
152+ path : path . join ( badDir , 'test.sock' ) ,
153+ } , function ( err , socket ) {
154+ if ( err ) {
155+ done ( err ) ;
156+ return ;
157+ }
158+ expect ( err ) . toBeNull ( ) ;
159+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
160+ done ( ) ;
161+ } ) ;
162+ }
125163 } ) ;
126164
127165 test ( "with a nested directory that doesn't exist should respond with the first free socket (test.sock)" , function ( done ) {
128- portfinder . getSocket ( {
129- path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
130- } , function ( err , socket ) {
131- expect ( err ) . toBeNull ( ) ;
132- expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
133- done ( ) ;
134- } ) ;
166+ if ( isPromise ) {
167+ method ( {
168+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
169+ } )
170+ . then ( function ( socket ) {
171+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
172+ done ( ) ;
173+ } )
174+ . catch ( function ( err ) {
175+ done ( err ) ;
176+ } ) ;
177+ } else {
178+ method ( {
179+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
180+ } , function ( err , socket ) {
181+ expect ( err ) . toBeNull ( ) ;
182+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
183+ done ( ) ;
184+ } ) ;
185+ }
135186 } ) ;
136187
137188 test ( 'with a directory that exists should respond with the first free socket (test.sock)' , function ( done ) {
138- portfinder . getSocket ( {
139- path : path . join ( socketDir , 'exists.sock' ) ,
140- } , function ( err , socket ) {
141- expect ( err ) . toBeNull ( ) ;
142- expect ( socket ) . toEqual ( path . join ( socketDir , 'exists.sock' ) ) ;
143- done ( ) ;
144- } ) ;
189+ if ( isPromise ) {
190+ method ( {
191+ path : path . join ( socketDir , 'test.sock' ) ,
192+ } )
193+ . then ( function ( socket ) {
194+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
195+ done ( ) ;
196+ } )
197+ . catch ( function ( err ) {
198+ done ( err ) ;
199+ } ) ;
200+ } else {
201+ method ( {
202+ path : path . join ( socketDir , 'test.sock' ) ,
203+ } , function ( err , socket ) {
204+ expect ( err ) . toBeNull ( ) ;
205+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
206+ done ( ) ;
207+ } ) ;
208+ }
145209 } ) ;
146210 } ) ;
147211 } ) ;
0 commit comments