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