@@ -46,22 +46,19 @@ function createServers (callback) {
4646
4747 server . listen ( sock , next ) ;
4848 base ++ ;
49- servers . push ( server ) ;
49+ servers . push ( [ server , sock ] ) ;
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 ) ;
53+ function stopServers ( callback ) {
54+ _async . each ( servers , function ( [ server , sock ] , next ) {
55+ server . close ( function ( ) {
56+ if ( process . platform === 'win32' ) {
57+ fs . unlinkSync ( sock ) ;
6058 }
59+ next ( ) ;
6160 } ) ;
62- } else {
63- callback ( null , true ) ;
64- }
61+ } , callback ) ;
6562}
6663
6764function cleanup ( callback ) {
@@ -74,7 +71,7 @@ function cleanup(callback) {
7471 if ( fs . existsSync ( badDir ) ) {
7572 fs . rmdirSync ( badDir ) ;
7673 }
77- stopServers ( callback , 0 ) ;
74+ stopServers ( callback ) ;
7875}
7976
8077describe ( 'portfinder' , function ( ) {
@@ -101,47 +98,117 @@ describe('portfinder', function () {
10198 stopServers ( done ) ;
10299 } ) ;
103100
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 ( ) ;
101+ describe . each ( [
102+ [ 'getSocket()' , false , portfinder . getSocket ] ,
103+ [ 'getSocket()' , true , portfinder . getSocket ] ,
104+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
105+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
106+ test ( 'should respond with the first free socket (test5.sock)' , function ( done ) {
107+ if ( isPromise ) {
108+ method ( {
109+ path : path . join ( socketDir , 'test.sock' )
110+ } )
111+ . then ( function ( socket ) {
112+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
113+ done ( ) ;
114+ } )
115+ . catch ( function ( err ) {
116+ done ( err ) ;
117+ } ) ;
118+ } else {
119+ method ( {
120+ path : path . join ( socketDir , 'test.sock' ) ,
121+ } , function ( err , socket ) {
122+ if ( err ) {
123+ done ( err ) ;
124+ return ;
125+ }
126+ expect ( err ) . toBeNull ( ) ;
127+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
128+ done ( ) ;
129+ } ) ;
130+ }
111131 } ) ;
112132 } ) ;
113133 } ) ;
114134
115135 describe ( 'with no existing servers' , function ( ) {
116- describe ( 'the getSocket() method' , function ( ) {
136+ describe . each ( [
137+ [ 'getSocket()' , false , portfinder . getSocket ] ,
138+ [ 'getSocket()' , true , portfinder . getSocket ] ,
139+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
140+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
117141 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- } ) ;
142+ if ( isPromise ) {
143+ method ( {
144+ path : path . join ( badDir , 'test.sock' ) ,
145+ } )
146+ . then ( function ( socket ) {
147+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
148+ done ( ) ;
149+ } )
150+ . catch ( function ( err ) {
151+ done ( err ) ;
152+ } ) ;
153+ } else {
154+ method ( {
155+ path : path . join ( badDir , 'test.sock' ) ,
156+ } , function ( err , socket ) {
157+ if ( err ) {
158+ done ( err ) ;
159+ return ;
160+ }
161+ expect ( err ) . toBeNull ( ) ;
162+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
163+ done ( ) ;
164+ } ) ;
165+ }
125166 } ) ;
126167
127168 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- } ) ;
169+ if ( isPromise ) {
170+ method ( {
171+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
172+ } )
173+ . then ( function ( socket ) {
174+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
175+ done ( ) ;
176+ } )
177+ . catch ( function ( err ) {
178+ done ( err ) ;
179+ } ) ;
180+ } else {
181+ method ( {
182+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
183+ } , function ( err , socket ) {
184+ expect ( err ) . toBeNull ( ) ;
185+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
186+ done ( ) ;
187+ } ) ;
188+ }
135189 } ) ;
136190
137191 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- } ) ;
192+ if ( isPromise ) {
193+ method ( {
194+ path : path . join ( socketDir , 'test.sock' ) ,
195+ } )
196+ . then ( function ( socket ) {
197+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
198+ done ( ) ;
199+ } )
200+ . catch ( function ( err ) {
201+ done ( err ) ;
202+ } ) ;
203+ } else {
204+ method ( {
205+ path : path . join ( socketDir , 'test.sock' ) ,
206+ } , function ( err , socket ) {
207+ expect ( err ) . toBeNull ( ) ;
208+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
209+ done ( ) ;
210+ } ) ;
211+ }
145212 } ) ;
146213 } ) ;
147214 } ) ;
0 commit comments