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" );
401 public static function getMultiWithSetCallback_provider() {
404 [ [ 'version' => 1 ], true ]
409 * @covers WANObjectCache::getWithSetCallback()
410 * @covers WANObjectCache::doGetWithSetCallback()
412 public function testLockTSE() {
413 $cache = $this->cache
;
414 $key = wfRandomString();
415 $value = wfRandomString();
418 $func = function() use ( &$calls, $value, $cache, $key ) {
420 // Immediately kill any mutex rather than waiting a second
421 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
425 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
426 $this->assertEquals( $value, $ret );
427 $this->assertEquals( 1, $calls, 'Value was populated' );
429 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
430 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
432 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
433 $ret = $cache->getWithSetCallback( $key, 30, $func,
434 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
435 $this->assertEquals( $value, $ret, 'Old value used' );
436 $this->assertEquals( 1, $calls, 'Callback was not used' );
438 $cache->delete( $key );
439 $ret = $cache->getWithSetCallback( $key, 30, $func,
440 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
441 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
442 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
444 $ret = $cache->getWithSetCallback( $key, 30, $func,
445 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
446 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
447 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
451 * @covers WANObjectCache::getWithSetCallback()
452 * @covers WANObjectCache::doGetWithSetCallback()
454 public function testLockTSESlow() {
455 $cache = $this->cache
;
456 $key = wfRandomString();
457 $value = wfRandomString();
460 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
462 $setOpts['since'] = microtime( true ) - 10;
463 // Immediately kill any mutex rather than waiting a second
464 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
468 // Value should be marked as stale due to snapshot lag
470 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
471 $this->assertEquals( $value, $ret );
472 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
473 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
474 $this->assertEquals( 1, $calls, 'Value was generated' );
476 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
477 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
478 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
479 $this->assertEquals( $value, $ret );
480 $this->assertEquals( 1, $calls, 'Callback was not used' );
484 * @covers WANObjectCache::getWithSetCallback()
485 * @covers WANObjectCache::doGetWithSetCallback()
487 public function testBusyValue() {
488 $cache = $this->cache
;
489 $key = wfRandomString();
490 $value = wfRandomString();
491 $busyValue = wfRandomString();
494 $func = function() use ( &$calls, $value, $cache, $key ) {
496 // Immediately kill any mutex rather than waiting a second
497 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
501 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
502 $this->assertEquals( $value, $ret );
503 $this->assertEquals( 1, $calls, 'Value was populated' );
505 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
506 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
508 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
509 $ret = $cache->getWithSetCallback( $key, 30, $func,
510 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
511 $this->assertEquals( $value, $ret, 'Callback used' );
512 $this->assertEquals( 2, $calls, 'Callback used' );
514 $ret = $cache->getWithSetCallback( $key, 30, $func,
515 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
516 $this->assertEquals( $value, $ret, 'Old value used' );
517 $this->assertEquals( 2, $calls, 'Callback was not used' );
519 $cache->delete( $key ); // no value at all anymore and still locked
520 $ret = $cache->getWithSetCallback( $key, 30, $func,
521 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
522 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
523 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
525 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
526 $ret = $cache->getWithSetCallback( $key, 30, $func,
527 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
528 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
529 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
531 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
532 $ret = $cache->getWithSetCallback( $key, 30, $func,
533 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
534 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
535 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
539 * @covers WANObjectCache::getMulti()
541 public function testGetMulti() {
542 $cache = $this->cache
;
544 $value1 = [ 'this' => 'is', 'a' => 'test' ];
545 $value2 = [ 'this' => 'is', 'another' => 'test' ];
547 $key1 = wfRandomString();
548 $key2 = wfRandomString();
549 $key3 = wfRandomString();
551 $cache->set( $key1, $value1, 5 );
552 $cache->set( $key2, $value2, 10 );
556 [ $key1 => $value1, $key2 => $value2 ],
557 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
558 'Result array populated'
561 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
562 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
563 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
565 $cKey1 = wfRandomString();
566 $cKey2 = wfRandomString();
568 $priorTime = microtime( true );
572 [ $key1 => $value1, $key2 => $value2 ],
573 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
574 "Result array populated even with new check keys"
576 $t1 = $cache->getCheckKeyTime( $cKey1 );
577 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
578 $t2 = $cache->getCheckKeyTime( $cKey2 );
579 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
580 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
581 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
582 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
587 [ $key1 => $value1, $key2 => $value2 ],
588 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
589 "Result array still populated even with new check keys"
591 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
592 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
593 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
597 * @covers WANObjectCache::getMulti()
598 * @covers WANObjectCache::processCheckKeys()
600 public function testGetMultiCheckKeys() {
601 $cache = $this->cache
;
603 $checkAll = wfRandomString();
604 $check1 = wfRandomString();
605 $check2 = wfRandomString();
606 $check3 = wfRandomString();
607 $value1 = wfRandomString();
608 $value2 = wfRandomString();
610 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
611 // several seconds during the test to assert the behaviour.
612 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
613 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
617 $cache->set( 'key1', $value1, 10 );
618 $cache->set( 'key2', $value2, 10 );
621 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
628 [ 'key1' => $value1, 'key2' => $value2 ],
632 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
633 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
634 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
635 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
637 $cache->touchCheckKey( $check1 );
640 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
647 [ 'key1' => $value1, 'key2' => $value2 ],
649 'key1 expired by check1, but value still provided'
651 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
652 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
654 $cache->touchCheckKey( $checkAll );
657 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
664 [ 'key1' => $value1, 'key2' => $value2 ],
666 'All keys expired by checkAll, but value still provided'
668 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
669 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
673 * @covers WANObjectCache::get()
674 * @covers WANObjectCache::processCheckKeys()
676 public function testCheckKeyInitHoldoff() {
677 $cache = $this->cache
;
679 for ( $i = 0; $i < 500; ++
$i ) {
680 $key = wfRandomString();
681 $checkKey = wfRandomString();
683 $cache->get( $key, $curTTL, [ $checkKey ] );
684 $cache->set( $key, 'val', 10 );
686 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
688 $this->assertEquals( 'val', $v );
689 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
692 for ( $i = 0; $i < 500; ++
$i ) {
693 $key = wfRandomString();
694 $checkKey = wfRandomString();
696 $cache->set( $key, 'val', 10 );
698 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
700 $this->assertEquals( 'val', $v );
701 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
706 * @covers WANObjectCache::delete()
708 public function testDelete() {
709 $key = wfRandomString();
710 $value = wfRandomString();
711 $this->cache
->set( $key, $value );
714 $v = $this->cache
->get( $key, $curTTL );
715 $this->assertEquals( $value, $v, "Key was created with value" );
716 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
718 $this->cache
->delete( $key );
721 $v = $this->cache
->get( $key, $curTTL );
722 $this->assertFalse( $v, "Deleted key has false value" );
723 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
725 $this->cache
->set( $key, $value . 'more' );
726 $v = $this->cache
->get( $key, $curTTL );
727 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
728 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
730 $this->cache
->set( $key, $value );
731 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
734 $v = $this->cache
->get( $key, $curTTL );
735 $this->assertFalse( $v, "Deleted key has false value" );
736 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
738 $this->cache
->set( $key, $value );
739 $v = $this->cache
->get( $key, $curTTL );
740 $this->assertEquals( $value, $v, "Key was created with value" );
741 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
745 * @dataProvider getWithSetCallback_versions_provider
746 * @param array $extOpts
749 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
750 $cache = $this->cache
;
752 $key = wfRandomString();
753 $value = wfRandomString();
756 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
761 // Set the main key (version N if versioned)
763 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
764 $this->assertEquals( $value, $v, "Value returned" );
765 $this->assertEquals( 1, $wasSet, "Value regenerated" );
766 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
767 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
768 // Set the key for version N+1 (if versioned)
770 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
773 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
774 $this->assertEquals( $value, $v, "Value returned" );
775 $this->assertEquals( 1, $wasSet, "Value regenerated" );
778 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
779 $this->assertEquals( $value, $v, "Value returned" );
780 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
784 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
785 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
788 $cache->delete( $key );
789 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
790 $this->assertEquals( $value, $v, "Value returned" );
791 $this->assertEquals( 1, $wasSet, "Value regenerated" );
795 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
796 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
797 $this->assertEquals( $value, $v, "Value returned" );
798 $this->assertEquals( 1, $wasSet, "Value regenerated" );
802 public static function getWithSetCallback_versions_provider() {
805 [ [ 'version' => 1 ], true ]
810 * @covers WANObjectCache::touchCheckKey()
811 * @covers WANObjectCache::resetCheckKey()
812 * @covers WANObjectCache::getCheckKeyTime()
814 public function testTouchKeys() {
815 $key = wfRandomString();
817 $priorTime = microtime( true );
819 $t0 = $this->cache
->getCheckKeyTime( $key );
820 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
822 $priorTime = microtime( true );
824 $this->cache
->touchCheckKey( $key );
825 $t1 = $this->cache
->getCheckKeyTime( $key );
826 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
828 $t2 = $this->cache
->getCheckKeyTime( $key );
829 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
832 $this->cache
->touchCheckKey( $key );
833 $t3 = $this->cache
->getCheckKeyTime( $key );
834 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
836 $t4 = $this->cache
->getCheckKeyTime( $key );
837 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
840 $this->cache
->resetCheckKey( $key );
841 $t5 = $this->cache
->getCheckKeyTime( $key );
842 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
844 $t6 = $this->cache
->getCheckKeyTime( $key );
845 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
849 * @covers WANObjectCache::getMulti()
851 public function testGetWithSeveralCheckKeys() {
852 $key = wfRandomString();
853 $tKey1 = wfRandomString();
854 $tKey2 = wfRandomString();
857 // Two check keys are newer (given hold-off) than $key, another is older
858 $this->internalCache
->set(
859 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
860 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
862 $this->internalCache
->set(
863 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
864 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
866 $this->internalCache
->set(
867 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
868 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
870 $this->cache
->set( $key, $value, 30 );
873 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
874 $this->assertEquals( $value, $v, "Value matches" );
875 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
876 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
880 * @covers WANObjectCache::reap()
881 * @covers WANObjectCache::reapCheckKey()
883 public function testReap() {
884 $vKey1 = wfRandomString();
885 $vKey2 = wfRandomString();
886 $tKey1 = wfRandomString();
887 $tKey2 = wfRandomString();
890 $knownPurge = time() - 60;
891 $goodTime = microtime( true ) - 5;
892 $badTime = microtime( true ) - 300;
894 $this->internalCache
->set(
895 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
897 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
898 WANObjectCache
::FLD_VALUE
=> $value,
899 WANObjectCache
::FLD_TTL
=> 3600,
900 WANObjectCache
::FLD_TIME
=> $goodTime
903 $this->internalCache
->set(
904 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
906 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
907 WANObjectCache
::FLD_VALUE
=> $value,
908 WANObjectCache
::FLD_TTL
=> 3600,
909 WANObjectCache
::FLD_TIME
=> $badTime
912 $this->internalCache
->set(
913 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
914 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
916 $this->internalCache
->set(
917 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
918 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
921 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
922 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
923 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
924 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
926 $this->assertFalse( $bad1 );
927 $this->assertTrue( $bad2 );
929 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
930 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
931 $this->assertFalse( $tBad1 );
932 $this->assertTrue( $tBad2 );
936 * @covers WANObjectCache::set()
938 public function testSetWithLag() {
941 $key = wfRandomString();
942 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
943 $this->cache
->set( $key, $value, 30, $opts );
944 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
946 $key = wfRandomString();
947 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
948 $this->cache
->set( $key, $value, 30, $opts );
949 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
951 $key = wfRandomString();
952 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
953 $this->cache
->set( $key, $value, 30, $opts );
954 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
958 * @covers WANObjectCache::set()
960 public function testWritePending() {
963 $key = wfRandomString();
964 $opts = [ 'pending' => true ];
965 $this->cache
->set( $key, $value, 30, $opts );
966 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
969 public function testMcRouterSupport() {
970 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
971 ->setMethods( [ 'set', 'delete' ] )->getMock();
972 $localBag->expects( $this->never() )->method( 'set' );
973 $localBag->expects( $this->never() )->method( 'delete' );
974 $wanCache = new WANObjectCache( [
975 'cache' => $localBag,
976 'pool' => 'testcache-hash',
977 'relayer' => new EventRelayerNull( [] )
979 $valFunc = function () {
983 // None of these should use broadcasting commands (e.g. SET, DELETE)
984 $wanCache->get( 'x' );
985 $wanCache->get( 'x', $ctl, [ 'check1' ] );
986 $wanCache->getMulti( [ 'x', 'y' ] );
987 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
988 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
989 $wanCache->getCheckKeyTime( 'zzz' );
990 $wanCache->reap( 'x', time() - 300 );
991 $wanCache->reap( 'zzz', time() - 300 );
995 * @dataProvider provideAdaptiveTTL
996 * @covers WANObjectCache::adaptiveTTL()
997 * @param float|int $ago
1000 * @param float $factor
1001 * @param int $adaptiveTTL
1003 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1004 $mtime = $ago ?
time() - $ago : $ago;
1006 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1008 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1009 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1011 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1013 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1014 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1017 public static function provideAdaptiveTTL() {
1019 [ 3600, 900, 30, .2, 720 ],
1020 [ 3600, 500, 30, .2, 500 ],
1021 [ 3600, 86400, 800, .2, 800 ],
1022 [ false, 86400, 800, .2, 800 ],
1023 [ null, 86400, 800, .2, 800 ]