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 $v = $cache->getMultiWithSetCallback(
314 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
315 $this->assertEquals( $value, $v[$keyB], "Value returned" );
316 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
318 $priorTime = microtime( true );
321 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
322 $v = $cache->getMultiWithSetCallback(
323 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
325 $this->assertEquals( $value, $v[$keyB], "Value returned" );
326 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
327 $this->assertEquals( $value, $priorValue, "Has prior value" );
328 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
329 $t1 = $cache->getCheckKeyTime( $cKey1 );
330 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
331 $t2 = $cache->getCheckKeyTime( $cKey2 );
332 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
334 $priorTime = microtime( true );
337 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
338 $v = $cache->getMultiWithSetCallback(
339 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
341 $this->assertEquals( $value, $v[$keyC], "Value returned" );
342 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
343 $t1 = $cache->getCheckKeyTime( $cKey1 );
344 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
345 $t2 = $cache->getCheckKeyTime( $cKey2 );
346 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
349 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
351 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
353 $this->assertEquals( $value, $v, "Value returned" );
355 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
358 $key = wfRandomString();
359 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
360 $v = $cache->getMultiWithSetCallback(
361 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
362 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
363 $cache->delete( $key );
364 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
365 $v = $cache->getMultiWithSetCallback(
366 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
367 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
368 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
371 $ids = [ 1, 2, 3, 4, 5, 6 ];
372 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
373 return $wanCache->makeKey( 'test', $id );
375 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
376 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
381 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
384 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
385 array_values( $values ),
386 "Correct values in correct order"
389 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
390 array_keys( $values ),
391 "Correct keys in correct order"
393 $this->assertEquals( count( $ids ), $calls );
395 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
396 $this->assertEquals( count( $ids ), $calls, "Values cached" );
399 public static function getMultiWithSetCallback_provider() {
402 [ [ 'version' => 1 ], true ]
407 * @covers WANObjectCache::getWithSetCallback()
408 * @covers WANObjectCache::doGetWithSetCallback()
410 public function testLockTSE() {
411 $cache = $this->cache
;
412 $key = wfRandomString();
413 $value = wfRandomString();
416 $func = function() use ( &$calls, $value, $cache, $key ) {
418 // Immediately kill any mutex rather than waiting a second
419 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
423 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
424 $this->assertEquals( $value, $ret );
425 $this->assertEquals( 1, $calls, 'Value was populated' );
427 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
428 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
430 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
431 $ret = $cache->getWithSetCallback( $key, 30, $func,
432 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
433 $this->assertEquals( $value, $ret, 'Old value used' );
434 $this->assertEquals( 1, $calls, 'Callback was not used' );
436 $cache->delete( $key );
437 $ret = $cache->getWithSetCallback( $key, 30, $func,
438 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
439 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
440 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
442 $ret = $cache->getWithSetCallback( $key, 30, $func,
443 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
444 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
445 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
449 * @covers WANObjectCache::getWithSetCallback()
450 * @covers WANObjectCache::doGetWithSetCallback()
452 public function testLockTSESlow() {
453 $cache = $this->cache
;
454 $key = wfRandomString();
455 $value = wfRandomString();
458 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
460 $setOpts['since'] = microtime( true ) - 10;
461 // Immediately kill any mutex rather than waiting a second
462 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
466 // Value should be marked as stale due to snapshot lag
468 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
469 $this->assertEquals( $value, $ret );
470 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
471 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
472 $this->assertEquals( 1, $calls, 'Value was generated' );
474 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
475 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
476 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
477 $this->assertEquals( $value, $ret );
478 $this->assertEquals( 1, $calls, 'Callback was not used' );
482 * @covers WANObjectCache::getWithSetCallback()
483 * @covers WANObjectCache::doGetWithSetCallback()
485 public function testBusyValue() {
486 $cache = $this->cache
;
487 $key = wfRandomString();
488 $value = wfRandomString();
489 $busyValue = wfRandomString();
492 $func = function() use ( &$calls, $value, $cache, $key ) {
494 // Immediately kill any mutex rather than waiting a second
495 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
499 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
500 $this->assertEquals( $value, $ret );
501 $this->assertEquals( 1, $calls, 'Value was populated' );
503 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
504 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
506 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
507 $ret = $cache->getWithSetCallback( $key, 30, $func,
508 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
509 $this->assertEquals( $value, $ret, 'Callback used' );
510 $this->assertEquals( 2, $calls, 'Callback used' );
512 $ret = $cache->getWithSetCallback( $key, 30, $func,
513 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
514 $this->assertEquals( $value, $ret, 'Old value used' );
515 $this->assertEquals( 2, $calls, 'Callback was not used' );
517 $cache->delete( $key ); // no value at all anymore and still locked
518 $ret = $cache->getWithSetCallback( $key, 30, $func,
519 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
520 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
521 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
523 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
524 $ret = $cache->getWithSetCallback( $key, 30, $func,
525 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
526 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
527 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
529 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
530 $ret = $cache->getWithSetCallback( $key, 30, $func,
531 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
532 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
533 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
537 * @covers WANObjectCache::getMulti()
539 public function testGetMulti() {
540 $cache = $this->cache
;
542 $value1 = [ 'this' => 'is', 'a' => 'test' ];
543 $value2 = [ 'this' => 'is', 'another' => 'test' ];
545 $key1 = wfRandomString();
546 $key2 = wfRandomString();
547 $key3 = wfRandomString();
549 $cache->set( $key1, $value1, 5 );
550 $cache->set( $key2, $value2, 10 );
554 [ $key1 => $value1, $key2 => $value2 ],
555 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
556 'Result array populated'
559 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
560 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
561 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
563 $cKey1 = wfRandomString();
564 $cKey2 = wfRandomString();
566 $priorTime = microtime( true );
570 [ $key1 => $value1, $key2 => $value2 ],
571 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
572 "Result array populated even with new check keys"
574 $t1 = $cache->getCheckKeyTime( $cKey1 );
575 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
576 $t2 = $cache->getCheckKeyTime( $cKey2 );
577 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
578 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
579 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
580 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
585 [ $key1 => $value1, $key2 => $value2 ],
586 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
587 "Result array still populated even with new check keys"
589 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
590 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
591 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
595 * @covers WANObjectCache::getMulti()
596 * @covers WANObjectCache::processCheckKeys()
598 public function testGetMultiCheckKeys() {
599 $cache = $this->cache
;
601 $checkAll = wfRandomString();
602 $check1 = wfRandomString();
603 $check2 = wfRandomString();
604 $check3 = wfRandomString();
605 $value1 = wfRandomString();
606 $value2 = wfRandomString();
608 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
609 // several seconds during the test to assert the behaviour.
610 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
611 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
615 $cache->set( 'key1', $value1, 10 );
616 $cache->set( 'key2', $value2, 10 );
619 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
626 [ 'key1' => $value1, 'key2' => $value2 ],
630 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
631 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
632 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
633 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
635 $cache->touchCheckKey( $check1 );
638 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
645 [ 'key1' => $value1, 'key2' => $value2 ],
647 'key1 expired by check1, but value still provided'
649 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
650 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
652 $cache->touchCheckKey( $checkAll );
655 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
662 [ 'key1' => $value1, 'key2' => $value2 ],
664 'All keys expired by checkAll, but value still provided'
666 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
667 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
671 * @covers WANObjectCache::get()
672 * @covers WANObjectCache::processCheckKeys()
674 public function testCheckKeyInitHoldoff() {
675 $cache = $this->cache
;
677 for ( $i = 0; $i < 500; ++
$i ) {
678 $key = wfRandomString();
679 $checkKey = wfRandomString();
681 $cache->get( $key, $curTTL, [ $checkKey ] );
682 $cache->set( $key, 'val', 10 );
684 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
686 $this->assertEquals( 'val', $v );
687 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
690 for ( $i = 0; $i < 500; ++
$i ) {
691 $key = wfRandomString();
692 $checkKey = wfRandomString();
694 $cache->set( $key, 'val', 10 );
696 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
698 $this->assertEquals( 'val', $v );
699 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
704 * @covers WANObjectCache::delete()
706 public function testDelete() {
707 $key = wfRandomString();
708 $value = wfRandomString();
709 $this->cache
->set( $key, $value );
712 $v = $this->cache
->get( $key, $curTTL );
713 $this->assertEquals( $value, $v, "Key was created with value" );
714 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
716 $this->cache
->delete( $key );
719 $v = $this->cache
->get( $key, $curTTL );
720 $this->assertFalse( $v, "Deleted key has false value" );
721 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
723 $this->cache
->set( $key, $value . 'more' );
724 $v = $this->cache
->get( $key, $curTTL );
725 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
726 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
728 $this->cache
->set( $key, $value );
729 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
732 $v = $this->cache
->get( $key, $curTTL );
733 $this->assertFalse( $v, "Deleted key has false value" );
734 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
736 $this->cache
->set( $key, $value );
737 $v = $this->cache
->get( $key, $curTTL );
738 $this->assertEquals( $value, $v, "Key was created with value" );
739 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
743 * @dataProvider getWithSetCallback_versions_provider
744 * @param array $extOpts
747 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
748 $cache = $this->cache
;
750 $key = wfRandomString();
751 $value = wfRandomString();
754 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
759 // Set the main key (version N if versioned)
761 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
762 $this->assertEquals( $value, $v, "Value returned" );
763 $this->assertEquals( 1, $wasSet, "Value regenerated" );
764 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
765 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
766 // Set the key for version N+1 (if versioned)
768 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
771 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
772 $this->assertEquals( $value, $v, "Value returned" );
773 $this->assertEquals( 1, $wasSet, "Value regenerated" );
776 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
777 $this->assertEquals( $value, $v, "Value returned" );
778 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
782 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
783 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
786 $cache->delete( $key );
787 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
788 $this->assertEquals( $value, $v, "Value returned" );
789 $this->assertEquals( 1, $wasSet, "Value regenerated" );
793 $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" );
800 public static function getWithSetCallback_versions_provider() {
803 [ [ 'version' => 1 ], true ]
808 * @covers WANObjectCache::touchCheckKey()
809 * @covers WANObjectCache::resetCheckKey()
810 * @covers WANObjectCache::getCheckKeyTime()
812 public function testTouchKeys() {
813 $key = wfRandomString();
815 $priorTime = microtime( true );
817 $t0 = $this->cache
->getCheckKeyTime( $key );
818 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
820 $priorTime = microtime( true );
822 $this->cache
->touchCheckKey( $key );
823 $t1 = $this->cache
->getCheckKeyTime( $key );
824 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
826 $t2 = $this->cache
->getCheckKeyTime( $key );
827 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
830 $this->cache
->touchCheckKey( $key );
831 $t3 = $this->cache
->getCheckKeyTime( $key );
832 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
834 $t4 = $this->cache
->getCheckKeyTime( $key );
835 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
838 $this->cache
->resetCheckKey( $key );
839 $t5 = $this->cache
->getCheckKeyTime( $key );
840 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
842 $t6 = $this->cache
->getCheckKeyTime( $key );
843 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
847 * @covers WANObjectCache::getMulti()
849 public function testGetWithSeveralCheckKeys() {
850 $key = wfRandomString();
851 $tKey1 = wfRandomString();
852 $tKey2 = wfRandomString();
855 // Two check keys are newer (given hold-off) than $key, another is older
856 $this->internalCache
->set(
857 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
858 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
860 $this->internalCache
->set(
861 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
862 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
864 $this->internalCache
->set(
865 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
866 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
868 $this->cache
->set( $key, $value, 30 );
871 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
872 $this->assertEquals( $value, $v, "Value matches" );
873 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
874 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
878 * @covers WANObjectCache::reap()
879 * @covers WANObjectCache::reapCheckKey()
881 public function testReap() {
882 $vKey1 = wfRandomString();
883 $vKey2 = wfRandomString();
884 $tKey1 = wfRandomString();
885 $tKey2 = wfRandomString();
888 $knownPurge = time() - 60;
889 $goodTime = microtime( true ) - 5;
890 $badTime = microtime( true ) - 300;
892 $this->internalCache
->set(
893 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
895 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
896 WANObjectCache
::FLD_VALUE
=> $value,
897 WANObjectCache
::FLD_TTL
=> 3600,
898 WANObjectCache
::FLD_TIME
=> $goodTime
901 $this->internalCache
->set(
902 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
904 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
905 WANObjectCache
::FLD_VALUE
=> $value,
906 WANObjectCache
::FLD_TTL
=> 3600,
907 WANObjectCache
::FLD_TIME
=> $badTime
910 $this->internalCache
->set(
911 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
912 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
914 $this->internalCache
->set(
915 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
916 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
919 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
920 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
921 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
922 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
924 $this->assertFalse( $bad1 );
925 $this->assertTrue( $bad2 );
927 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
928 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
929 $this->assertFalse( $tBad1 );
930 $this->assertTrue( $tBad2 );
934 * @covers WANObjectCache::set()
936 public function testSetWithLag() {
939 $key = wfRandomString();
940 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
941 $this->cache
->set( $key, $value, 30, $opts );
942 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
944 $key = wfRandomString();
945 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
946 $this->cache
->set( $key, $value, 30, $opts );
947 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
949 $key = wfRandomString();
950 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
951 $this->cache
->set( $key, $value, 30, $opts );
952 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
956 * @covers WANObjectCache::set()
958 public function testWritePending() {
961 $key = wfRandomString();
962 $opts = [ 'pending' => true ];
963 $this->cache
->set( $key, $value, 30, $opts );
964 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
967 public function testMcRouterSupport() {
968 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
969 ->setMethods( [ 'set', 'delete' ] )->getMock();
970 $localBag->expects( $this->never() )->method( 'set' );
971 $localBag->expects( $this->never() )->method( 'delete' );
972 $wanCache = new WANObjectCache( [
973 'cache' => $localBag,
974 'pool' => 'testcache-hash',
975 'relayer' => new EventRelayerNull( [] )
977 $valFunc = function () {
981 // None of these should use broadcasting commands (e.g. SET, DELETE)
982 $wanCache->get( 'x' );
983 $wanCache->get( 'x', $ctl, [ 'check1' ] );
984 $wanCache->getMulti( [ 'x', 'y' ] );
985 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
986 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
987 $wanCache->getCheckKeyTime( 'zzz' );
988 $wanCache->reap( 'x', time() - 300 );
989 $wanCache->reap( 'zzz', time() - 300 );
993 * @dataProvider provideAdaptiveTTL
994 * @covers WANObjectCache::adaptiveTTL()
995 * @param float|int $ago
998 * @param float $factor
999 * @param int $adaptiveTTL
1001 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1002 $mtime = $ago ?
time() - $ago : $ago;
1004 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1006 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1007 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1009 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1011 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1012 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1015 public static function provideAdaptiveTTL() {
1017 [ 3600, 900, 30, .2, 720 ],
1018 [ 3600, 500, 30, .2, 500 ],
1019 [ 3600, 86400, 800, .2, 800 ],
1020 [ false, 86400, 800, .2, 800 ],
1021 [ null, 86400, 800, .2, 800 ]