Skip to content

Commit 5773780

Browse files
committed
Make Opcache tests using the cli server more reliable
Same fix applied to ext/curl/tests/server.inc and sapi/cli/tests/php_cli_server.inc 1. Increases the amount of time for the PHP built-in server to accept a connection 2. Outputs an error if the PHP built-in server fails
1 parent e2a159f commit 5773780

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

ext/opcache/tests/php_cli_server.inc

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,60 @@ define ("PHP_CLI_SERVER_PORT", 8964);
44
define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
55

66
function php_cli_server_start($ini = "") {
7-
$php_executable = getenv('TEST_PHP_EXECUTABLE');
8-
$doc_root = __DIR__;
9-
10-
$descriptorspec = array(
11-
0 => STDIN,
12-
1 => STDOUT,
13-
2 => STDERR,
14-
);
15-
16-
if (substr(PHP_OS, 0, 3) == 'WIN') {
17-
$cmd = "{$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS;
18-
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
19-
} else {
20-
$cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
21-
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
22-
}
23-
24-
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
25-
// it might not be listening yet...need to wait until fsockopen() call returns
26-
$i = 0;
27-
while (($i++ < 30) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
28-
usleep(10000);
7+
$php_executable = getenv('TEST_PHP_EXECUTABLE');
8+
$doc_root = __DIR__;
9+
10+
$descriptorspec = array(
11+
0 => STDIN,
12+
1 => STDOUT,
13+
2 => STDERR,
14+
);
15+
16+
if (substr(PHP_OS, 0, 3) == 'WIN') {
17+
$cmd = "{$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS;
18+
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
19+
} else {
20+
$cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
21+
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
22+
}
23+
24+
25+
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
26+
// it might not be listening yet...need to wait until fsockopen() call returns
27+
$error = "Unable to connect to servers\n";
28+
for ($i=0; $i < 60; $i++) {
29+
usleep(25000); // 25ms per try
30+
$status = proc_get_status($handle);
31+
$fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT);
32+
// Failure, the server is no longer running
33+
if (!($status && $status['running'])) {
34+
$error = "Server is not running\n";
35+
break;
36+
}
37+
// Success, Connected to servers
38+
if ($fp) {
39+
$error = '';
40+
break;
41+
}
2942
}
3043

3144
if ($fp) {
3245
fclose($fp);
3346
}
3447

35-
register_shutdown_function(
36-
function($handle) {
37-
proc_terminate($handle);
38-
},
39-
$handle
40-
);
41-
// don't bother sleeping, server is already up
42-
// server can take a variable amount of time to be up, so just sleeping a guessed amount of time
43-
// does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
44-
// sleeping doesn't work.
48+
if ($error) {
49+
echo $error;
50+
proc_terminate($handle);
51+
exit(1);
52+
}
53+
54+
register_shutdown_function(
55+
function($handle) {
56+
proc_terminate($handle);
57+
},
58+
$handle
59+
);
60+
4561
}
4662
?>
4763

0 commit comments

Comments
 (0)