Skip to content

Commit 87a011d

Browse files
Matt Fickenweltling
authored andcommitted
Fix bug 61769 Random failure of php_cli_server*phpt tests
1 parent ad3a42c commit 87a011d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sapi/cli/tests/php_cli_server.inc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?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);
35

46
function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE) {
57
$php_executable = getenv('TEST_PHP_EXECUTABLE');
@@ -32,6 +34,19 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
3234

3335
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
3436
}
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+
3550

3651
register_shutdown_function(
3752
function($handle) use($router) {
@@ -40,7 +55,11 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
4055
},
4156
$handle
4257
);
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.
4463
}
4564
?>
4665

sapi/cli/tests/php_cli_server_014.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
1313
$port = intval($port)?:80;
1414
$output = '';
1515

16+
// note: select() on Windows (& some other platforms) has historical issues with
17+
// timeouts less than 1000 millis(0.5). it may be better to increase these
18+
// timeouts to 1000 millis(1.0) (fsockopen eventually calls select()).
19+
// see articles like: http://support.microsoft.com/kb/257821
1620
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
1721
if (!$fp) {
1822
die("connect failed");

0 commit comments

Comments
 (0)