Skip to content

Commit 2cf4f98

Browse files
author
Shein Alexey
committed
Refactored and speeded up php_cli_server tests, also get rid of test hanging on ubuntu:
1) Prepended starting the server with "exec" so proc_terminate can correctly close it (see also this note http://www.php.net/manual/en/function.proc-get-status.php#93382 for details) 2) Moved putting down the server to the shutdown function to make it independent from tests (fatal) errors 3) Moved php cli executable into the function to make tests more readable 4) changed sleep(1) to usleep(50000) (50 ms) to make tests faster - this needs more testing and if timeout is too small should be increased
1 parent 0d74e0f commit 2cf4f98

4 files changed

Lines changed: 15 additions & 20 deletions

File tree

sapi/cli/tests/php_cli_server.inc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
define ("PHP_CLI_SERVER_ADDRESS", "localhost:8964");
33

4-
function php_cli_server_start($php_executable, $code = 'echo "Hello world";') {
4+
function php_cli_server_start($code = 'echo "Hello world";') {
5+
$php_executable = getenv('TEST_PHP_EXECUTABLE');
56
$doc_root = __DIR__;
67
$router = "router.php";
78
file_put_contents($doc_root . '/' . $router, '<?php ' . $code . ' ?>');
@@ -12,18 +13,18 @@ function php_cli_server_start($php_executable, $code = 'echo "Hello world";') {
1213
2 => STDERR,
1314
);
1415

15-
$cmd = "{$php_executable} -t {$doc_root} -S " . PHP_CLI_SERVER_ADDRESS . " {$router}";
16+
$cmd = "exec {$php_executable} -t {$doc_root} -S " . PHP_CLI_SERVER_ADDRESS . " {$router}";
1617

1718
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
18-
sleep(1);
1919

20-
return $handle;
21-
}
20+
register_shutdown_function(
21+
function($handle) {
22+
proc_terminate($handle);
23+
@unlink(__DIR__ . "/router.php");
24+
},
25+
$handle
26+
);
2227

23-
function php_cli_server_shutdown($handle) {
24-
proc_terminate($handle);
25-
proc_close($handle);
26-
@unlink(__DIR__ . "router.php");
27-
return true;
28+
usleep(50000);
2829
}
2930
?>

sapi/cli/tests/php_cli_server_001.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
1212
--FILE--
1313
<?php
1414
include "php_cli_server.inc";
15-
$php = getenv('TEST_PHP_EXECUTABLE');
16-
$handle = php_cli_server_start($php);
15+
php_cli_server_start();
1716
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS));
18-
php_cli_server_shutdown($handle);
1917
?>
20-
--EXPECT--
18+
--EXPECT--
2119
string(11) "Hello world"

sapi/cli/tests/php_cli_server_002.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
1212
--FILE--
1313
<?php
1414
include "php_cli_server.inc";
15-
$php = getenv('TEST_PHP_EXECUTABLE');
16-
$handle = php_cli_server_start($php, 'var_dump($_SERVER["DOCUMENT_ROOT"], $_SERVER["SERVER_SOFTWARE"]);');
15+
php_cli_server_start('var_dump($_SERVER["DOCUMENT_ROOT"], $_SERVER["SERVER_SOFTWARE"]);');
1716
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS));
18-
php_cli_server_shutdown($handle);
1917
?>
2018
--EXPECTF--
2119
string(%d) "string(%d) "%s/tests"

sapi/cli/tests/php_cli_server_003.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
1212
--FILE--
1313
<?php
1414
include "php_cli_server.inc";
15-
$php = getenv('TEST_PHP_EXECUTABLE');
16-
$handle = php_cli_server_start($php, 'chdir("/tmp"); echo "okey";');
15+
php_cli_server_start('chdir("/tmp"); echo "okey";');
1716
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS));
1817
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS));
19-
php_cli_server_shutdown($handle);
2018
?>
2119
--EXPECTF--
2220
string(4) "okey"

0 commit comments

Comments
 (0)