MediaWiki  REL1_24
RedisBloomCacheTest.php
Go to the documentation of this file.
00001 <?php
00002 
00011 class BloomCacheRedisTest extends MediaWikiTestCase {
00012     private static $suffix;
00013 
00014     protected function setUp() {
00015         parent::setUp();
00016 
00017         self::$suffix = self::$suffix ? : mt_rand();
00018 
00019         $fcache = BloomCache::get( 'main' );
00020         if ( $fcache instanceof BloomCacheRedis ) {
00021             $fcache->delete( "unit-testing-" . self::$suffix );
00022         } else {
00023             $this->markTestSkipped( 'The main bloom cache is not redis.' );
00024         }
00025     }
00026 
00027     public function testBloomCache() {
00028         $key = "unit-testing-" . self::$suffix;
00029         $fcache = BloomCache::get( 'main' );
00030         $count = 1500;
00031 
00032         $this->assertTrue( $fcache->delete( $key ), "OK delete of filter '$key'." );
00033         $this->assertTrue( $fcache->init( $key, $count, .001 ), "OK init of filter '$key'." );
00034 
00035         $members = array();
00036         for ( $i = 0; $i < $count; ++$i ) {
00037             $members[] = "$i-value-$i";
00038         }
00039         $this->assertTrue( $fcache->add( $key, $members ), "Addition of members to '$key' OK." );
00040 
00041         for ( $i = 0; $i < $count; ++$i ) {
00042             $this->assertTrue( $fcache->isHit( $key, "$i-value-$i" ), "Hit on member '$i-value-$i'." );
00043         }
00044 
00045         $falsePositives = array();
00046         for ( $i = $count; $i < 2 * $count; ++$i ) {
00047             if ( $fcache->isHit( $key, "value$i" ) ) {
00048                 $falsePositives[] = "value$i";
00049             }
00050         }
00051 
00052         $eFalsePositives = array(
00053             'value1763',
00054             'value2245',
00055             'value2353',
00056             'value2791',
00057             'value2898',
00058             'value2975'
00059         );
00060         $this->assertEquals( $eFalsePositives, $falsePositives, "Correct number of false positives found." );
00061     }
00062 
00063     protected function tearDown() {
00064         parent::tearDown();
00065 
00066         $fcache = BloomCache::get( 'main' );
00067         if ( $fcache instanceof BloomCacheRedis ) {
00068             $fcache->delete( "unit-testing-" . self::$suffix );
00069         }
00070     }
00071 }