]> granicus.if.org Git - php/commitdiff
Makes the sapi web server and curl tests more reliable
authorMitch Hagstrand <mhagstrand@gmail.com>
Fri, 23 Dec 2016 20:07:47 +0000 (12:07 -0800)
committerNikita Popov <nikic@php.net>
Tue, 27 Dec 2016 20:52:05 +0000 (21:52 +0100)
1. Increased the time for the sapi/cli/tests/php_cli_server.inc to accept connections.
2. sapi/cli/tests/php_cli_server.inc has errors messages and a `stop` function.
3. bug67429.phpt uses the `stop` function to shutdown the webserver before starting a new one.
4. Removed ext/curl/tests/bug48203_multi-win32.phpt test now that ext/curl/tests/bug48203_multi.phpt
   runs on Windows also.

ext/curl/tests/bug48203_multi-win32.phpt [deleted file]
ext/curl/tests/bug48203_multi.phpt
ext/curl/tests/server.inc
sapi/cli/tests/bug67429.phpt
sapi/cli/tests/php_cli_server.inc

diff --git a/ext/curl/tests/bug48203_multi-win32.phpt b/ext/curl/tests/bug48203_multi-win32.phpt
deleted file mode 100644 (file)
index 9016725..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
---TEST--
-Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to curl are closed before calling curl_multi_exec)
---SKIPIF--
-<?php
-include 'skipif.inc';
-if(substr(PHP_OS, 0, 3) != 'WIN' ) {
-    die('skip Windows only test');
-}
-?>
---FILE--
-<?php
-include 'server.inc';
-function checkForClosedFilePointer($curl_option, $description) {
-       $fp = fopen(dirname(__FILE__) . '/bug48203.tmp', 'w');
-
-       $ch1 = curl_init();
-       $ch2 = curl_init();
-
-       $options = array(
-               CURLOPT_RETURNTRANSFER => 1,
-               $curl_option => $fp,
-               CURLOPT_URL => curl_cli_server_start()
-       );
-
-       // we also need to set CURLOPT_VERBOSE to test CURLOPT_STDERR properly
-       if (CURLOPT_STDERR == $curl_option) {
-               $options[CURLOPT_VERBOSE] = 1;
-       }
-
-       if (CURLOPT_INFILE == $curl_option) {
-           $options[CURLOPT_UPLOAD] = 1;
-       }
-
-       curl_setopt_array($ch1, $options);
-       curl_setopt_array($ch2, $options);
-
-       fclose($fp); // <-- premature close of $fp caused a crash!
-
-       $mh = curl_multi_init();
-
-       curl_multi_add_handle($mh, $ch1);
-       curl_multi_add_handle($mh, $ch2);
-
-       $active = 0;
-       do {
-               curl_multi_exec($mh, $active);
-       } while ($active > 0);
-
-       curl_multi_remove_handle($mh, $ch1);
-       curl_multi_remove_handle($mh, $ch2);
-       curl_multi_close($mh);
-
-       echo "Ok for $description\n";
-}
-
-$options_to_check = array(
-       "CURLOPT_STDERR", "CURLOPT_WRITEHEADER", "CURLOPT_FILE", "CURLOPT_INFILE"
-);
-
-foreach($options_to_check as $option) {
-       checkForClosedFilePointer(constant($option), $option);
-}
-
-?>
---CLEAN--
-<?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
---EXPECTF--
-Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
-
-Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
-Ok for CURLOPT_STDERR
-%A
-
-Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
-
-Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
-Ok for CURLOPT_WRITEHEADER
-
-Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
-
-Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
-Hello World!
-Hello World!Hello World!
-Hello World!Ok for CURLOPT_FILE
-
-Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
-
-Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
-Ok for CURLOPT_INFILE
index ed09ed02ce90332bf1e5f193c065e1e33f06e74f..5f9e2ba6b212def577f910f5608cbf3a86e05807 100644 (file)
@@ -3,9 +3,6 @@ Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to
 --SKIPIF--
 <?php
 include 'skipif.inc';
-if(substr(PHP_OS, 0, 3) == 'WIN' ) {
-    die('skip not for Windows');
-}
 ?>
 --FILE--
 <?php
