]> granicus.if.org Git - php/commitdiff
This test is flaky, and some of it doesn't make sense.
authorJoe Watkins <krakjoe@php.net>
Sat, 15 Jun 2019 07:57:51 +0000 (09:57 +0200)
committerJoe Watkins <krakjoe@php.net>
Sat, 15 Jun 2019 07:57:51 +0000 (09:57 +0200)
I've refactored based on the original bug report, related bugs, and commits
to php-src that were related to them.

It is supposed to be testing windows specific behaviour related to non-blocking pipes,
nevertheless the test runs everywhere.

ext/standard/tests/streams/proc_open_bug60120.phpt

index d12833864ad30ed0e36084f81b346c20c054f898..053de2ebe0588f854de9b13817b1264f671da177 100644 (file)
@@ -1,70 +1,97 @@
 --TEST--
-Bug #60120 proc_open hangs with stdin/out with 2048+ bytes
+Bug #60120 proc_open hangs with stdin/out with >2048 bytes
 --FILE--
 <?php
 error_reporting(E_ALL);
 
-if (substr(PHP_OS, 0, 3) == 'WIN') {
-       $cmd = PHP_BINARY . ' -n -r "fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);"';
-} else {
-       $cmd = PHP_BINARY . ' -n -r \'fwrite(STDOUT, $in = file_get_contents("php://stdin")); fwrite(STDERR, $in);\'';
+$file = preg_replace(
+    "~\.phpt?$~", ".io.php", __FILE__);
+
+file_put_contents($file, <<<TMPFILE
+<?php
+\$input = stream_get_contents(STDIN);
+
+if (\$input) {
+    fwrite(STDOUT, \$input);
+    fwrite(STDERR, \$input);
 }
-$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
-$stdin = str_repeat('*', 1024 * 16) . '!';
-$stdin = str_repeat('*', 2049 );
+?>
+TMPFILE
+);
 
-$options = array_merge(array('suppress_errors' => true, 'bypass_shell' => false));
-$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(), $options);
+$command = sprintf("%s -n %s", PHP_BINARY, $file);
 
-foreach ($pipes as $pipe) {
-    stream_set_blocking($pipe, false);
+$process = proc_open(
+    $command,
+    [
+        ['pipe', 'r'],
+        ['pipe', 'w'],
+        ['pipe', 'w']
+    ],
+    $pipes,
+    getcwd(),
+    [],
+    [
+        'suppress_errors' => true, 
+        'bypass_shell' => false
+    ]
+);
+
+if (!is_resource($process)) {
+    die(sprintf(
+        "could not open process \"%s\"", 
+        $command));
 }
-$writePipes = array($pipes[0]);
-$stdinLen = strlen($stdin);
-$stdinOffset = 0;
 
-unset($pipes[0]);
+fwrite($pipes[0], str_repeat('*', 10000));
+fclose($pipes[0]);
 
-while ($pipes || $writePipes) {
-    $r = $pipes;
-    $w = $writePipes;
-    $e = null;
-    $n = stream_select($r, $w, $e, 60);
+stream_set_blocking($pipes[1], false);
+stream_set_blocking($pipes[2], false);
 
-    if (false === $n) {
-        break;
-    } elseif ($n === 0) {
-        proc_terminate($process);
+$buffers = [
+    1 => "",
+    2 => ""
+];
 
-    }
-    if ($w) {
-        $written = fwrite($writePipes[0], substr($stdin, $stdinOffset), 8192);
-        if (false !== $written) {
-            $stdinOffset += $written;
-        }
-        if ($stdinOffset >= $stdinLen) {
-            fclose($writePipes[0]);
-            $writePipes = null;
+do {
+    $r = [$pipes[1], $pipes[2]];
+    $w = [];
+    $e = [];
+    $s = stream_select($r, $w, $e, 60);
+
+    if (!$s) {
+        if ($s === false) {
+            proc_terminate($process);
         }
+        break;
     }
 
-    foreach ($r as $pipe) {
-        $type = array_search($pipe, $pipes);
-        $data = fread($pipe, 8192);
-        var_dump($data);
-        if (false === $data || feof($pipe)) {
-            fclose($pipe);
-            unset($pipes[$type]);
-        }
+    foreach ($r as $ready) {
+        $buffers[
+            array_search($ready, $pipes)
+        ] .= fread($ready, 8192);
     }
-}
 
+    if (strlen($buffers[1]) === 10000 &&
+        strlen($buffers[2]) === 10000) {
+        break;
+    }
+} while (1);
+
+var_dump(
+    $buffers[1],
+    $buffers[2],
+    fread($pipes[1], 1),
+    fread($pipes[2], 1));
 
+fclose($pipes[1]);
+fclose($pipes[2]);
 ?>
-===DONE===
 --EXPECTF--
-string(2049) "%s"
-string(2049) "%s"
+string(10000) "%s"
+string(10000) "%s"
 string(0) ""
 string(0) ""
-===DONE===
+--CLEAN--
+unlink($file);