@@ -20,28 +20,43 @@ function php_cli_server_start($ini = "") {
20
20
$ cmd = "exec {$ php_executable } -t {$ doc_root } $ ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null " ;
21
21
$ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
22
22
}
23
-
23
+
24
24
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
25
25
// it might not be listening yet...need to wait until fsockopen() call returns
26
- $ i = 0 ;
27
- while (($ i ++ < 30 ) && !($ fp = @fsockopen (PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT ))) {
28
- usleep (10000 );
29
- }
26
+ $ error = "Unable to connect to servers \n" ;
27
+ for ($ i =0 ; $ i < 60 ; $ i ++) {
28
+ usleep (25000 ); // 25ms per try
29
+ $ status = proc_get_status ($ handle );
30
+ $ fp = @fsockopen (PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT );
31
+ // Failure, the server is no longer running
32
+ if (!($ status && $ status ['running ' ])) {
33
+ $ error = "Server is not running \n" ;
34
+ break ;
35
+ }
36
+ // Success, Connected to servers
37
+ if ($ fp ) {
38
+ $ error = '' ;
39
+ break ;
40
+ }
41
+ }
30
42
31
- if ($ fp ) {
32
- fclose ($ fp );
33
- }
43
+ if ($ fp ) {
44
+ fclose ($ fp );
45
+ }
46
+
47
+ if ($ error ) {
48
+ echo $ error ;
49
+ proc_terminate ($ handle );
50
+ exit (1 );
51
+ }
34
52
35
53
register_shutdown_function (
36
54
function ($ handle ) {
37
55
proc_terminate ($ handle );
38
56
},
39
- $ handle
40
- );
41
- // don't bother sleeping, server is already up
42
- // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
43
- // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
44
- // sleeping doesn't work.
57
+ $ handle
58
+ );
59
+
45
60
}
46
61
?>
47
62
0 commit comments