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( [
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( [] )
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 ) {
38 $key = wfRandomString();
40 $this->cache
->get( $key, $curTTL, [], $asOf );
41 $this->assertNull( $curTTL, "Current TTL is null" );
42 $this->assertNull( $asOf, "Current as-of-time is infinite" );
44 $t = microtime( true );
45 $this->cache
->set( $key, $value, $ttl );
47 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
48 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
49 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
51 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
52 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
54 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
55 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
58 public static function provideSetAndGet() {
65 [ (object)[ 'meow' ], 3 ],
73 * @covers WANObjectCache::get()
75 public function testGetNotExists() {
76 $key = wfRandomString();
78 $value = $this->cache
->get( $key, $curTTL );
80 $this->assertFalse( $value, "Non-existing key has false value" );
81 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
85 * @covers WANObjectCache::set()
87 public function testSetOver() {
88 $key = wfRandomString();
89 for ( $i = 0; $i < 3; ++
$i ) {
90 $value = wfRandomString();
91 $this->cache
->set( $key, $value, 3 );
93 $this->assertEquals( $this->cache
->get( $key ), $value );
98 * @covers WANObjectCache::set()
100 public function testStaleSet() {
101 $key = wfRandomString();
102 $value = wfRandomString();
103 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
105 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
109 * @dataProvider getWithSetCallback_provider
110 * @covers WANObjectCache::getWithSetCallback()
111 * @covers WANObjectCache::doGetWithSetCallback()
112 * @param array $extOpts
113 * @param bool $versioned
115 public function testGetWithSetCallback( array $extOpts, $versioned ) {
116 $cache = $this->cache
;
118 $key = wfRandomString();
119 $value = wfRandomString();
120 $cKey1 = wfRandomString();
121 $cKey2 = wfRandomString();
124 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
126 $ttl = 20; // override with another value
131 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
132 $this->assertEquals( $value, $v, "Value returned" );
133 $this->assertEquals( 1, $wasSet, "Value regenerated" );
136 $cache->get( $key, $curTTL );
137 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
138 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
141 $v = $cache->getWithSetCallback( $key, 30, $func, [
145 $this->assertEquals( $value, $v, "Value returned" );
146 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
148 $priorTime = microtime( true );
151 $v = $cache->getWithSetCallback(
152 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
154 $this->assertEquals( $value, $v, "Value returned" );
155 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
156 $t1 = $cache->getCheckKeyTime( $cKey1 );
157 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
158 $t2 = $cache->getCheckKeyTime( $cKey2 );
159 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
161 $priorTime = microtime( true );
163 $v = $cache->getWithSetCallback(
164 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
166 $this->assertEquals( $value, $v, "Value returned" );
167 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
168 $t1 = $cache->getCheckKeyTime( $cKey1 );
169 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
170 $t2 = $cache->getCheckKeyTime( $cKey2 );
171 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
174 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
176 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
178 $this->assertEquals( $value, $v, "Value returned" );
180 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
183 $key = wfRandomString();
184 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
185 $this->assertEquals( $value, $v, "Value returned" );
186 $cache->delete( $key );
187 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
188 $this->assertEquals( $value, $v, "Value still returned after deleted" );
189 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
192 public static function getWithSetCallback_provider() {
195 [ [ 'version' => 1 ], true ]
200 * @covers WANObjectCache::getWithSetCallback()
201 * @covers WANObjectCache::doGetWithSetCallback()
203 public function testLockTSE() {
204 $cache = $this->cache
;
205 $key = wfRandomString();
206 $value = wfRandomString();
209 $func = function() use ( &$calls, $value ) {
214 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
215 $this->assertEquals( $value, $ret );
216 $this->assertEquals( 1, $calls, 'Value was populated' );
218 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
219 $this->internalCache
->lock( $key, 0 );
220 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
221 $this->assertEquals( $value, $ret );
222 $this->assertEquals( 1, $calls, 'Callback was not used' );
226 * @covers WANObjectCache::getWithSetCallback()
227 * @covers WANObjectCache::doGetWithSetCallback()
229 public function testLockTSESlow() {
230 $cache = $this->cache
;
231 $key = wfRandomString();
232 $value = wfRandomString();
235 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
237 $setOpts['since'] = microtime( true ) - 10;
241 // Value should be marked as stale due to snapshot lag
243 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
244 $this->assertEquals( $value, $ret );
245 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
246 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
247 $this->assertEquals( 1, $calls, 'Value was generated' );
249 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
250 $this->internalCache
->lock( $key, 0 );
251 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
252 $this->assertEquals( $value, $ret );
253 $this->assertEquals( 1, $calls, 'Callback was not used' );
257 * @covers WANObjectCache::getMulti()
259 public function testGetMulti() {
260 $cache = $this->cache
;
262 $value1 = [ 'this' => 'is', 'a' => 'test' ];
263 $value2 = [ 'this' => 'is', 'another' => 'test' ];
265 $key1 = wfRandomString();
266 $key2 = wfRandomString();
267 $key3 = wfRandomString();
269 $cache->set( $key1, $value1, 5 );
270 $cache->set( $key2, $value2, 10 );
274 [ $key1 => $value1, $key2 => $value2 ],
275 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
276 'Result array populated'
279 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
280 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
281 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
283 $cKey1 = wfRandomString();
284 $cKey2 = wfRandomString();
286 $priorTime = microtime( true );
290 [ $key1 => $value1, $key2 => $value2 ],
291 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
292 "Result array populated even with new check keys"
294 $t1 = $cache->getCheckKeyTime( $cKey1 );
295 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
296 $t2 = $cache->getCheckKeyTime( $cKey2 );
297 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
298 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
299 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
300 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
305 [ $key1 => $value1, $key2 => $value2 ],
306 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
307 "Result array still populated even with new check keys"
309 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
310 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
311 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
315 * @covers WANObjectCache::getMulti()
316 * @covers WANObjectCache::processCheckKeys()
318 public function testGetMultiCheckKeys() {
319 $cache = $this->cache
;
321 $checkAll = wfRandomString();
322 $check1 = wfRandomString();
323 $check2 = wfRandomString();
324 $check3 = wfRandomString();
325 $value1 = wfRandomString();
326 $value2 = wfRandomString();
328 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
329 // several seconds during the test to assert the behaviour.
330 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
331 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
335 $cache->set( 'key1', $value1, 10 );
336 $cache->set( 'key2', $value2, 10 );
339 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
346 [ 'key1' => $value1, 'key2' => $value2 ],
350 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
351 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
352 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
353 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
355 $cache->touchCheckKey( $check1 );
358 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
365 [ 'key1' => $value1, 'key2' => $value2 ],
367 'key1 expired by check1, but value still provided'
369 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
370 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
372 $cache->touchCheckKey( $checkAll );
375 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
382 [ 'key1' => $value1, 'key2' => $value2 ],
384 'All keys expired by checkAll, but value still provided'
386 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
387 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
391 * @covers WANObjectCache::get()
392 * @covers WANObjectCache::processCheckKeys()
394 public function testCheckKeyInitHoldoff() {
395 $cache = $this->cache
;
397 for ( $i = 0; $i < 500; ++
$i ) {
398 $key = wfRandomString();
399 $checkKey = wfRandomString();
401 $cache->get( $key, $curTTL, [ $checkKey ] );
402 $cache->set( $key, 'val', 10 );
404 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
406 $this->assertEquals( 'val', $v );
407 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
410 for ( $i = 0; $i < 500; ++
$i ) {
411 $key = wfRandomString();
412 $checkKey = wfRandomString();
414 $cache->set( $key, 'val', 10 );
416 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
418 $this->assertEquals( 'val', $v );
419 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
424 * @covers WANObjectCache::delete()
426 public function testDelete() {
427 $key = wfRandomString();
428 $value = wfRandomString();
429 $this->cache
->set( $key, $value );
432 $v = $this->cache
->get( $key, $curTTL );
433 $this->assertEquals( $value, $v, "Key was created with value" );
434 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
436 $this->cache
->delete( $key );
439 $v = $this->cache
->get( $key, $curTTL );
440 $this->assertFalse( $v, "Deleted key has false value" );
441 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
443 $this->cache
->set( $key, $value . 'more' );
444 $v = $this->cache
->get( $key, $curTTL );
445 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
446 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
448 $this->cache
->set( $key, $value );
449 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
452 $v = $this->cache
->get( $key, $curTTL );
453 $this->assertFalse( $v, "Deleted key has false value" );
454 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
456 $this->cache
->set( $key, $value );
457 $v = $this->cache
->get( $key, $curTTL );
458 $this->assertEquals( $value, $v, "Key was created with value" );
459 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
463 * @dataProvider getWithSetCallback_versions_provider
464 * @param array $extOpts
467 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
468 $cache = $this->cache
;
470 $key = wfRandomString();
471 $value = wfRandomString();
474 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
479 // Set the main key (version N if versioned)
481 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
482 $this->assertEquals( $value, $v, "Value returned" );
483 $this->assertEquals( 1, $wasSet, "Value regenerated" );
484 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
485 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
486 // Set the key for version N+1 (if versioned)
488 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
491 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
492 $this->assertEquals( $value, $v, "Value returned" );
493 $this->assertEquals( 1, $wasSet, "Value regenerated" );
496 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
497 $this->assertEquals( $value, $v, "Value returned" );
498 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
502 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
503 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
506 $cache->delete( $key );
507 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
508 $this->assertEquals( $value, $v, "Value returned" );
509 $this->assertEquals( 1, $wasSet, "Value regenerated" );
513 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
514 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
515 $this->assertEquals( $value, $v, "Value returned" );
516 $this->assertEquals( 1, $wasSet, "Value regenerated" );
520 public static function getWithSetCallback_versions_provider() {
523 [ [ 'version' => 1 ], true ]
528 * @covers WANObjectCache::touchCheckKey()
529 * @covers WANObjectCache::resetCheckKey()
530 * @covers WANObjectCache::getCheckKeyTime()
532 public function testTouchKeys() {
533 $key = wfRandomString();
535 $priorTime = microtime( true );
537 $t0 = $this->cache
->getCheckKeyTime( $key );
538 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
540 $priorTime = microtime( true );
542 $this->cache
->touchCheckKey( $key );
543 $t1 = $this->cache
->getCheckKeyTime( $key );
544 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
546 $t2 = $this->cache
->getCheckKeyTime( $key );
547 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
550 $this->cache
->touchCheckKey( $key );
551 $t3 = $this->cache
->getCheckKeyTime( $key );
552 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
554 $t4 = $this->cache
->getCheckKeyTime( $key );
555 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
558 $this->cache
->resetCheckKey( $key );
559 $t5 = $this->cache
->getCheckKeyTime( $key );
560 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
562 $t6 = $this->cache
->getCheckKeyTime( $key );
563 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
567 * @covers WANObjectCache::getMulti()
569 public function testGetWithSeveralCheckKeys() {
570 $key = wfRandomString();
571 $tKey1 = wfRandomString();
572 $tKey2 = wfRandomString();
575 // Two check keys are newer (given hold-off) than $key, another is older
576 $this->internalCache
->set(
577 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
578 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
580 $this->internalCache
->set(
581 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
582 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
584 $this->internalCache
->set(
585 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
586 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
588 $this->cache
->set( $key, $value, 30 );
591 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
592 $this->assertEquals( $value, $v, "Value matches" );
593 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
594 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
598 * @covers WANObjectCache::set()
600 public function testSetWithLag() {
603 $key = wfRandomString();
604 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
605 $this->cache
->set( $key, $value, 30, $opts );
606 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
608 $key = wfRandomString();
609 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
610 $this->cache
->set( $key, $value, 30, $opts );
611 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
613 $key = wfRandomString();
614 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
615 $this->cache
->set( $key, $value, 30, $opts );
616 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
620 * @covers WANObjectCache::set()
622 public function testWritePending() {
625 $key = wfRandomString();
626 $opts = [ 'pending' => true ];
627 $this->cache
->set( $key, $value, 30, $opts );
628 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );