1
1
<?php
2
- define ("PHP_CLI_SERVER_ADDRESS " , "localhost:8964 " );
2
+ define ("PHP_CLI_SERVER_HOSTNAME " , "localhost " );
3
+ define ("PHP_CLI_SERVER_PORT " , 8964 );
4
+ define ("PHP_CLI_SERVER_ADDRESS " , PHP_CLI_SERVER_HOSTNAME .": " .PHP_CLI_SERVER_PORT );
3
5
4
6
function php_cli_server_start ($ code = 'echo "Hello world"; ' , $ no_router = FALSE ) {
5
7
$ php_executable = getenv ('TEST_PHP_EXECUTABLE ' );
@@ -32,6 +34,19 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
32
34
33
35
$ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
34
36
}
37
+
38
+ // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
39
+ // it might not be listening yet...need to wait until fsockopen() call returns
40
+ $ fp = fsockopen (PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT );
41
+ if ($ fp ) {
42
+ // server will report Unexpected EOF error (socket opened, closed without writing
43
+ // anything) but that can be ignored
44
+ fclose ($ fp );
45
+ } else {
46
+ // test will fail to connect if server doesn't start listening/accepting
47
+ // in the next few microseconds
48
+ }
49
+
35
50
36
51
register_shutdown_function (
37
52
function ($ handle ) use ($ router ) {
@@ -40,7 +55,11 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
40
55
},
41
56
$ handle
42
57
);
43
- usleep (50000 );
58
+ // don't bother sleeping, server is already up
59
+ //usleep(50000);
60
+ // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
61
+ // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
62
+ // sleeping doesn't work.
44
63
}
45
64
?>
46
65
0 commit comments