| 147 | |
| 148 | /** |
| 149 | * Test that the timeouts are functioning as inteded |
| 150 | * @return void |
| 151 | */ |
| 152 | public function test_timeout() { |
| 153 | $this->knownWPBug( 13841 ); |
| 154 | |
| 155 | // Create a fake server to make requests from |
| 156 | $server = new SimpleSocketServer(); |
| 157 | try { |
| 158 | $server->start( '127.0.0.1', 8112 ); |
| 159 | } catch ( Exception $e ) { |
| 160 | $this->markTestIncomplete( "Could not start socket server: " . $e->getMessage() ); |
| 161 | } |
| 162 | |
| 163 | // Make the request and time out |
| 164 | $url = 'http://127.0.0.1:8112/'; |
| 165 | $start = microtime( true ); |
| 166 | $res = wp_remote_request( $url, array( 'timeout' => 1 ) ); |
| 167 | $stop = microtime( true ); |
| 168 | $server->stop(); |
| 169 | |
| 170 | // Make sure it's a timeout |
| 171 | $this->assertTrue( is_wp_error( $res ) ); |
| 172 | $this->assertEquals( 'http_request_failed', $res->get_error_code() ); |
| 173 | |
| 174 | // Make sure the runtime was between .9 and 1.1 seconds |
| 175 | $this->assertGreaterThanOrEqual( .9, ($stop - $start) ); |
| 176 | $this->assertLessThanOrEqual( 1.1, ($stop - $start) ); |
| 177 | } |
158 | | } |
159 | | No newline at end of file |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Simple socket server |
| 193 | * |
| 194 | * Create a server which never responds. This is |
| 195 | * fairly useless, unless you're testing timeouts. |
| 196 | * |
| 197 | * @package WordPress |
| 198 | * @subpackage Unit Tests |
| 199 | * @since 3.4.0 |
| 200 | */ |
| 201 | class SimpleSocketServer { |
| 202 | |
| 203 | /** |
| 204 | * Socket |
| 205 | * @var mixed |
| 206 | */ |
| 207 | protected $_sock = null; |
| 208 | |
| 209 | /** |
| 210 | * Start listening |
| 211 | * @param string $ip |
| 212 | * @param int $port |
| 213 | * @throws Exception |
| 214 | * @return void |
| 215 | */ |
| 216 | public function start( $ip, $port ) { |
| 217 | $this->_sock = stream_socket_server( "tcp://$ip:$port", $errno, $errstr ); |
| 218 | stream_set_blocking( $this->_sock, 0 ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Stop listening |
| 223 | * @return void |
| 224 | */ |
| 225 | public function stop() { |
| 226 | if ( null !== $this->_sock ) { |
| 227 | fclose( $this->_sock ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Destructor |
| 233 | * @return void |
| 234 | */ |
| 235 | public function __destruct() { |
| 236 | $this->stop; |
| 237 | } |
| 238 | } |
| 239 | |