summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/cache/RedisBloomCacheTest.php
blob: 3d491e9062b65eaa671dfc2cf5d8ba33cf7fca1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

/**
 * Test for BloomCacheRedis class.
 *
 * @TODO: some generic base "redis test server conf" for all testing?
 *
 * @covers BloomCacheRedis
 * @group Cache
 */
class BloomCacheRedisTest extends MediaWikiTestCase {
	private static $suffix;

	protected function setUp() {
		parent::setUp();

		self::$suffix = self::$suffix ? : mt_rand();

		$fcache = BloomCache::get( 'main' );
		if ( $fcache instanceof BloomCacheRedis ) {
			$fcache->delete( "unit-testing-" . self::$suffix );
		} else {
			$this->markTestSkipped( 'The main bloom cache is not redis.' );
		}
	}

	public function testBloomCache() {
		$key = "unit-testing-" . self::$suffix;
		$fcache = BloomCache::get( 'main' );
		$count = 1500;

		$this->assertTrue( $fcache->delete( $key ), "OK delete of filter '$key'." );
		$this->assertTrue( $fcache->init( $key, $count, .001 ), "OK init of filter '$key'." );

		$members = array();
		for ( $i = 0; $i < $count; ++$i ) {
			$members[] = "$i-value-$i";
		}
		$this->assertTrue( $fcache->add( $key, $members ), "Addition of members to '$key' OK." );

		for ( $i = 0; $i < $count; ++$i ) {
			$this->assertTrue( $fcache->isHit( $key, "$i-value-$i" ), "Hit on member '$i-value-$i'." );
		}

		$falsePositives = array();
		for ( $i = $count; $i < 2 * $count; ++$i ) {
			if ( $fcache->isHit( $key, "value$i" ) ) {
				$falsePositives[] = "value$i";
			}
		}

		$eFalsePositives = array(
			'value1763',
			'value2245',
			'value2353',
			'value2791',
			'value2898',
			'value2975'
		);
		$this->assertEquals( $eFalsePositives, $falsePositives, "Correct number of false positives found." );
	}

	protected function tearDown() {
		parent::tearDown();

		$fcache = BloomCache::get( 'main' );
		if ( $fcache instanceof BloomCacheRedis ) {
			$fcache->delete( "unit-testing-" . self::$suffix );
		}
	}
}