@@ -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 ( ) {
@@ -88,60 +85,124 @@ describe('portfinder', function () {
8885
8986 describe ( 'with 5 existing servers' , function ( ) {
9087 beforeAll ( function ( done ) {
91- createServers ( function ( ) {
92- portfinder . getSocket ( {
93- path : path . join ( badDir , 'test.sock' ) ,
94- } , function ( ) {
95- done ( ) ;
96- } ) ;
97- } ) ;
88+ createServers ( done ) ;
9889 } ) ;
9990
10091 afterAll ( function ( done ) {
10192 stopServers ( done ) ;
10293 } ) ;
10394
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 ( ) ;
95+ describe . each ( [
96+ [ 'getSocket()' , false , portfinder . getSocket ] ,
97+ [ 'getSocket()' , true , portfinder . getSocket ] ,
98+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
99+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
100+ test ( 'should respond with the first free socket (test5.sock)' , function ( done ) {
101+ if ( isPromise ) {
102+ method ( {
103+ path : path . join ( socketDir , 'test.sock' )
104+ } )
105+ . then ( function ( socket ) {
106+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
107+ done ( ) ;
108+ } )
109+ . catch ( function ( err ) {
110+ done ( err ) ;
111+ } ) ;
112+ } else {
113+ method ( {
114+ path : path . join ( socketDir , 'test.sock' ) ,
115+ } , function ( err , socket ) {
116+ if ( err ) {
117+ done ( err ) ;
118+ return ;
119+ }
120+ expect ( err ) . toBeNull ( ) ;
121+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
122+ done ( ) ;
123+ } ) ;
124+ }
111125 } ) ;
112126 } ) ;
113127 } ) ;
114128
115129 describe ( 'with no existing servers' , function ( ) {
116- describe ( 'the getSocket() method' , function ( ) {
130+ describe . each ( [
131+ [ 'getSocket()' , false , portfinder . getSocket ] ,
132+ [ 'getSocket()' , true , portfinder . getSocket ] ,
133+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
134+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
117135 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- } ) ;
136+ if ( isPromise ) {
137+ method ( {
138+ path : path . join ( badDir , 'test.sock' ) ,
139+ } )
140+ . then ( function ( socket ) {
141+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
142+ done ( ) ;
143+ } )
144+ . catch ( function ( err ) {
145+ done ( err ) ;
146+ } ) ;
147+ } else {
148+ method ( {
149+ path : path . join ( badDir , 'test.sock' ) ,
150+ } , function ( err , socket ) {
151+ if ( err ) {
152+ done ( err ) ;
153+ return ;
154+ }
155+ expect ( err ) . toBeNull ( ) ;
156+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
157+ done ( ) ;
158+ } ) ;
159+ }
125160 } ) ;
126161
127162 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- } ) ;
163+ if ( isPromise ) {
164+ method ( {
165+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
166+ } )
167+ . then ( function ( socket ) {
168+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
169+ done ( ) ;
170+ } )
171+ . catch ( function ( err ) {
172+ done ( err ) ;
173+ } ) ;
174+ } else {
175+ method ( {
176+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
177+ } , function ( err , socket ) {
178+ expect ( err ) . toBeNull ( ) ;
179+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
180+ done ( ) ;
181+ } ) ;
182+ }
135183 } ) ;
136184
137185 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- } ) ;
186+ if ( isPromise ) {
187+ method ( {
188+ path : path . join ( socketDir , 'test.sock' ) ,
189+ } )
190+ . then ( function ( socket ) {
191+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
192+ done ( ) ;
193+ } )
194+ . catch ( function ( err ) {
195+ done ( err ) ;
196+ } ) ;
197+ } else {
198+ method ( {
199+ path : path . join ( socketDir , 'test.sock' ) ,
200+ } , function ( err , socket ) {
201+ expect ( err ) . toBeNull ( ) ;
202+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test.sock' ) ) ;
203+ done ( ) ;
204+ } ) ;
205+ }
145206 } ) ;
146207 } ) ;
147208 } ) ;
0 commit comments