@@ -25,7 +25,8 @@ function createServers (callback) {
2525 function ( next ) {
2626 const server = net . createServer ( function ( ) { } ) ,
2727 name = base === 0 ? 'test.sock' : 'test' + base + '.sock' ;
28- let sock = path . join ( socketDir , name ) ;
28+ const socket = path . join ( socketDir , name ) ;
29+ let sock = socket ;
2930
3031 // shamelessly stolen from foreverjs,
3132 // https://github.com/foreverjs/forever/blob/6d143609dd3712a1cf1bc515d24ac6b9d32b2588/lib/forever/worker.js#L141-L154
@@ -46,22 +47,19 @@ function createServers (callback) {
4647
4748 server . listen ( sock , next ) ;
4849 base ++ ;
49- servers . push ( server ) ;
50+ servers . push ( [ server , socket ] ) ;
5051 } , callback ) ;
5152}
5253
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 ) ;
54+ function stopServers ( callback ) {
55+ _async . each ( servers , function ( [ server , socket ] , next ) {
56+ server . close ( function ( ) {
57+ if ( process . platform === 'win32' && fs . existsSync ( socket ) ) {
58+ fs . unlinkSync ( socket ) ;
6059 }
60+ next ( ) ;
6161 } ) ;
62- } else {
63- callback ( null , true ) ;
64- }
62+ } , callback ) ;
6563}
6664
6765function cleanup ( callback ) {
@@ -74,7 +72,7 @@ function cleanup(callback) {
7472 if ( fs . existsSync ( badDir ) ) {
7573 fs . rmdirSync ( badDir ) ;
7674 }
77- stopServers ( callback , 0 ) ;
75+ stopServers ( callback ) ;
7876}
7977
8078describe ( 'portfinder' , function ( ) {
@@ -88,60 +86,126 @@ describe('portfinder', function () {
8886
8987 describe ( 'with 5 existing servers' , function ( ) {
9088 beforeAll ( function ( done ) {
91- createServers ( function ( ) {
92- portfinder . getSocket ( {
93- path : path . join ( badDir , 'test.sock' ) ,
94- } , function ( ) {
95- done ( ) ;
96- } ) ;
97- } ) ;
89+ createServers ( done ) ;
9890 } ) ;
9991
10092 afterAll ( function ( done ) {
10193 stopServers ( done ) ;
10294 } ) ;
10395
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 ( ) ;
96+ describe . each ( [
97+ [ 'getSocket()' , false , portfinder . getSocket ] ,
98+ [ 'getSocket()' , true , portfinder . getSocket ] ,
99+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
100+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
101+ test ( 'should respond with the first free socket (test5.sock)' , function ( done ) {
102+ if ( isPromise ) {
103+ method ( {
104+ path : path . join ( socketDir , 'test.sock' )
105+ } )
106+ . then ( function ( socket ) {
107+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
108+ done ( ) ;
109+ } )
110+ . catch ( function ( err ) {
111+ done ( err ) ;
112+ } ) ;
113+ } else {
114+ method ( {
115+ path : path . join ( socketDir , 'test.sock' ) ,
116+ } , function ( err , socket ) {
117+ if ( err ) {
118+ done ( err ) ;
119+ return ;
120+ }
121+ expect ( err ) . toBeNull ( ) ;
122+ expect ( socket ) . toEqual ( path . join ( socketDir , 'test5.sock' ) ) ;
123+ done ( ) ;
124+ } ) ;
125+ }
111126 } ) ;
112127 } ) ;
113128 } ) ;
114129
115130 describe ( 'with no existing servers' , function ( ) {
116- describe ( 'the getSocket() method' , function ( ) {
131+ describe . each ( [
132+ [ 'getSocket()' , false , portfinder . getSocket ] ,
133+ [ 'getSocket()' , true , portfinder . getSocket ] ,
134+ [ 'getSocketPromise()' , true , portfinder . getSocketPromise ] ,
135+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
117136 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- } ) ;
137+ if ( isPromise ) {
138+ method ( {
139+ path : path . join ( badDir , 'test.sock' ) ,
140+ } )
141+ . then ( function ( socket ) {
142+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
143+ done ( ) ;
144+ } )
145+ . catch ( function ( err ) {
146+ done ( err ) ;
147+ } ) ;
148+ } else {
149+ method ( {
150+ path : path . join ( badDir , 'test.sock' ) ,
151+ } , function ( err , socket ) {
152+ if ( err ) {
153+ done ( err ) ;
154+ return ;
155+ }
156+ expect ( err ) . toBeNull ( ) ;
157+ expect ( socket ) . toEqual ( path . join ( badDir , 'test.sock' ) ) ;
158+ done ( ) ;
159+ } ) ;
160+ }
125161 } ) ;
126162
127163 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- } ) ;
164+ if ( isPromise ) {
165+ method ( {
166+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
167+ } )
168+ . then ( function ( socket ) {
169+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
170+ done ( ) ;
171+ } )
172+ . catch ( function ( err ) {
173+ done ( err ) ;
174+ } ) ;
175+ } else {
176+ method ( {
177+ path : path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ,
178+ } , function ( err , socket ) {
179+ expect ( err ) . toBeNull ( ) ;
180+ expect ( socket ) . toEqual ( path . join ( badDir , 'deeply' , 'nested' , 'test.sock' ) ) ;
181+ done ( ) ;
182+ } ) ;
183+ }
135184 } ) ;
136185
137- 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+ // We don't use `test.sock` here due to some race condition on Windows in freeing the `test.sock` file
187+ // when we close the servers.
188+ test ( 'with a directory that exists should respond with the first free socket (foo.sock)' , function ( done ) {
189+ if ( isPromise ) {
190+ method ( {
191+ path : path . join ( socketDir , 'foo.sock' ) ,
192+ } )
193+ . then ( function ( socket ) {
194+ expect ( socket ) . toEqual ( path . join ( socketDir , 'foo.sock' ) ) ;
195+ done ( ) ;
196+ } )
197+ . catch ( function ( err ) {
198+ done ( err ) ;
199+ } ) ;
200+ } else {
201+ method ( {
202+ path : path . join ( socketDir , 'foo.sock' ) ,
203+ } , function ( err , socket ) {
204+ expect ( err ) . toBeNull ( ) ;
205+ expect ( socket ) . toEqual ( path . join ( socketDir , 'foo.sock' ) ) ;
206+ done ( ) ;
207+ } ) ;
208+ }
145209 } ) ;
146210 } ) ;
147211 } ) ;
0 commit comments