Make WordPress Core

Ticket #13841: 13841-unit-test.2.diff

File 13841-unit-test.2.diff, 2.0 KB (added by kirasong, 13 years ago)

Ported unit test to use phpunit

  • tests/http/base.php

     
    178178
    179179                unlink($res['filename']); // Remove the temporary file
    180180        }
     181
     182        /**
     183         * Test that the timeouts are functioning as inteded
     184         * @ticket 13841
     185         * @return void
     186         */
     187        function test_timeout() {
     188                // Create a fake server to make requests from
     189                $server = new SimpleSocketServer();
     190                try {
     191                        $server->start( '127.0.0.1', 8112 );
     192                } catch ( Exception $e ) {
     193                        $this->markTestIncomplete( "Could not start socket server: " . $e->getMessage() );
     194                }
     195
     196                // Make the request and time out
     197                $url = 'http://127.0.0.1:8112/';
     198                $start = microtime( true );
     199                $res = wp_remote_request( $url, array( 'timeout' => 1 ) );
     200                $stop = microtime( true );
     201                $server->stop();
     202
     203                // Make sure it's a timeout
     204                $this->assertTrue( is_wp_error( $res ) );
     205                $this->assertEquals( 'http_request_failed', $res->get_error_code() );
     206
     207                // Make sure the runtime was between .9 and 1.1 seconds
     208                $this->assertGreaterThanOrEqual( .9, ($stop - $start) );
     209                $this->assertLessThanOrEqual( 1.1, ($stop - $start) );
     210        }
    181211}
     212
     213/**
     214 * Simple socket server
     215 *
     216 * Create a server which never responds.  This is
     217 * fairly useless, unless you're testing timeouts.
     218 *
     219 * @package    WordPress
     220 * @subpackage Unit Tests
     221 * @since      3.4.1
     222 */
     223class SimpleSocketServer {
     224
     225        /**
     226         * Socket
     227         * @var mixed
     228         */
     229        protected $_sock = null;
     230
     231        /**
     232         * Start listening
     233         * @param string $ip
     234         * @param int $port
     235         * @throws Exception
     236         * @return void
     237         */
     238        public function start( $ip, $port ) {
     239                $this->_sock = stream_socket_server( "tcp://$ip:$port", $errno, $errstr );
     240                stream_set_blocking( $this->_sock, 0 );
     241        }
     242
     243        /**
     244         * Stop listening
     245         * @return void
     246         */
     247        public function stop() {
     248                if ( null !== $this->_sock ) {
     249                        fclose( $this->_sock );
     250                }
     251        }
     252
     253        /**
     254         * Destructor
     255         * @return void
     256         */
     257        public function __destruct() {
     258                $this->stop;
     259        }
     260}