@@ -50,6 +47,10 @@ function checkForClosedFilePointer($target_url, $curl_option, $description) {
        curl_multi_remove_handle($mh, $ch2);
        curl_multi_close($mh);
 
+       // Force curl to output results
+       fflush(STDERR);
+       fflush(STDOUT);
+
        echo "Ok for $description\n";
 }
 
@@ -66,24 +67,25 @@ foreach($options_to_check as $option) {
 --CLEAN--
 <?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
 --EXPECTF--
-Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
 
-Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
 %A
 Ok for CURLOPT_STDERR
-%A
-Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %sbug48203_multi.php on line 36
 
-Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
+
+Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
 Ok for CURLOPT_WRITEHEADER
 
-Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
 
-Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
-%AOk for CURLOPT_FILE
+Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
+Hello World!
+Hello World!Hello World!
+Hello World!Ok for CURLOPT_FILE
 
-Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
 
-Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
+Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
 Ok for CURLOPT_INFILE
-
index 62f9a2d30ce9817c463c3e9e30751a98d46bc906..315fd68cc4775b9e538bf5ffbc822428a7f10232 100644 (file)
@@ -56,7 +56,7 @@ function curl_cli_server_start() {
 
     if ($error) {
         echo $error;
-        proc_close($handle);
+        proc_terminate($handle);
         exit(1);
     }
 
@@ -65,7 +65,7 @@ function curl_cli_server_start() {
             proc_terminate($handle);
         },
         $handle
-               );
+    );
 
     return PHP_CURL_SERVER_ADDRESS;
 }
index 856946b29dac81fd16771009b075d7a100f8ea3a..1c861be899e5c986abe94f1136881ca1d5279225 100644 (file)
@@ -8,8 +8,9 @@ include "skipif.inc";
 <?php
 include "php_cli_server.inc";
 
+// This creates a new server for each response code
 foreach ([308, 426] as $code) {
-  php_cli_server_start(<<<PHP
+  $proc_handle = php_cli_server_start(<<<PHP
 http_response_code($code);
 PHP
   );
@@ -34,6 +35,10 @@ HEADER
   }
 
   fclose($fp);
+  // Shutdown the servers or another server may not be able to start
+  // because of the this server still being bound to the port
+
+  php_cli_server_stop($proc_handle);
 }
 ?>
 --EXPECTF--
index 6b1e90c4dc16b91b64d3c0f51addb3825b82e4f7..6e7b70cb60a535a36c306d4e407a950b8d270242 100644 (file)
@@ -33,29 +33,62 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
 
                $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
        }
-       
-       // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
-       //       it might not be listening yet...need to wait until fsockopen() call returns
-    $i = 0;
-    while (($i++ < 30) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
-        usleep(10000);
+
+    // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
+    //       it might not be listening yet...need to wait until fsockopen() call returns
+    $error = "Unable to connect to servers\n";
+    for ($i=0; $i < 60; $i++) {
+        usleep(25000); // 25ms per try
+        $status = proc_get_status($handle);
+        $fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT);
+        // Failure, the server is no longer running
+        if (!($status && $status['running'])) {
+            $error = "Server is not running\n";
+            break;
+        }
+        // Success, Connected to servers
+        if ($fp) {
+            $error = '';
+            break;
+        }
     }
 
     if ($fp) {
         fclose($fp);
     }
 
-       register_shutdown_function(
-               function($handle) use($router) {
-                       proc_terminate($handle);
-                       @unlink(__DIR__ . "/{$router}");
-               },
-                       $handle
-               );
-       // don't bother sleeping, server is already up
-       // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
-       // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
-       // sleeping doesn't work.
+    if ($error) {
+        echo $error;
+        proc_terminate($handle);
+        exit(1);
+    }
+
+    register_shutdown_function(
+        function($handle) use($router) {
+            proc_terminate($handle);
+            @unlink(__DIR__ . "/{$router}");
+        },
+        $handle
+    );
+
+    return $handle;
+}
+
+function php_cli_server_stop($handle) {
+    $success = FALSE;
+    if ($handle) {
+        proc_terminate($handle);
+        /* Wait for server to shutdown */
+        for ($i = 0; $i < 60; $i++) {
+            $status = proc_get_status($handle);
+            if (!($status && $status['running'])) {
+                $success = TRUE;
+                break;
+            }
+            usleep(25000);
+        }
+    }
+    return $success;
 }
 ?>