Index: tests/db.php
===================================================================
--- tests/db.php	(revision 960)
+++ tests/db.php	(working copy)
@@ -14,6 +14,18 @@
 	protected $_queries = array();
 
 	/**
+	 * PHP Errors
+	 * @var array
+	 */
+	protected $_php_errors = array();
+	
+	/**
+	 * PHP error handler
+	 * @var callable|null
+	 */
+	protected $_error_handler = null;
+
+	/**
 	 * Set up the test fixture
 	 */
 	public function setUp() {
@@ -78,4 +90,53 @@
 		// Restore locale
 		setlocale( LC_ALL, $current_locale );
 	}
+	
+	/**
+	 * Test that flush doesn't fail with a non-existent result
+	 * @ticket 21533
+	 */
+	public function test_no_mysql_free_warnings() {
+		global $wpdb;
+		
+		// Start with an empty resource
+		$wpdb->result = null;
+		$wpdb->suppress_errors( true );
+		$this->assertNotInternalType( 'resource', $wpdb->result );
+		
+		// Watch for errors
+		$this->error_handler = set_error_handler( array( $this, '_error_handler' ), E_WARNING );
+		
+		// Make a bad query
+		$wpdb->query('SELECT * FROM non_existent_table');
+		$this->assertNotInternalType( 'resource', $wpdb->result );
+		
+		// Look for warnings related to mysql_free_result
+		foreach ( $this->_php_errors as $error ) {
+			$this->assertNotContains( 'mysql_free_result() expects parameter 1 to be resource, null given', $error['message'] );
+		}
+
+		// Restore the original handler
+		restore_error_handler();
+	}
+	
+	/**
+	 * Log the error and call the original error handler
+	 * @param int $type
+	 * @param string $message
+	 * @param string $file
+	 * @param int $line
+	 * @return bool
+	 */
+	public function _error_handler( $type, $message, $file, $line ) {
+		$this->_php_errors[] = array(
+		    'type'    => $type,
+		    'message' => $message,
+		    'file'    => $file,
+		    'line'    => $line
+		);
+		if ( !is_callable( $this->_error_handler ) ) {
+			return false;
+		}
+		return call_user_func_array( $this->_error_handler, func_get_args() );
+	}
 }
