1--TEST--
2Bug #69900 Commandline input/output weird behaviour with STDIO
3--FILE--
4<?php
5
6error_reporting(E_ALL);
7
8$fl = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test69900.php";
9$max_ms = ((bool)getenv('TRAVIS')) ? 5 : 1;
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
28for($i = 0; $i < 10; $i++){
29	fwrite($pipes[0], "hello$i\r\n");
30	fflush($pipes[0]);
31
32	$t0 = microtime(1);
33	$s = fgets($pipes[1]);
34	$t1 = microtime(1);
35
36	echo $s;
37	echo "fgets() took ", (($t1 - $t0)*1000 > $max_ms ? 'more' : 'less'), " than $max_ms ms\n";
38}
39
40fclose($pipes[0]);
41fclose($pipes[1]);
42
43proc_close($process);
44
45?>
46===DONE===
47--CLEAN--
48<?php
49$fl = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test69900.php";
50@unlink($fl);
51?>
52--EXPECTF--
53hello0
54fgets() took more than %d ms
55hello1
56fgets() took less than %d ms
57hello2
58fgets() took less than %d ms
59hello3
60fgets() took less than %d ms
61hello4
62fgets() took less than %d ms
63hello5
64fgets() took less than %d ms
65hello6
66fgets() took less than %d ms
67hello7
68fgets() took less than %d ms
69hello8
70fgets() took less than %d ms
71hello9
72fgets() took less than %d ms
73===DONE===
74