@@ -6,6 +6,7 @@ define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_POR
6
6
function php_cli_server_start ($ code = 'echo "Hello world"; ' , $ router = 'index.php ' , $ cmd_args = null ) {
7
7
$ php_executable = getenv ('TEST_PHP_EXECUTABLE ' );
8
8
$ doc_root = __DIR__ ;
9
+ $ error = null ;
9
10
10
11
if ($ code ) {
11
12
file_put_contents ($ doc_root . '/ ' . ($ router ?: 'index.php ' ), '<?php ' . $ code . ' ?> ' );
@@ -41,33 +42,63 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
41
42
}
42
43
43
44
// note: here we check the process is running
44
- for ($ i =0 ; $ i < 60 ; $ i ++) {
45
- usleep (50000 ); // 50ms per try
45
+ for ($ i =0 ; $ i < 120 ; $ i ++) {
46
46
$ status = proc_get_status ($ handle );
47
47
48
- // Failure, the server is no longer running
49
- if (!($ status && $ status ['running ' ])) {
50
- $ error = "Server is not running \n" ;
48
+ if (!$ status || !$ status ['running ' ]) {
49
+ if ($ status &&
50
+ ($ status ['running ' ] == false && $ status ['exitcode ' ] != 0 )) {
51
+ $ error =
52
+ "Server could not be started \n" ;
53
+ break ;
54
+ }
55
+
56
+ usleep (50000 ); // 50ms per try
57
+ continue ;
58
+ }
59
+
60
+ if ($ status ['signaled ' ]) {
61
+ $ error =
62
+ "Server was terminated with {$ status ['termsig ' ]}\n" ;
51
63
break ;
52
64
}
65
+
66
+ if ($ status ['stopped ' ]) {
67
+ $ error =
68
+ "Server was stopped with {$ status ['stopsig ' ]}\n" ;
69
+ break ;
70
+ }
71
+
72
+ // note: here we check the server is listening, even when the server prints
73
+ // listening on %s:%d
74
+ // it may not be ready to accept connections
75
+ $ start = time ();
76
+
77
+ for ($ try = 0 ; $ try < 120 ; $ try ++) {
78
+ $ error = @fsockopen (
79
+ PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT ) ?
80
+ null :
81
+ sprintf (
82
+ "Server is not accepting connections after %d seconds \n" ,
83
+ time () - $ start );
84
+
85
+ if (!$ error ) {
86
+ break 2 ;
87
+ }
88
+
89
+ usleep (50000 );
90
+ }
91
+
92
+ break ;
53
93
}
54
94
55
95
php_cli_server_start_error:
56
- if (isset ( $ error) ) {
96
+ if ($ error ) {
57
97
echo $ error ;
58
98
proc_terminate ($ handle );
59
99
exit (1 );
60
100
}
61
101
62
- // note: here we check the server is listening, even when the server prints
63
- // listening on %s:%d
64
- // it may not be ready to accept connections
65
- if (!fsockopen (PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT )) {
66
- $ error =
67
- "Server is not accepting connections \n" ;
68
- goto php_cli_server_start_error;
69
- }
70
-
71
102
register_shutdown_function (
72
103
function ($ handle ) use ($ router ) {
73
104
proc_terminate ($ handle );
0 commit comments