3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 protected function setUp() {
10 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
11 $name = $this->getCliArg( 'use-wanobjectcache' );
13 $this->cache
= ObjectCache
::getWANInstance( $name );
15 $this->cache
= new WANObjectCache( array(
16 'cache' => new HashBagOStuff(),
17 'pool' => 'testcache-hash',
18 'relayer' => new EventRelayerNull( array() )
24 * @dataProvider provider_testSetAndGet
25 * @covers WANObjectCache::set()
26 * @covers WANObjectCache::get()
30 public function testSetAndGet( $value, $ttl ) {
31 $key = wfRandomString();
32 $this->cache
->set( $key, $value, $ttl );
35 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL ) );
36 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
37 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
39 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
40 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
44 public static function provider_testSetAndGet() {
51 array( (object)array( 'meow' ), 3 ),
54 array( 'pizzacat', INF
),
58 public function testGetNotExists() {
59 $key = wfRandomString();
61 $value = $this->cache
->get( $key, $curTTL );
63 $this->assertFalse( $value, "Non-existing key has false value" );
64 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
67 public function testSetOver() {
68 $key = wfRandomString();
69 for ( $i=0; $i<3; ++
$i ) {
70 $value = wfRandomString();
71 $this->cache
->set($key, $value, 3);
73 $this->assertEquals( $this->cache
->get( $key ), $value );
78 * @covers WANObjectCache::getWithSetCallback()
80 public function testGetWithSetCallback() {
81 $cache = $this->cache
;
83 $key = wfRandomString();
84 $value = wfRandomString();
85 $cKey1 = wfRandomString();
86 $cKey2 = wfRandomString();
89 $func = function() use ( &$wasSet, $value ) { ++
$wasSet; return $value; };
92 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
93 $this->assertEquals( $v, $value );
94 $this->assertEquals( 1, $wasSet, "Value regenerated" );
97 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
98 $this->assertEquals( $v, $value );
99 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
101 $priorTime = microtime( true );
104 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
105 $this->assertEquals( $v, $value );
106 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
107 $t1 = $cache->getCheckKeyTime( $cKey1 );
108 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
109 $t2 = $cache->getCheckKeyTime( $cKey2 );
110 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
112 $priorTime = microtime( true );
114 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
115 $this->assertEquals( $v, $value );
116 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
117 $t1 = $cache->getCheckKeyTime( $cKey1 );
118 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
119 $t2 = $cache->getCheckKeyTime( $cKey2 );
120 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
123 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
124 $this->assertEquals( $v, $value );
125 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
129 * @covers WANObjectCache::getMulti()
131 public function testGetMulti() {
132 $cache = $this->cache
;
134 $value1 = array( 'this' => 'is', 'a' => 'test' );
135 $value2 = array( 'this' => 'is', 'another' => 'test' );
137 $key1 = wfRandomString();
138 $key2 = wfRandomString();
139 $key3 = wfRandomString();
141 $cache->set( $key1, $value1, 5 );
142 $cache->set( $key2, $value2, 10 );
146 array( $key1 => $value1, $key2 => $value2 ),
147 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
150 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
151 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
152 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
154 $cKey1 = wfRandomString();
155 $cKey2 = wfRandomString();
158 array( $key1 => $value1, $key2 => $value2 ),
159 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
160 'Result array populated'
163 $priorTime = microtime( true );
167 array( $key1 => $value1, $key2 => $value2 ),
168 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
169 "Result array populated even with new check keys"
171 $t1 = $cache->getCheckKeyTime( $cKey1 );
172 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
173 $t2 = $cache->getCheckKeyTime( $cKey2 );
174 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
175 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
176 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
177 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
182 array( $key1 => $value1, $key2 => $value2 ),
183 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
184 "Result array still populated even with new check keys"
186 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
187 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
188 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
192 * @covers WANObjectCache::delete()
194 public function testDelete() {
195 $key = wfRandomString();
196 $value = wfRandomString();
197 $this->cache
->set( $key, $value );
200 $v = $this->cache
->get( $key, $curTTL );
201 $this->assertEquals( $value, $v, "Key was created with value" );
202 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
204 $this->cache
->delete( $key );
207 $v = $this->cache
->get( $key, $curTTL );
208 $this->assertFalse( $v, "Deleted key has false value" );
209 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
211 $this->cache
->set( $key, $value . 'more' );
212 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
213 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
217 * @covers WANObjectCache::touchCheckKey()
218 * @covers WANObjectCache::getCheckKeyTime()
220 public function testTouchKeys() {
221 $key = wfRandomString();
223 $t0 = $this->cache
->getCheckKeyTime( $key );
224 $this->assertFalse( $t0, 'Check key time is false' );
226 $priorTime = microtime( true );
228 $this->cache
->touchCheckKey( $key );
229 $t1 = $this->cache
->getCheckKeyTime( $key );
230 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
232 $t2 = $this->cache
->getCheckKeyTime( $key );
233 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
236 $this->cache
->touchCheckKey( $key );
237 $t3 = $this->cache
->getCheckKeyTime( $key );
238 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
240 $t4 = $this->cache
->getCheckKeyTime( $key );
241 $this->assertEquals( $t3, $t4, 'Check key time did not change' );