Skip to content

Commit d0fcc74

Browse files
mhagstrandnikic
authored andcommitted
Make Opcache tests using the cli server more reliable
Same fix already 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 88d1a2c commit d0fcc74

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

ext/opcache/tests/php_cli_server.inc

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,43 @@ function php_cli_server_start($ini = "") {
2020
$cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
2121
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
2222
}
23-
23+
2424
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
2525
// 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);
29-
}
26+
$error = "Unable to connect to servers\n";
27+
for ($i=0; $i < 60; $i++) {
28+
usleep(25000); // 25ms per try
29+
$status = proc_get_status($handle);
30+
$fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT);
31+
// Failure, the server is no longer running
32+
if (!($status && $status['running'])) {
33+
$error = "Server is not running\n";
34+
break;
35+
}
36+
// Success, Connected to servers
37+
if ($fp) {
38+
$error = '';
39+
break;
40+
}
41+
}
3042

31-
if ($fp) {
32-
fclose($fp);
33-
}
43+
if ($fp) {
44+
fclose($fp);
45+
}
46+
47+
if ($error) {
48+
echo $error;
49+
proc_terminate($handle);
50+
exit(1);
51+
}
3452

3553
register_shutdown_function(
3654
function($handle) {
3755
proc_terminate($handle);
3856
},
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.
57+
$handle
58+
);
59+
4560
}
4661
?>
4762

0 commit comments

Comments
 (0)