@@ -62,13 +62,12 @@ var CLI = require("cli"),
6262 ASSERT = require ( "assert" ) ,
6363 SOCKET_IO_CLIENT = require ( "socket.io-client" ) ,
6464 XML2JS = require ( "xml2js" ) ,
65- NET = require ( "net" ) ,
66- EXEC = require ( "child_process" ) . exec ;
65+ NET = require ( "net" ) ;
6766
6867
6968var serverInfo = { } ,
70- ourServer = false , // if we started the debug proxy server
71- verboseServerLog = false ;
69+ serverChildInstance = null ,
70+ ourServer = false ; // if we started the debug proxy server
7271
7372
7473exports . getTestTimeout = function ( extra )
@@ -116,7 +115,8 @@ exports.debugScript = function(name, sessionName)
116115 'export XDEBUG_CONFIG="' + 'idekey=' + ( ( serverInfo . idekey ) ?serverInfo . idekey :'' ) + ',session=' + sessionName + '";' ,
117116 "php " + PATH . dirname ( PATH . dirname ( module . id ) ) + "/php/scripts/" + name + ".php"
118117 ] . join ( " " ) , function ( error , stdout , stderr ) {
119- // console.log("[debugScript][stdout] " + stdout);
118+ if ( serverInfo . verbose )
119+ console . log ( "[debugScript][stdout] " + stdout ) ;
120120 if ( stderr )
121121 console . log ( "[debugScript][stderr] " + stderr ) ;
122122 } ) ;
@@ -126,6 +126,7 @@ exports.ready = function(callback)
126126{
127127 // See: https://github.com/chriso/cli/blob/master/examples/static.js
128128 CLI . parse ( {
129+ verbose : [ "v" , 'Log major events to console' , 'boolean' , false ] ,
129130 port : [ false , 'Listen on this port' , 'number' , PROXY_PORT ] ,
130131 php : [ false , 'Hostname for `../php/`' , 'string' , PHP_VHOST ] ,
131132 'skip-browser-tests' : [ false , 'Skip browser tests?' , 'boolean' , false ] ,
@@ -148,6 +149,11 @@ exports.ready = function(callback)
148149 } ) ;
149150}
150151
152+ exports . done = function ( callback )
153+ {
154+ stopServer ( callback ) ;
155+ }
156+
151157exports . fatalExit = function fatalExit ( message )
152158{
153159 UTIL . debug ( "Error: " + message + "\n" ) ;
@@ -194,6 +200,17 @@ function testConnection()
194200 return result . promise ;
195201}
196202
203+ function stopServer ( callback )
204+ {
205+ if ( serverChildInstance === null )
206+ return ;
207+ serverChildInstance . on ( "exit" , function ( )
208+ {
209+ callback ( ) ;
210+ } ) ;
211+ serverChildInstance . kill ( ) ;
212+ }
213+
197214function startServer ( )
198215{
199216 var result = Q . defer ( ) ;
@@ -204,14 +221,12 @@ function startServer()
204221
205222 console . log ( "Starting proxy server: " + command ) ;
206223
207- EXEC ( command , function ( error , stdout , stderr )
224+ serverChildInstance = EXEC ( command , function ( error , stdout , stderr )
208225 {
209- // Ignore (server has stopped after stopServer() was called)
210- // NOTE: The following will only print if there was an error and the server stopped prematurely
211- if ( verboseServerLog )
226+ if ( serverInfo . verbose )
212227 console . error ( "[proxyServer] " + stdout . split ( "\n" ) . join ( "\n[proxyServer] " ) + "\n" ) ;
213228 } ) ;
214-
229+
215230 // Give server 500ms to start up
216231 var counter = 0 ;
217232 var intervalID = setInterval ( function ( )
@@ -241,10 +256,18 @@ function startServer()
241256 }
242257
243258 // Ping server for as long as the test script runs
259+ var pingIntervalID = null ;
244260 Q . when ( result . promise , function ( )
245261 {
246262 ping ( ) ;
247- setInterval ( ping , 300 ) ;
263+ pingIntervalID = setInterval ( ping , 300 ) ;
264+ } ) ;
265+
266+ serverChildInstance . on ( "exit" , function ( )
267+ {
268+ if ( pingIntervalID !== null )
269+ clearInterval ( pingIntervalID ) ;
270+ serverChildInstance = null ;
248271 } ) ;
249272
250273 return result . promise ;
0 commit comments