Skip to content

Commit 2d51267

Browse files
mhagstrandnikic
authored andcommitted
Makes the sapi web server and curl tests more reliable
1. Increased the time for the sapi/cli/tests/php_cli_server.inc to accept connections. 2. sapi/cli/tests/php_cli_server.inc has errors messages and a `stop` function. 3. bug67429.phpt uses the `stop` function to shutdown the webserver before starting a new one. 4. Removed ext/curl/tests/bug48203_multi-win32.phpt test now that ext/curl/tests/bug48203_multi.phpt runs on Windows also.
1 parent 92678d1 commit 2d51267

File tree

5 files changed

+74
-123
lines changed

5 files changed

+74
-123
lines changed

ext/curl/tests/bug48203_multi-win32.phpt

Lines changed: 0 additions & 89 deletions
This file was deleted.

ext/curl/tests/bug48203_multi.phpt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to
33
--SKIPIF--
44
<?php
55
include 'skipif.inc';
6-
if(substr(PHP_OS, 0, 3) == 'WIN' ) {
7-
die('skip not for Windows');
8-
}
96
?>
107
--FILE--
118
<?php
@@ -50,6 +47,10 @@ function checkForClosedFilePointer($target_url, $curl_option, $description) {
5047
curl_multi_remove_handle($mh, $ch2);
5148
curl_multi_close($mh);
5249

50+
// Force curl to output results
51+
fflush(STDERR);
52+
fflush(STDOUT);
53+
5354
echo "Ok for $description\n";
5455
}
5556

@@ -66,24 +67,25 @@ foreach($options_to_check as $option) {
6667
--CLEAN--
6768
<?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
6869
--EXPECTF--
69-
Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %sbug48203_multi.php on line 36
70+
Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
7071

71-
Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %sbug48203_multi.php on line 36
72+
Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
7273
%A
7374
Ok for CURLOPT_STDERR
74-
%A
75-
Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %sbug48203_multi.php on line 36
7675

77-
Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %sbug48203_multi.php on line 36
76+
Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
77+
78+
Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
7879
Ok for CURLOPT_WRITEHEADER
7980

80-
Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
81+
Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
8182

82-
Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
83-
%AOk for CURLOPT_FILE
83+
Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
84+
Hello World!
85+
Hello World!Hello World!
86+
Hello World!Ok for CURLOPT_FILE
8487

85-
Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
88+
Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
8689

87-
Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
90+
Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
8891
Ok for CURLOPT_INFILE
89-

ext/curl/tests/server.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function curl_cli_server_start() {
5656

5757
if ($error) {
5858
echo $error;
59-
proc_close($handle);
59+
proc_terminate($handle);
6060
exit(1);
6161
}
6262

@@ -65,7 +65,7 @@ function curl_cli_server_start() {
6565
proc_terminate($handle);
6666
},
6767
$handle
68-
);
68+
);
6969

7070
return PHP_CURL_SERVER_ADDRESS;
7171
}

sapi/cli/tests/bug67429.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ include "skipif.inc";
88
<?php
99
include "php_cli_server.inc";
1010

11+
// This creates a new server for each response code
1112
foreach ([308, 426] as $code) {
12-
php_cli_server_start(<<<PHP
13+
$proc_handle = php_cli_server_start(<<<PHP
1314
http_response_code($code);
1415
PHP
1516
);
@@ -34,6 +35,10 @@ HEADER
3435
}
3536

3637
fclose($fp);
38+
// Shutdown the servers or another server may not be able to start
39+
// because of the this server still being bound to the port
40+
41+
php_cli_server_stop($proc_handle);
3742
}
3843
?>
3944
--EXPECTF--

sapi/cli/tests/php_cli_server.inc

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,62 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
3333

3434
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
3535
}
36-
37-
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
38-
// it might not be listening yet...need to wait until fsockopen() call returns
39-
$i = 0;
40-
while (($i++ < 30) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
41-
usleep(10000);
36+
37+
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
38+
// it might not be listening yet...need to wait until fsockopen() call returns
39+
$error = "Unable to connect to servers\n";
40+
for ($i=0; $i < 60; $i++) {
41+
usleep(25000); // 25ms per try
42+
$status = proc_get_status($handle);
43+
$fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT);
44+
// Failure, the server is no longer running
45+
if (!($status && $status['running'])) {
46+
$error = "Server is not running\n";
47+
break;
48+
}
49+
// Success, Connected to servers
50+
if ($fp) {
51+
$error = '';
52+
break;
53+
}
4254
}
4355

4456
if ($fp) {
4557
fclose($fp);
4658
}
4759

48-
register_shutdown_function(
49-
function($handle) use($router) {
50-
proc_terminate($handle);
51-
@unlink(__DIR__ . "/{$router}");
52-
},
53-
$handle
54-
);
55-
// don't bother sleeping, server is already up
56-
// server can take a variable amount of time to be up, so just sleeping a guessed amount of time
57-
// does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
58-
// sleeping doesn't work.
60+
if ($error) {
61+
echo $error;
62+
proc_terminate($handle);
63+
exit(1);
64+
}
65+
66+
register_shutdown_function(
67+
function($handle) use($router) {
68+
proc_terminate($handle);
69+
@unlink(__DIR__ . "/{$router}");
70+
},
71+
$handle
72+
);
73+
74+
return $handle;
75+
}
76+
77+
function php_cli_server_stop($handle) {
78+
$success = FALSE;
79+
if ($handle) {
80+
proc_terminate($handle);
81+
/* Wait for server to shutdown */
82+
for ($i = 0; $i < 60; $i++) {
83+
$status = proc_get_status($handle);
84+
if (!($status && $status['running'])) {
85+
$success = TRUE;
86+
break;
87+
}
88+
usleep(25000);
89+
}
90+
}
91+
return $success;
5992
}
6093
?>
6194

0 commit comments

Comments
 (0)