From ba06fc4f5b065fbbfabe42cd5c10ca571c0148b7 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Tue, 6 Oct 2015 03:36:18 +0000 Subject: [PATCH] HTTP timeouts should cause some tests to be skipped, not failed A number of the HTTP external tests can inconstantly fail. As the HTTP API is one that doesn't change often, this failure creates noise. With the goal of increasing the signal from the unit tests, these tests are now skipped if they timeout. A notice is added when running the external http tests so that the developer knows what skipped tests may mean here. See #33968 git-svn-id: https://develop.svn.wordpress.org/trunk@34848 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/bootstrap.php | 7 +++++++ tests/phpunit/tests/http/base.php | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index e74b618691..0beefd604a 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -164,6 +164,13 @@ class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt { foreach ( $skipped_groups as $group_name => $skipped ) { echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL; } + + if ( ! isset( $skipped_groups['external-http'] ) ){ + echo PHP_EOL; + echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL; + echo 'If this changeset inclues changes to HTTP, make sure there are no timeouts.' . PHP_EOL; + echo PHP_EOL; + } } } new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] ); diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index c95fac303e..337a74b50e 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -17,6 +17,27 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { protected $http_request_args; + /** + * Mark test as skipped if the HTTP request times out + */ + function skipTestOnTimeout( $response ) { + if( ! is_wp_error( $response ) ){ + return; + } + if ( 'connect() timed out!' === $response->get_error_message() ){ + $this->markTestSkipped( 'HTTP timeout' ); + } + + if ( 0 === strpos( $response->get_error_message(), 'Operation timed out after' ) ){ + $this->markTestSkipped( 'HTTP timeout' ); + } + + if ( 'stream_socket_client(): unable to connect to tcp://s.w.org:80 (Connection timed out)' === $response->get_error_message() ){ + $this->markTestSkipped( 'HTTP timeout' ); + } + + } + function setUp() { if ( is_callable( array('WP_Http', '_getTransport') ) ) { @@ -198,6 +219,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { unlink( $res['filename'] ); } + $this->skipTestOnTimeout ($res ); + $this->assertNotWPError( $res ); $this->assertEquals( '', $res['body'] ); // The body should be empty. $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..) @@ -219,6 +242,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { unlink( $res['filename'] ); } + $this->skipTestOnTimeout ($res ); + $this->assertNotWPError( $res ); $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters @@ -235,6 +260,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { $res = wp_remote_request( $url, array( 'timeout' => 30, 'limit_response_size' => $size ) ); + $this->skipTestOnTimeout ($res ); + $this->assertNotWPError( $res ); $this->assertEquals( $size, strlen( $res['body'] ) ); }