Skip to content

Commit eda5d8a

Browse files
committed
refactor a little more to add some more useful error messages and raise the limits on waiting for slow machines
1 parent d6480fa commit eda5d8a

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

sapi/cli/tests/php_cli_server.inc

+46-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_POR
66
function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.php', $cmd_args = null) {
77
$php_executable = getenv('TEST_PHP_EXECUTABLE');
88
$doc_root = __DIR__;
9+
$error = null;
910

1011
if ($code) {
1112
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
4142
}
4243

4344
// 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++) {
4646
$status = proc_get_status($handle);
4747

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";
5163
break;
5264
}
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;
5393
}
5494

5595
php_cli_server_start_error:
56-
if (isset($error)) {
96+
if ($error) {
5797
echo $error;
5898
proc_terminate($handle);
5999
exit(1);
60100
}
61101

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-
71102
register_shutdown_function(
72103
function($handle) use($router) {
73104
proc_terminate($handle);

0 commit comments

Comments
 (0)