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 );
221 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
222 $ret = $cache->getWithSetCallback( $key, 30, $func,
223 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
224 $this->assertEquals( $value, $ret );
225 $this->assertEquals( 1, $calls, 'Callback was not used' );
227 $cache->delete( $key );
228 $ret = $cache->getWithSetCallback( $key, 30, $func,
229 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] ); // should use interim value
230 $this->assertEquals( $value, $ret );
231 $this->assertEquals( 2, $calls, 'Callback was used' );
233 $ret = $cache->getWithSetCallback( $key, 30, $func,
234 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
235 $this->assertEquals( $value, $ret );
236 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
240 * @covers WANObjectCache::getWithSetCallback()
241 * @covers WANObjectCache::doGetWithSetCallback()
243 public function testLockTSESlow() {
244 $cache = $this->cache
;
245 $key = wfRandomString();
246 $value = wfRandomString();
249 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
251 $setOpts['since'] = microtime( true ) - 10;
255 // Value should be marked as stale due to snapshot lag
257 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
258 $this->assertEquals( $value, $ret );
259 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
260 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
261 $this->assertEquals( 1, $calls, 'Value was generated' );
263 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
264 $this->internalCache
->lock( $key, 0 );
265 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
266 $this->assertEquals( $value, $ret );
267 $this->assertEquals( 1, $calls, 'Callback was not used' );
271 * @covers WANObjectCache::getMulti()
273 public function testGetMulti() {
274 $cache = $this->cache
;
276 $value1 = [ 'this' => 'is', 'a' => 'test' ];
277 $value2 = [ 'this' => 'is', 'another' => 'test' ];
279 $key1 = wfRandomString();
280 $key2 = wfRandomString();
281 $key3 = wfRandomString();
283 $cache->set( $key1, $value1, 5 );
284 $cache->set( $key2, $value2, 10 );
288 [ $key1 => $value1, $key2 => $value2 ],
289 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
290 'Result array populated'
293 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
294 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
295 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
297 $cKey1 = wfRandomString();
298 $cKey2 = wfRandomString();
300 $priorTime = microtime( true );
304 [ $key1 => $value1, $key2 => $value2 ],
305 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
306 "Result array populated even with new check keys"
308 $t1 = $cache->getCheckKeyTime( $cKey1 );
309 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
310 $t2 = $cache->getCheckKeyTime( $cKey2 );
311 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
312 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
313 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
314 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
319 [ $key1 => $value1, $key2 => $value2 ],
320 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
321 "Result array still populated even with new check keys"
323 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
324 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
325 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
329 * @covers WANObjectCache::getMulti()
330 * @covers WANObjectCache::processCheckKeys()
332 public function testGetMultiCheckKeys() {
333 $cache = $this->cache
;
335 $checkAll = wfRandomString();
336 $check1 = wfRandomString();
337 $check2 = wfRandomString();
338 $check3 = wfRandomString();
339 $value1 = wfRandomString();
340 $value2 = wfRandomString();
342 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
343 // several seconds during the test to assert the behaviour.
344 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
345 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
349 $cache->set( 'key1', $value1, 10 );
350 $cache->set( 'key2', $value2, 10 );
353 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
360 [ 'key1' => $value1, 'key2' => $value2 ],
364 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
365 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
366 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
367 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
369 $cache->touchCheckKey( $check1 );
372 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
379 [ 'key1' => $value1, 'key2' => $value2 ],
381 'key1 expired by check1, but value still provided'
383 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
384 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
386 $cache->touchCheckKey( $checkAll );
389 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
396 [ 'key1' => $value1, 'key2' => $value2 ],
398 'All keys expired by checkAll, but value still provided'
400 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
401 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
405 * @covers WANObjectCache::get()
406 * @covers WANObjectCache::processCheckKeys()
408 public function testCheckKeyInitHoldoff() {
409 $cache = $this->cache
;
411 for ( $i = 0; $i < 500; ++
$i ) {
412 $key = wfRandomString();
413 $checkKey = wfRandomString();
415 $cache->get( $key, $curTTL, [ $checkKey ] );
416 $cache->set( $key, 'val', 10 );
418 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
420 $this->assertEquals( 'val', $v );
421 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
424 for ( $i = 0; $i < 500; ++
$i ) {
425 $key = wfRandomString();
426 $checkKey = wfRandomString();
428 $cache->set( $key, 'val', 10 );
430 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
432 $this->assertEquals( 'val', $v );
433 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
438 * @covers WANObjectCache::delete()
440 public function testDelete() {
441 $key = wfRandomString();
442 $value = wfRandomString();
443 $this->cache
->set( $key, $value );
446 $v = $this->cache
->get( $key, $curTTL );
447 $this->assertEquals( $value, $v, "Key was created with value" );
448 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
450 $this->cache
->delete( $key );
453 $v = $this->cache
->get( $key, $curTTL );
454 $this->assertFalse( $v, "Deleted key has false value" );
455 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
457 $this->cache
->set( $key, $value . 'more' );
458 $v = $this->cache
->get( $key, $curTTL );
459 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
460 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
462 $this->cache
->set( $key, $value );
463 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
466 $v = $this->cache
->get( $key, $curTTL );
467 $this->assertFalse( $v, "Deleted key has false value" );
468 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
470 $this->cache
->set( $key, $value );
471 $v = $this->cache
->get( $key, $curTTL );
472 $this->assertEquals( $value, $v, "Key was created with value" );
473 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
477 * @dataProvider getWithSetCallback_versions_provider
478 * @param array $extOpts
481 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
482 $cache = $this->cache
;
484 $key = wfRandomString();
485 $value = wfRandomString();
488 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
493 // Set the main key (version N if versioned)
495 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
496 $this->assertEquals( $value, $v, "Value returned" );
497 $this->assertEquals( 1, $wasSet, "Value regenerated" );
498 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
499 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
500 // Set the key for version N+1 (if versioned)
502 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
505 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
506 $this->assertEquals( $value, $v, "Value returned" );
507 $this->assertEquals( 1, $wasSet, "Value regenerated" );
510 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
511 $this->assertEquals( $value, $v, "Value returned" );
512 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
516 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
517 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
520 $cache->delete( $key );
521 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
522 $this->assertEquals( $value, $v, "Value returned" );
523 $this->assertEquals( 1, $wasSet, "Value regenerated" );
527 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
528 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
529 $this->assertEquals( $value, $v, "Value returned" );
530 $this->assertEquals( 1, $wasSet, "Value regenerated" );
534 public static function getWithSetCallback_versions_provider() {
537 [ [ 'version' => 1 ], true ]
542 * @covers WANObjectCache::touchCheckKey()
543 * @covers WANObjectCache::resetCheckKey()
544 * @covers WANObjectCache::getCheckKeyTime()
546 public function testTouchKeys() {
547 $key = wfRandomString();
549 $priorTime = microtime( true );
551 $t0 = $this->cache
->getCheckKeyTime( $key );
552 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
554 $priorTime = microtime( true );
556 $this->cache
->touchCheckKey( $key );
557 $t1 = $this->cache
->getCheckKeyTime( $key );
558 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
560 $t2 = $this->cache
->getCheckKeyTime( $key );
561 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
564 $this->cache
->touchCheckKey( $key );
565 $t3 = $this->cache
->getCheckKeyTime( $key );
566 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
568 $t4 = $this->cache
->getCheckKeyTime( $key );
569 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
572 $this->cache
->resetCheckKey( $key );
573 $t5 = $this->cache
->getCheckKeyTime( $key );
574 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
576 $t6 = $this->cache
->getCheckKeyTime( $key );
577 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
581 * @covers WANObjectCache::getMulti()
583 public function testGetWithSeveralCheckKeys() {
584 $key = wfRandomString();
585 $tKey1 = wfRandomString();
586 $tKey2 = wfRandomString();
589 // Two check keys are newer (given hold-off) than $key, another is older
590 $this->internalCache
->set(
591 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
592 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
594 $this->internalCache
->set(
595 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
596 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
598 $this->internalCache
->set(
599 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
600 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
602 $this->cache
->set( $key, $value, 30 );
605 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
606 $this->assertEquals( $value, $v, "Value matches" );
607 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
608 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
612 * @covers WANObjectCache::set()
614 public function testSetWithLag() {
617 $key = wfRandomString();
618 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
619 $this->cache
->set( $key, $value, 30, $opts );
620 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
622 $key = wfRandomString();
623 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
624 $this->cache
->set( $key, $value, 30, $opts );
625 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
627 $key = wfRandomString();
628 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
629 $this->cache
->set( $key, $value, 30, $opts );
630 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
634 * @covers WANObjectCache::set()
636 public function testWritePending() {
639 $key = wfRandomString();
640 $opts = [ 'pending' => true ];
641 $this->cache
->set( $key, $value, 30, $opts );
642 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );