Skip to content

Commit 8259505

Browse files
Tests: Use skipTestOnTimeout() in more HTTP tests.
Adjust it to handle more types of timeouts, e.g. "Resolving timed out", "Connection timed out". Merges [43511], [43512], [46682], [46996] to the 4.7 branch. See #51669. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@50088 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e972b67 commit 8259505

File tree

4 files changed

+261
-186
lines changed

4 files changed

+261
-186
lines changed

tests/phpunit/includes/testcase.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ function clean_up_global_scope() {
175175
$_POST = array();
176176
self::flush_cache();
177177
}
178-
179-
/**
180-
* Allow tests to be skipped on some automated runs
181-
*
182-
* For test runs on Travis for something other than trunk/master
178+
179+
/**
180+
* Allow tests to be skipped on some automated runs.
181+
*
182+
* For test runs on Travis for something other than trunk/master
183183
* we want to skip tests that only need to run for master.
184184
*/
185185
public function skipOnAutomatedBranches() {
@@ -231,8 +231,31 @@ protected function reset_taxonomies() {
231231
protected function reset_post_statuses() {
232232
foreach ( get_post_stati( array( '_builtin' => false ) ) as $post_status ) {
233233
_unregister_post_status( $post_status );
234-
}
235-
}
234+
}
235+
}
236+
237+
/**
238+
* Allow tests to be skipped if the HTTP request times out.
239+
*
240+
* @param array|WP_Error $response HTTP response.
241+
*/
242+
public function skipTestOnTimeout( $response ) {
243+
if ( ! is_wp_error( $response ) ) {
244+
return;
245+
}
246+
if ( 'connect() timed out!' === $response->get_error_message() ) {
247+
$this->markTestSkipped( 'HTTP timeout' );
248+
}
249+
250+
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
251+
$this->markTestSkipped( 'HTTP timeout' );
252+
}
253+
254+
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
255+
$this->markTestSkipped( 'HTTP timeout' );
256+
}
257+
258+
}
236259

237260
/**
238261
* Reset `$_SERVER` variables

tests/phpunit/tests/external-http/basic.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ function test_readme() {
99
$this->skipOnAutomatedBranches();
1010

1111
$readme = file_get_contents( ABSPATH . 'readme.html' );
12+
1213
preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );
1314

1415
$response = wp_remote_get( 'https://secure.php.net/supported-versions.php' );
15-
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
16-
$this->markTestSkipped( 'Could not contact PHP.net to check versions.' );
16+
17+
$this->skipTestOnTimeout( $response );
18+
19+
$response_code = wp_remote_retrieve_response_code( $response );
20+
if ( 200 !== $response_code ) {
21+
$this->fail( sprintf( 'Could not contact PHP.net to check versions. Response code: %s', $response_code ) );
1722
}
23+
1824
$php = wp_remote_retrieve_body( $response );
1925

2026
preg_match_all( '#<tr class="stable">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $php, $phpmatches );
@@ -24,9 +30,14 @@ function test_readme() {
2430
preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
2531

2632
$response = wp_remote_get( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );
27-
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
28-
$this->markTestSkipped( 'Could not contact dev.MySQL.com to check versions.' );
33+
34+
$this->skipTestOnTimeout( $response );
35+
36+
$response_code = wp_remote_retrieve_response_code( $response );
37+
if ( 200 !== $response_code ) {
38+
$this->fail( sprintf( 'Could not contact dev.MySQL.com to check versions. Response code: %s', $response_code ) );
2939
}
40+
3041
$mysql = wp_remote_retrieve_body( $response );
3142

3243
preg_match( '#(\d{4}-\d{2}-\d{2}), General Availability#', $mysql, $mysqlmatches );

0 commit comments

Comments
 (0)