d0ae8c990c19bb58d99d9d2cc600175313269182
3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 private $internalCache;
9 protected function setUp() {
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
15 $this->cache
= ObjectCache
::getWANInstance( $name );
17 $this->cache
= new WANObjectCache( array(
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( array() )
24 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
25 $this->internalCache
= $wanCache->cache
;
29 * @dataProvider provideSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
35 public function testSetAndGet( $value, $ttl ) {
36 $key = wfRandomString();
37 $this->cache
->set( $key, $value, $ttl );
40 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL ) );
41 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
42 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
44 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
45 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
49 public static function provideSetAndGet() {
56 array( (object)array( 'meow' ), 3 ),
59 array( 'pizzacat', INF
),
64 * @covers WANObjectCache::get()
66 public function testGetNotExists() {
67 $key = wfRandomString();
69 $value = $this->cache
->get( $key, $curTTL );
71 $this->assertFalse( $value, "Non-existing key has false value" );
72 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
76 * @covers WANObjectCache::set()
78 public function testSetOver() {
79 $key = wfRandomString();
80 for ( $i = 0; $i < 3; ++
$i ) {
81 $value = wfRandomString();
82 $this->cache
->set( $key, $value, 3 );
84 $this->assertEquals( $this->cache
->get( $key ), $value );
89 * @covers WANObjectCache::set()
91 public function testStaleSet() {
92 $key = wfRandomString();
93 $value = wfRandomString();
94 $this->cache
->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
96 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
100 * @covers WANObjectCache::getWithSetCallback()
101 * @covers WANObjectCache::doGetWithSetCallback()
103 public function testGetWithSetCallback() {
104 $cache = $this->cache
;
106 $key = wfRandomString();
107 $value = wfRandomString();
108 $cKey1 = wfRandomString();
109 $cKey2 = wfRandomString();
112 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
114 $ttl = 20; // override with another value
119 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
120 $this->assertEquals( $value, $v, "Value returned" );
121 $this->assertEquals( 1, $wasSet, "Value regenerated" );
124 $cache->get( $key, $curTTL );
125 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
126 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
129 $v = $cache->getWithSetCallback( $key, 30, $func, array(
133 $this->assertEquals( $value, $v, "Value returned" );
134 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
136 $priorTime = microtime( true );
139 $v = $cache->getWithSetCallback( $key, 30, $func,
140 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
141 $this->assertEquals( $value, $v, "Value returned" );
142 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
143 $t1 = $cache->getCheckKeyTime( $cKey1 );
144 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
145 $t2 = $cache->getCheckKeyTime( $cKey2 );
146 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
148 $priorTime = microtime( true );
150 $v = $cache->getWithSetCallback( $key, 30, $func,
151 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
152 $this->assertEquals( $value, $v, "Value returned" );
153 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
154 $t1 = $cache->getCheckKeyTime( $cKey1 );
155 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
156 $t2 = $cache->getCheckKeyTime( $cKey2 );
157 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
160 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
161 $this->assertEquals( $value, $v, "Value returned" );
162 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
165 $key = wfRandomString();
166 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
167 $this->assertEquals( $value, $v, "Value returned" );
168 $cache->delete( $key );
169 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
170 $this->assertEquals( $value, $v, "Value still returned after deleted" );
171 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
175 * @covers WANObjectCache::getWithSetCallback()
176 * @covers WANObjectCache::doGetWithSetCallback()
178 public function testLockTSE() {
179 $cache = $this->cache
;
180 $key = wfRandomString();
181 $value = wfRandomString();
184 $func = function() use ( &$calls, $value ) {
189 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
190 $this->assertEquals( $value, $ret );
191 $this->assertEquals( 1, $calls, 'Value was populated' );
193 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
194 $this->internalCache
->lock( $key, 0 );
195 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
196 $this->assertEquals( $value, $ret );
197 $this->assertEquals( 1, $calls, 'Callback was not used' );
201 * @covers WANObjectCache::getWithSetCallback()
202 * @covers WANObjectCache::doGetWithSetCallback()
204 public function testLockTSESlow() {
205 $cache = $this->cache
;
206 $key = wfRandomString();
207 $value = wfRandomString();
210 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
212 $setOpts['since'] = microtime( true ) - 10;
216 // Value should be marked as stale due to snapshot lag
218 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
219 $this->assertEquals( $value, $ret );
220 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
221 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
222 $this->assertEquals( 1, $calls, 'Value was generated' );
224 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
225 $this->internalCache
->lock( $key, 0 );
226 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
227 $this->assertEquals( $value, $ret );
228 $this->assertEquals( 1, $calls, 'Callback was not used' );
232 * @covers WANObjectCache::getMulti()
234 public function testGetMulti() {
235 $cache = $this->cache
;
237 $value1 = array( 'this' => 'is', 'a' => 'test' );
238 $value2 = array( 'this' => 'is', 'another' => 'test' );
240 $key1 = wfRandomString();
241 $key2 = wfRandomString();
242 $key3 = wfRandomString();
244 $cache->set( $key1, $value1, 5 );
245 $cache->set( $key2, $value2, 10 );
249 array( $key1 => $value1, $key2 => $value2 ),
250 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
253 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
254 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
255 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
257 $cKey1 = wfRandomString();
258 $cKey2 = wfRandomString();
261 array( $key1 => $value1, $key2 => $value2 ),
262 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
263 'Result array populated'
266 $priorTime = microtime( true );
270 array( $key1 => $value1, $key2 => $value2 ),
271 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
272 "Result array populated even with new check keys"
274 $t1 = $cache->getCheckKeyTime( $cKey1 );
275 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
276 $t2 = $cache->getCheckKeyTime( $cKey2 );
277 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
278 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
279 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
280 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
285 array( $key1 => $value1, $key2 => $value2 ),
286 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
287 "Result array still populated even with new check keys"
289 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
290 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
291 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
295 * @covers WANObjectCache::delete()
297 public function testDelete() {
298 $key = wfRandomString();
299 $value = wfRandomString();
300 $this->cache
->set( $key, $value );
303 $v = $this->cache
->get( $key, $curTTL );
304 $this->assertEquals( $value, $v, "Key was created with value" );
305 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
307 $this->cache
->delete( $key );
310 $v = $this->cache
->get( $key, $curTTL );
311 $this->assertFalse( $v, "Deleted key has false value" );
312 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
314 $this->cache
->set( $key, $value . 'more' );
315 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
316 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
320 * @covers WANObjectCache::touchCheckKey()
321 * @covers WANObjectCache::resetCheckKey()
322 * @covers WANObjectCache::getCheckKeyTime()
324 public function testTouchKeys() {
325 $key = wfRandomString();
327 $priorTime = microtime( true );
329 $t0 = $this->cache
->getCheckKeyTime( $key );
330 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
332 $priorTime = microtime( true );
334 $this->cache
->touchCheckKey( $key );
335 $t1 = $this->cache
->getCheckKeyTime( $key );
336 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
338 $t2 = $this->cache
->getCheckKeyTime( $key );
339 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
342 $this->cache
->touchCheckKey( $key );
343 $t3 = $this->cache
->getCheckKeyTime( $key );
344 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
346 $t4 = $this->cache
->getCheckKeyTime( $key );
347 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
350 $this->cache
->resetCheckKey( $key );
351 $t5 = $this->cache
->getCheckKeyTime( $key );
352 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
354 $t6 = $this->cache
->getCheckKeyTime( $key );
355 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
359 * @covers WANObjectCache::getMulti()
361 public function testGetWithSeveralCheckKeys() {
362 $key = wfRandomString();
363 $tKey1 = wfRandomString();
364 $tKey2 = wfRandomString();
367 // Two check keys are newer (given hold-off) than $key, another is older
368 $this->internalCache
->set(
369 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
370 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
372 $this->internalCache
->set(
373 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
374 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
376 $this->internalCache
->set(
377 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
378 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
380 $this->cache
->set( $key, $value, 30 );
383 $v = $this->cache
->get( $key, $curTTL, array( $tKey1, $tKey2 ) );
384 $this->assertEquals( $value, $v, "Value matches" );
385 $this->assertLessThan( -5, $curTTL, "Correct CTL" );
386 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
390 * @covers WANObjectCache::set()
392 public function testSetWithLag() {
395 $key = wfRandomString();
396 $opts = array( 'lag' => 300, 'since' => microtime( true ) );
397 $this->cache
->set( $key, $value, 30, $opts );
398 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
400 $key = wfRandomString();
401 $opts = array( 'lag' => 0, 'since' => microtime( true ) - 300 );
402 $this->cache
->set( $key, $value, 30, $opts );
403 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
405 $key = wfRandomString();
406 $opts = array( 'lag' => 5, 'since' => microtime( true ) - 5 );
407 $this->cache
->set( $key, $value, 30, $opts );
408 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
412 * @covers WANObjectCache::set()
414 public function testWritePending() {
417 $key = wfRandomString();
418 $opts = array( 'pending' => true );
419 $this->cache
->set( $key, $value, 30, $opts );
420 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );