3 use Wikimedia\TestingAccessWrapper
;
5 class WANObjectCacheTest
extends PHPUnit_Framework_TestCase
{
6 /** @var WANObjectCache */
9 private $internalCache;
11 protected function setUp() {
14 $this->cache
= new WANObjectCache( [
15 'cache' => new HashBagOStuff(),
16 'pool' => 'testcache-hash',
17 'relayer' => new EventRelayerNull( [] )
20 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
21 /** @noinspection PhpUndefinedFieldInspection */
22 $this->internalCache
= $wanCache->cache
;
26 * @dataProvider provideSetAndGet
27 * @covers WANObjectCache::set()
28 * @covers WANObjectCache::get()
29 * @covers WANObjectCache::makeKey()
33 public function testSetAndGet( $value, $ttl ) {
36 $key = $this->cache
->makeKey( 'x', wfRandomString() );
38 $this->cache
->get( $key, $curTTL, [], $asOf );
39 $this->assertNull( $curTTL, "Current TTL is null" );
40 $this->assertNull( $asOf, "Current as-of-time is infinite" );
42 $t = microtime( true );
43 $this->cache
->set( $key, $value, $ttl );
45 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
46 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
47 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
49 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
50 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
52 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
53 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
56 public static function provideSetAndGet() {
63 [ (object)[ 'meow' ], 3 ],
71 * @covers WANObjectCache::get()
72 * @covers WANObjectCache::makeGlobalKey()
74 public function testGetNotExists() {
75 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
77 $value = $this->cache
->get( $key, $curTTL );
79 $this->assertFalse( $value, "Non-existing key has false value" );
80 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
84 * @covers WANObjectCache::set()
86 public function testSetOver() {
87 $key = wfRandomString();
88 for ( $i = 0; $i < 3; ++
$i ) {
89 $value = wfRandomString();
90 $this->cache
->set( $key, $value, 3 );
92 $this->assertEquals( $this->cache
->get( $key ), $value );
97 * @covers WANObjectCache::set()
99 public function testStaleSet() {
100 $key = wfRandomString();
101 $value = wfRandomString();
102 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
104 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
107 public function testProcessCache() {
109 $callback = function () use ( &$hit ) {
113 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
114 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
116 foreach ( $keys as $i => $key ) {
117 $this->cache
->getWithSetCallback(
118 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
120 $this->assertEquals( 3, $hit );
122 foreach ( $keys as $i => $key ) {
123 $this->cache
->getWithSetCallback(
124 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
126 $this->assertEquals( 3, $hit, "Values cached" );
128 foreach ( $keys as $i => $key ) {
129 $this->cache
->getWithSetCallback(
130 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
132 $this->assertEquals( 6, $hit );
134 foreach ( $keys as $i => $key ) {
135 $this->cache
->getWithSetCallback(
136 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
138 $this->assertEquals( 6, $hit, "New values cached" );
140 foreach ( $keys as $i => $key ) {
141 $this->cache
->delete( $key );
142 $this->cache
->getWithSetCallback(
143 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
145 $this->assertEquals( 9, $hit, "Values evicted" );
147 $key = reset( $keys );
149 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
150 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
151 $this->assertEquals( 10, $hit, "Value cached" );
152 $outerCallback = function () use ( &$callback, $key ) {
153 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
157 $this->cache
->getWithSetCallback( $key, 100, $outerCallback );
158 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
162 * @dataProvider getWithSetCallback_provider
163 * @covers WANObjectCache::getWithSetCallback()
164 * @covers WANObjectCache::doGetWithSetCallback()
165 * @param array $extOpts
166 * @param bool $versioned
168 public function testGetWithSetCallback( array $extOpts, $versioned ) {
169 $cache = $this->cache
;
171 $key = wfRandomString();
172 $value = wfRandomString();
173 $cKey1 = wfRandomString();
174 $cKey2 = wfRandomString();
179 $func = function( $old, &$ttl, &$opts, $asOf )
180 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
185 $ttl = 20; // override with another value
190 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
191 $this->assertEquals( $value, $v, "Value returned" );
192 $this->assertEquals( 1, $wasSet, "Value regenerated" );
193 $this->assertFalse( $priorValue, "No prior value" );
194 $this->assertNull( $priorAsOf, "No prior value" );
197 $cache->get( $key, $curTTL );
198 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
199 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
202 $v = $cache->getWithSetCallback( $key, 30, $func, [
206 $this->assertEquals( $value, $v, "Value returned" );
207 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
209 $priorTime = microtime( true );
212 $v = $cache->getWithSetCallback(
213 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
215 $this->assertEquals( $value, $v, "Value returned" );
216 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
217 $this->assertEquals( $value, $priorValue, "Has prior value" );
218 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
219 $t1 = $cache->getCheckKeyTime( $cKey1 );
220 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
221 $t2 = $cache->getCheckKeyTime( $cKey2 );
222 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
224 $priorTime = microtime( true );
226 $v = $cache->getWithSetCallback(
227 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
229 $this->assertEquals( $value, $v, "Value returned" );
230 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
231 $t1 = $cache->getCheckKeyTime( $cKey1 );
232 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
233 $t2 = $cache->getCheckKeyTime( $cKey2 );
234 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
237 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
239 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
241 $this->assertEquals( $value, $v, "Value returned" );
243 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
246 $key = wfRandomString();
247 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
248 $this->assertEquals( $value, $v, "Value returned" );
249 $cache->delete( $key );
250 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
251 $this->assertEquals( $value, $v, "Value still returned after deleted" );
252 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
255 public static function getWithSetCallback_provider() {
258 [ [ 'version' => 1 ], true ]
263 * @dataProvider getMultiWithSetCallback_provider
264 * @covers WANObjectCache::getMultiWithSetCallback()
265 * @covers WANObjectCache::makeMultiKeys()
266 * @param array $extOpts
267 * @param bool $versioned
269 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
270 $cache = $this->cache
;
272 $keyA = wfRandomString();
273 $keyB = wfRandomString();
274 $keyC = wfRandomString();
275 $cKey1 = wfRandomString();
276 $cKey2 = wfRandomString();
281 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
282 &$wasSet, &$priorValue, &$priorAsOf
287 $ttl = 20; // override with another value
292 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
294 $v = $cache->getMultiWithSetCallback(
295 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
296 $this->assertEquals( $value, $v[$keyA], "Value returned" );
297 $this->assertEquals( 1, $wasSet, "Value regenerated" );
298 $this->assertFalse( $priorValue, "No prior value" );
299 $this->assertNull( $priorAsOf, "No prior value" );
302 $cache->get( $keyA, $curTTL );
303 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
304 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
308 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
309 $v = $cache->getMultiWithSetCallback(
310 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
311 $this->assertEquals( $value, $v[$keyB], "Value returned" );
312 $this->assertEquals( 1, $wasSet, "Value regenerated" );
313 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
314 $v = $cache->getMultiWithSetCallback(
315 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
316 $this->assertEquals( $value, $v[$keyB], "Value returned" );
317 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
318 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
320 $priorTime = microtime( true );
323 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
324 $v = $cache->getMultiWithSetCallback(
325 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
327 $this->assertEquals( $value, $v[$keyB], "Value returned" );
328 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
329 $this->assertEquals( $value, $priorValue, "Has prior value" );
330 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
331 $t1 = $cache->getCheckKeyTime( $cKey1 );
332 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
333 $t2 = $cache->getCheckKeyTime( $cKey2 );
334 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
336 $priorTime = microtime( true );
339 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
340 $v = $cache->getMultiWithSetCallback(
341 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
343 $this->assertEquals( $value, $v[$keyC], "Value returned" );
344 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
345 $t1 = $cache->getCheckKeyTime( $cKey1 );
346 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
347 $t2 = $cache->getCheckKeyTime( $cKey2 );
348 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
351 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
353 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
355 $this->assertEquals( $value, $v, "Value returned" );
357 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
360 $key = wfRandomString();
361 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
362 $v = $cache->getMultiWithSetCallback(
363 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
364 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
365 $cache->delete( $key );
366 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
367 $v = $cache->getMultiWithSetCallback(
368 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
369 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
370 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
373 $ids = [ 1, 2, 3, 4, 5, 6 ];
374 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
375 return $wanCache->makeKey( 'test', $id );
377 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
378 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
383 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
386 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
387 array_values( $values ),
388 "Correct values in correct order"
391 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
392 array_keys( $values ),
393 "Correct keys in correct order"
395 $this->assertEquals( count( $ids ), $calls );
397 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
398 $this->assertEquals( count( $ids ), $calls, "Values cached" );
400 // Mock the BagOStuff to assure only one getMulti() call given process caching
401 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
402 ->setMethods( [ 'getMulti' ] )->getMock();
403 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
404 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
405 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
407 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
409 // Warm the process cache
410 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
412 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
413 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
415 // Use the process cache
417 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
418 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
422 public static function getMultiWithSetCallback_provider() {
425 [ [ 'version' => 1 ], true ]
430 * @covers WANObjectCache::getWithSetCallback()
431 * @covers WANObjectCache::doGetWithSetCallback()
433 public function testLockTSE() {
434 $cache = $this->cache
;
435 $key = wfRandomString();
436 $value = wfRandomString();
439 $func = function() use ( &$calls, $value, $cache, $key ) {
441 // Immediately kill any mutex rather than waiting a second
442 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
446 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
447 $this->assertEquals( $value, $ret );
448 $this->assertEquals( 1, $calls, 'Value was populated' );
450 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
451 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
453 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
454 $ret = $cache->getWithSetCallback( $key, 30, $func,
455 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
456 $this->assertEquals( $value, $ret, 'Old value used' );
457 $this->assertEquals( 1, $calls, 'Callback was not used' );
459 $cache->delete( $key );
460 $ret = $cache->getWithSetCallback( $key, 30, $func,
461 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
462 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
463 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
465 $ret = $cache->getWithSetCallback( $key, 30, $func,
466 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
467 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
468 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
472 * @covers WANObjectCache::getWithSetCallback()
473 * @covers WANObjectCache::doGetWithSetCallback()
475 public function testLockTSESlow() {
476 $cache = $this->cache
;
477 $key = wfRandomString();
478 $value = wfRandomString();
481 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
483 $setOpts['since'] = microtime( true ) - 10;
484 // Immediately kill any mutex rather than waiting a second
485 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
489 // Value should be marked as stale due to snapshot lag
491 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
492 $this->assertEquals( $value, $ret );
493 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
494 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
495 $this->assertEquals( 1, $calls, 'Value was generated' );
497 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
498 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
499 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
500 $this->assertEquals( $value, $ret );
501 $this->assertEquals( 1, $calls, 'Callback was not used' );
505 * @covers WANObjectCache::getWithSetCallback()
506 * @covers WANObjectCache::doGetWithSetCallback()
508 public function testBusyValue() {
509 $cache = $this->cache
;
510 $key = wfRandomString();
511 $value = wfRandomString();
512 $busyValue = wfRandomString();
515 $func = function() use ( &$calls, $value, $cache, $key ) {
517 // Immediately kill any mutex rather than waiting a second
518 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
522 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
523 $this->assertEquals( $value, $ret );
524 $this->assertEquals( 1, $calls, 'Value was populated' );
526 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
527 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
529 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
530 $ret = $cache->getWithSetCallback( $key, 30, $func,
531 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
532 $this->assertEquals( $value, $ret, 'Callback used' );
533 $this->assertEquals( 2, $calls, 'Callback used' );
535 $ret = $cache->getWithSetCallback( $key, 30, $func,
536 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
537 $this->assertEquals( $value, $ret, 'Old value used' );
538 $this->assertEquals( 2, $calls, 'Callback was not used' );
540 $cache->delete( $key ); // no value at all anymore and still locked
541 $ret = $cache->getWithSetCallback( $key, 30, $func,
542 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
543 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
544 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
546 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
547 $ret = $cache->getWithSetCallback( $key, 30, $func,
548 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
549 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
550 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
552 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
553 $ret = $cache->getWithSetCallback( $key, 30, $func,
554 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
555 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
556 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
560 * @covers WANObjectCache::getMulti()
562 public function testGetMulti() {
563 $cache = $this->cache
;
565 $value1 = [ 'this' => 'is', 'a' => 'test' ];
566 $value2 = [ 'this' => 'is', 'another' => 'test' ];
568 $key1 = wfRandomString();
569 $key2 = wfRandomString();
570 $key3 = wfRandomString();
572 $cache->set( $key1, $value1, 5 );
573 $cache->set( $key2, $value2, 10 );
577 [ $key1 => $value1, $key2 => $value2 ],
578 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
579 'Result array populated'
582 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
583 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
584 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
586 $cKey1 = wfRandomString();
587 $cKey2 = wfRandomString();
589 $priorTime = microtime( true );
593 [ $key1 => $value1, $key2 => $value2 ],
594 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
595 "Result array populated even with new check keys"
597 $t1 = $cache->getCheckKeyTime( $cKey1 );
598 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
599 $t2 = $cache->getCheckKeyTime( $cKey2 );
600 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
601 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
602 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
603 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
608 [ $key1 => $value1, $key2 => $value2 ],
609 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
610 "Result array still populated even with new check keys"
612 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
613 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
614 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
618 * @covers WANObjectCache::getMulti()
619 * @covers WANObjectCache::processCheckKeys()
621 public function testGetMultiCheckKeys() {
622 $cache = $this->cache
;
624 $checkAll = wfRandomString();
625 $check1 = wfRandomString();
626 $check2 = wfRandomString();
627 $check3 = wfRandomString();
628 $value1 = wfRandomString();
629 $value2 = wfRandomString();
631 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
632 // several seconds during the test to assert the behaviour.
633 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
634 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
638 $cache->set( 'key1', $value1, 10 );
639 $cache->set( 'key2', $value2, 10 );
642 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
649 [ 'key1' => $value1, 'key2' => $value2 ],
653 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
654 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
655 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
656 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
658 $cache->touchCheckKey( $check1 );
661 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
668 [ 'key1' => $value1, 'key2' => $value2 ],
670 'key1 expired by check1, but value still provided'
672 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
673 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
675 $cache->touchCheckKey( $checkAll );
678 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
685 [ 'key1' => $value1, 'key2' => $value2 ],
687 'All keys expired by checkAll, but value still provided'
689 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
690 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
694 * @covers WANObjectCache::get()
695 * @covers WANObjectCache::processCheckKeys()
697 public function testCheckKeyInitHoldoff() {
698 $cache = $this->cache
;
700 for ( $i = 0; $i < 500; ++
$i ) {
701 $key = wfRandomString();
702 $checkKey = wfRandomString();
704 $cache->get( $key, $curTTL, [ $checkKey ] );
705 $cache->set( $key, 'val', 10 );
707 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
709 $this->assertEquals( 'val', $v );
710 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
713 for ( $i = 0; $i < 500; ++
$i ) {
714 $key = wfRandomString();
715 $checkKey = wfRandomString();
717 $cache->set( $key, 'val', 10 );
719 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
721 $this->assertEquals( 'val', $v );
722 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
727 * @covers WANObjectCache::delete()
729 public function testDelete() {
730 $key = wfRandomString();
731 $value = wfRandomString();
732 $this->cache
->set( $key, $value );
735 $v = $this->cache
->get( $key, $curTTL );
736 $this->assertEquals( $value, $v, "Key was created with value" );
737 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
739 $this->cache
->delete( $key );
742 $v = $this->cache
->get( $key, $curTTL );
743 $this->assertFalse( $v, "Deleted key has false value" );
744 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
746 $this->cache
->set( $key, $value . 'more' );
747 $v = $this->cache
->get( $key, $curTTL );
748 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
749 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
751 $this->cache
->set( $key, $value );
752 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
755 $v = $this->cache
->get( $key, $curTTL );
756 $this->assertFalse( $v, "Deleted key has false value" );
757 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
759 $this->cache
->set( $key, $value );
760 $v = $this->cache
->get( $key, $curTTL );
761 $this->assertEquals( $value, $v, "Key was created with value" );
762 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
766 * @dataProvider getWithSetCallback_versions_provider
767 * @param array $extOpts
770 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
771 $cache = $this->cache
;
773 $key = wfRandomString();
774 $value = wfRandomString();
777 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
782 // Set the main key (version N if versioned)
784 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
785 $this->assertEquals( $value, $v, "Value returned" );
786 $this->assertEquals( 1, $wasSet, "Value regenerated" );
787 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
788 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
789 // Set the key for version N+1 (if versioned)
791 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
794 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
795 $this->assertEquals( $value, $v, "Value returned" );
796 $this->assertEquals( 1, $wasSet, "Value regenerated" );
799 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
800 $this->assertEquals( $value, $v, "Value returned" );
801 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
805 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
806 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
809 $cache->delete( $key );
810 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
811 $this->assertEquals( $value, $v, "Value returned" );
812 $this->assertEquals( 1, $wasSet, "Value regenerated" );
816 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
817 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
818 $this->assertEquals( $value, $v, "Value returned" );
819 $this->assertEquals( 1, $wasSet, "Value regenerated" );
823 public static function getWithSetCallback_versions_provider() {
826 [ [ 'version' => 1 ], true ]
831 * @covers WANObjectCache::touchCheckKey()
832 * @covers WANObjectCache::resetCheckKey()
833 * @covers WANObjectCache::getCheckKeyTime()
835 public function testTouchKeys() {
836 $key = wfRandomString();
838 $priorTime = microtime( true );
840 $t0 = $this->cache
->getCheckKeyTime( $key );
841 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
843 $priorTime = microtime( true );
845 $this->cache
->touchCheckKey( $key );
846 $t1 = $this->cache
->getCheckKeyTime( $key );
847 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
849 $t2 = $this->cache
->getCheckKeyTime( $key );
850 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
853 $this->cache
->touchCheckKey( $key );
854 $t3 = $this->cache
->getCheckKeyTime( $key );
855 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
857 $t4 = $this->cache
->getCheckKeyTime( $key );
858 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
861 $this->cache
->resetCheckKey( $key );
862 $t5 = $this->cache
->getCheckKeyTime( $key );
863 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
865 $t6 = $this->cache
->getCheckKeyTime( $key );
866 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
870 * @covers WANObjectCache::getMulti()
872 public function testGetWithSeveralCheckKeys() {
873 $key = wfRandomString();
874 $tKey1 = wfRandomString();
875 $tKey2 = wfRandomString();
878 // Two check keys are newer (given hold-off) than $key, another is older
879 $this->internalCache
->set(
880 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
881 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
883 $this->internalCache
->set(
884 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
885 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
887 $this->internalCache
->set(
888 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
889 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
891 $this->cache
->set( $key, $value, 30 );
894 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
895 $this->assertEquals( $value, $v, "Value matches" );
896 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
897 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
901 * @covers WANObjectCache::reap()
902 * @covers WANObjectCache::reapCheckKey()
904 public function testReap() {
905 $vKey1 = wfRandomString();
906 $vKey2 = wfRandomString();
907 $tKey1 = wfRandomString();
908 $tKey2 = wfRandomString();
911 $knownPurge = time() - 60;
912 $goodTime = microtime( true ) - 5;
913 $badTime = microtime( true ) - 300;
915 $this->internalCache
->set(
916 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
918 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
919 WANObjectCache
::FLD_VALUE
=> $value,
920 WANObjectCache
::FLD_TTL
=> 3600,
921 WANObjectCache
::FLD_TIME
=> $goodTime
924 $this->internalCache
->set(
925 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
927 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
928 WANObjectCache
::FLD_VALUE
=> $value,
929 WANObjectCache
::FLD_TTL
=> 3600,
930 WANObjectCache
::FLD_TIME
=> $badTime
933 $this->internalCache
->set(
934 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
935 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
937 $this->internalCache
->set(
938 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
939 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
942 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
943 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
944 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
945 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
947 $this->assertFalse( $bad1 );
948 $this->assertTrue( $bad2 );
950 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
951 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
952 $this->assertFalse( $tBad1 );
953 $this->assertTrue( $tBad2 );
957 * @covers WANObjectCache::set()
959 public function testSetWithLag() {
962 $key = wfRandomString();
963 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
964 $this->cache
->set( $key, $value, 30, $opts );
965 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
967 $key = wfRandomString();
968 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
969 $this->cache
->set( $key, $value, 30, $opts );
970 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
972 $key = wfRandomString();
973 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
974 $this->cache
->set( $key, $value, 30, $opts );
975 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
979 * @covers WANObjectCache::set()
981 public function testWritePending() {
984 $key = wfRandomString();
985 $opts = [ 'pending' => true ];
986 $this->cache
->set( $key, $value, 30, $opts );
987 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
990 public function testMcRouterSupport() {
991 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
992 ->setMethods( [ 'set', 'delete' ] )->getMock();
993 $localBag->expects( $this->never() )->method( 'set' );
994 $localBag->expects( $this->never() )->method( 'delete' );
995 $wanCache = new WANObjectCache( [
996 'cache' => $localBag,
997 'pool' => 'testcache-hash',
998 'relayer' => new EventRelayerNull( [] )
1000 $valFunc = function () {
1004 // None of these should use broadcasting commands (e.g. SET, DELETE)
1005 $wanCache->get( 'x' );
1006 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1007 $wanCache->getMulti( [ 'x', 'y' ] );
1008 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1009 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1010 $wanCache->getCheckKeyTime( 'zzz' );
1011 $wanCache->reap( 'x', time() - 300 );
1012 $wanCache->reap( 'zzz', time() - 300 );
1016 * @dataProvider provideAdaptiveTTL
1017 * @covers WANObjectCache::adaptiveTTL()
1018 * @param float|int $ago
1019 * @param int $maxTTL
1020 * @param int $minTTL
1021 * @param float $factor
1022 * @param int $adaptiveTTL
1024 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1025 $mtime = $ago ?
time() - $ago : $ago;
1027 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1029 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1030 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1032 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1034 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1035 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1038 public static function provideAdaptiveTTL() {
1040 [ 3600, 900, 30, .2, 720 ],
1041 [ 3600, 500, 30, .2, 500 ],
1042 [ 3600, 86400, 800, .2, 800 ],
1043 [ false, 86400, 800, .2, 800 ],
1044 [ null, 86400, 800, .2, 800 ]