]> granicus.if.org Git - php/commitdiff
refactor a little more to add some more useful error messages and raise the limits...
authorJoe Watkins <krakjoe@php.net>
Wed, 19 Jun 2019 09:41:24 +0000 (11:41 +0200)
committerJoe Watkins <krakjoe@php.net>
Wed, 19 Jun 2019 09:41:24 +0000 (11:41 +0200)
sapi/cli/tests/php_cli_server.inc

index 1a02e7e0ff64cf3433fca5e9f0d51ddca993d24f..1f7091be5c577b45f5c73dce57768d8f5f64d191 100644 (file)
@@ -6,6 +6,7 @@ define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_POR
 function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.php', $cmd_args = null) {
        $php_executable = getenv('TEST_PHP_EXECUTABLE');
        $doc_root = __DIR__;
+    $error = null;
 
        if ($code) {
                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
        }
 
     // note: here we check the process is running
-    for ($i=0; $i < 60; $i++) {
-        usleep(50000); // 50ms per try
+    for ($i=0; $i < 120; $i++) {
         $status = proc_get_status($handle);
 
-        // Failure, the server is no longer running
-        if (!($status && $status['running'])) {
-            $error = "Server is not running\n";
+        if (!$status || !$status['running']) {
+            if ($status &&
+               ($status['running'] == false && $status['exitcode'] != 0)) {
+                $error =
+                    "Server could not be started\n";
+                break;
+            }
+
+            usleep(50000); // 50ms per try
+            continue;
+        }
+
+        if ($status['signaled']) {
+            $error =
+                "Server was terminated with {$status['termsig']}\n";
             break;
         }
+
+        if ($status['stopped']) {
+            $error =
+                "Server was stopped with {$status['stopsig']}\n";
+            break;
+        }
+
+        // note: here we check the server is listening, even when the server prints
+        //          listening on %s:%d
+        //       it may not be ready to accept connections
+        $start = time();
+
+        for ($try = 0; $try < 120; $try++) {
+            $error = @fsockopen(
+                        PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT) ?
+                            null :
+                            sprintf(
+                                "Server is not accepting connections after %d seconds\n",
+                                time() - $start);
+
+            if (!$error) {
+                break 2;
+            }
+
+            usleep(50000);
+        }
+
+        break;
     }
 
 php_cli_server_start_error:
-    if (isset($error)) {
+    if ($error) {
         echo $error;
         proc_terminate($handle);
         exit(1);
     }
 
-    // note: here we check the server is listening, even when the server prints
-    //          listening on %s:%d
-    //       it may not be ready to accept connections
-    if (!fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT)) {
-        $error =
-            "Server is not accepting connections\n";
-        goto php_cli_server_start_error;
-    }
-
     register_shutdown_function(
         function($handle) use($router) {
             proc_terminate($handle);