1--TEST--
2Bug #69900 Commandline input/output weird behaviour with STDIO
3--CONFLICTS--
4all
5--FILE--
6<?php
7
8error_reporting(E_ALL);
9
10$fl = __DIR__ . DIRECTORY_SEPARATOR . "test69900.php";
11$max_ms = 20;
12
13$test_content = '<?php
14
15$in = fopen("php://stdin", "rb", false, stream_context_create(array("pipe" => array("blocking" => true))));
16
17while(!feof($in)){
18$s = fgets($in);
19    fwrite(STDOUT, $s);
20}
21
22?>';
23file_put_contents($fl, $test_content);
24
25$descriptorspec = array(0 => array("pipe", "r"),1 => array("pipe", "w"));
26$pipes = array();
27
28$process = proc_open(getenv('TEST_PHP_EXECUTABLE_ESCAPED').' -n -f ' . escapeshellarg($fl), $descriptorspec, $pipes, NULL, NULL, array("blocking_pipes" => true));
29
30$moreThanLimit = 0;
31for($i = 0; $i < 10; $i++){
32    fwrite($pipes[0], "hello$i\r\n");
33    fflush($pipes[0]);
34
35    $t0 = microtime(1);
36    $s = fgets($pipes[1]);
37    $t1 = microtime(1);
38
39    echo $s;
40
41    $dt_ms = ($t1 - $t0)*1000;
42    if ($dt_ms > $max_ms) {
43        $moreThanLimit++;
44    }
45}
46
47fclose($pipes[0]);
48fclose($pipes[1]);
49
50proc_close($process);
51
52/* It is expected that the first call takes more than the limit.
53 * Allow two more to account for possible process switch, etc. */
54if ($moreThanLimit > 3) {
55    echo "fgets() took more than $max_ms ms $moreThanLimit times\n";
56}
57
58?>
59--CLEAN--
60<?php
61$fl = __DIR__ . DIRECTORY_SEPARATOR . "test69900.php";
62@unlink($fl);
63?>
64--EXPECT--
65hello0
66hello1
67hello2
68hello3
69hello4
70hello5
71hello6
72hello7
73hello8
74hello9
75