Skip to content

Commit 92678d1

Browse files
mhagstrandnikic
authored andcommitted
Fixes the curl tests to be more reliable in Travis CI
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 3. In bug48203_multi.phpt the test no longer starts and stops multiple PHP built-in servers
1 parent 7f29e7c commit 92678d1

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

ext/curl/tests/bug48203_multi.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if(substr(PHP_OS, 0, 3) == 'WIN' ) {
1010
--FILE--
1111
<?php
1212
include 'server.inc';
13-
function checkForClosedFilePointer($curl_option, $description) {
13+
function checkForClosedFilePointer($target_url, $curl_option, $description) {
1414
$fp = fopen(dirname(__FILE__) . '/bug48203.tmp', 'w');
1515

1616
$ch1 = curl_init();
@@ -19,7 +19,7 @@ function checkForClosedFilePointer($curl_option, $description) {
1919
$options = array(
2020
CURLOPT_RETURNTRANSFER => 1,
2121
$curl_option => $fp,
22-
CURLOPT_URL => curl_cli_server_start()
22+
CURLOPT_URL => $target_url,
2323
);
2424

2525
// we also need to set CURLOPT_VERBOSE to test CURLOPT_STDERR properly
@@ -57,8 +57,9 @@ $options_to_check = array(
5757
"CURLOPT_STDERR", "CURLOPT_WRITEHEADER", "CURLOPT_FILE", "CURLOPT_INFILE"
5858
);
5959

60+
$target_url = curl_cli_server_start();
6061
foreach($options_to_check as $option) {
61-
checkForClosedFilePointer(constant($option), $option);
62+
checkForClosedFilePointer($target_url, constant($option), $option);
6263
}
6364

6465
?>
@@ -85,3 +86,4 @@ Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to
8586

8687
Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %sbug48203_multi.php on line 36
8788
Ok for CURLOPT_INFILE
89+

ext/curl/tests/server.inc

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,64 @@ function curl_cli_server_start() {
99
return getenv('PHP_CURL_HTTP_REMOTE_SERVER');
1010
}
1111

12-
$php_executable = getenv('TEST_PHP_EXECUTABLE');
13-
$doc_root = __DIR__;
14-
$router = "responder/get.php";
15-
16-
$descriptorspec = array(
17-
0 => STDIN,
18-
1 => STDOUT,
19-
2 => STDERR,
20-
);
21-
22-
if (substr(PHP_OS, 0, 3) == 'WIN') {
23-
$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
12+
$php_executable = getenv('TEST_PHP_EXECUTABLE');
13+
$doc_root = __DIR__;
14+
$router = "responder/get.php";
15+
16+
$descriptorspec = array(
17+
0 => STDIN,
18+
1 => STDOUT,
19+
2 => STDERR,
20+
);
21+
22+
if (substr(PHP_OS, 0, 3) == 'WIN') {
23+
$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
24+
$cmd .= " {$router}";
25+
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
26+
} else {
27+
$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
2428
$cmd .= " {$router}";
25-
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
26-
} else {
27-
$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
28-
$cmd .= " {$router}";
29-
$cmd .= " 2>/dev/null";
30-
31-
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
32-
}
29+
$cmd .= " 2>/dev/null";
30+
31+
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
32+
}
3333

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

4153
if ($fp) {
4254
fclose($fp);
4355
}
4456

45-
register_shutdown_function(
46-
function($handle) use($router) {
47-
proc_terminate($handle);
48-
},
49-
$handle
57+
if ($error) {
58+
echo $error;
59+
proc_close($handle);
60+
exit(1);
61+
}
62+
63+
register_shutdown_function(
64+
function($handle) use($router) {
65+
proc_terminate($handle);
66+
},
67+
$handle
5068
);
51-
// don't bother sleeping, server is already up
52-
// server can take a variable amount of time to be up, so just sleeping a guessed amount of time
53-
// does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
54-
// sleeping doesn't work.
69+
5570
return PHP_CURL_SERVER_ADDRESS;
5671
}
72+

0 commit comments

Comments
 (0)