3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 private $internalCache;
9 protected function setUp() {
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
15 $this->cache
= ObjectCache
::getWANInstance( $name );
17 $this->cache
= new WANObjectCache( [
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( [] )
24 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
25 /** @noinspection PhpUndefinedFieldInspection */
26 $this->internalCache
= $wanCache->cache
;
30 * @dataProvider provideSetAndGet
31 * @covers WANObjectCache::set()
32 * @covers WANObjectCache::get()
33 * @covers WANObjectCache::makeKey()
37 public function testSetAndGet( $value, $ttl ) {
40 $key = $this->cache
->makeKey( 'x', wfRandomString() );
42 $this->cache
->get( $key, $curTTL, [], $asOf );
43 $this->assertNull( $curTTL, "Current TTL is null" );
44 $this->assertNull( $asOf, "Current as-of-time is infinite" );
46 $t = microtime( true );
47 $this->cache
->set( $key, $value, $ttl );
49 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
50 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
51 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
53 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
54 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
56 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
57 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
60 public static function provideSetAndGet() {
67 [ (object)[ 'meow' ], 3 ],
75 * @covers WANObjectCache::get()
76 * @covers WANObjectCache::makeGlobalKey()
78 public function testGetNotExists() {
79 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
81 $value = $this->cache
->get( $key, $curTTL );
83 $this->assertFalse( $value, "Non-existing key has false value" );
84 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
88 * @covers WANObjectCache::set()
90 public function testSetOver() {
91 $key = wfRandomString();
92 for ( $i = 0; $i < 3; ++
$i ) {
93 $value = wfRandomString();
94 $this->cache
->set( $key, $value, 3 );
96 $this->assertEquals( $this->cache
->get( $key ), $value );
101 * @covers WANObjectCache::set()
103 public function testStaleSet() {
104 $key = wfRandomString();
105 $value = wfRandomString();
106 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
108 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
111 public function testProcessCache() {
113 $callback = function () use ( &$hit ) {
117 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
118 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
120 foreach ( $keys as $i => $key ) {
121 $this->cache
->getWithSetCallback(
122 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
124 $this->assertEquals( 3, $hit );
126 foreach ( $keys as $i => $key ) {
127 $this->cache
->getWithSetCallback(
128 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
130 $this->assertEquals( 3, $hit, "Values cached" );
132 foreach ( $keys as $i => $key ) {
133 $this->cache
->getWithSetCallback(
134 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
136 $this->assertEquals( 6, $hit );
138 foreach ( $keys as $i => $key ) {
139 $this->cache
->getWithSetCallback(
140 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
142 $this->assertEquals( 6, $hit, "New values cached" );
144 foreach ( $keys as $i => $key ) {
145 $this->cache
->delete( $key );
146 $this->cache
->getWithSetCallback(
147 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
149 $this->assertEquals( 9, $hit, "Values evicted" );
153 * @dataProvider getWithSetCallback_provider
154 * @covers WANObjectCache::getWithSetCallback()
155 * @covers WANObjectCache::doGetWithSetCallback()
156 * @param array $extOpts
157 * @param bool $versioned
159 public function testGetWithSetCallback( array $extOpts, $versioned ) {
160 $cache = $this->cache
;
162 $key = wfRandomString();
163 $value = wfRandomString();
164 $cKey1 = wfRandomString();
165 $cKey2 = wfRandomString();
170 $func = function( $old, &$ttl, &$opts, $asOf )
171 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
176 $ttl = 20; // override with another value
181 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
182 $this->assertEquals( $value, $v, "Value returned" );
183 $this->assertEquals( 1, $wasSet, "Value regenerated" );
184 $this->assertFalse( $priorValue, "No prior value" );
185 $this->assertNull( $priorAsOf, "No prior value" );
188 $cache->get( $key, $curTTL );
189 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
190 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
193 $v = $cache->getWithSetCallback( $key, 30, $func, [
197 $this->assertEquals( $value, $v, "Value returned" );
198 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
200 $priorTime = microtime( true );
203 $v = $cache->getWithSetCallback(
204 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
206 $this->assertEquals( $value, $v, "Value returned" );
207 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
208 $this->assertEquals( $value, $priorValue, "Has prior value" );
209 $this->assertType( 'float', $priorAsOf, "Has prior value" );
210 $t1 = $cache->getCheckKeyTime( $cKey1 );
211 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
212 $t2 = $cache->getCheckKeyTime( $cKey2 );
213 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
215 $priorTime = microtime( true );
217 $v = $cache->getWithSetCallback(
218 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
220 $this->assertEquals( $value, $v, "Value returned" );
221 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
222 $t1 = $cache->getCheckKeyTime( $cKey1 );
223 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
224 $t2 = $cache->getCheckKeyTime( $cKey2 );
225 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
228 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
230 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
232 $this->assertEquals( $value, $v, "Value returned" );
234 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
237 $key = wfRandomString();
238 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
239 $this->assertEquals( $value, $v, "Value returned" );
240 $cache->delete( $key );
241 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
242 $this->assertEquals( $value, $v, "Value still returned after deleted" );
243 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
246 public static function getWithSetCallback_provider() {
249 [ [ 'version' => 1 ], true ]
254 * @dataProvider getMultiWithSetCallback_provider
255 * @covers WANObjectCache::getMultiWithSetCallback()
256 * @covers WANObjectCache::makeMultiKeys()
257 * @param array $extOpts
258 * @param bool $versioned
260 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
261 $cache = $this->cache
;
263 $keyA = wfRandomString();
264 $keyB = wfRandomString();
265 $keyC = wfRandomString();
266 $cKey1 = wfRandomString();
267 $cKey2 = wfRandomString();
272 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
273 &$wasSet, &$priorValue, &$priorAsOf
278 $ttl = 20; // override with another value
283 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
285 $v = $cache->getMultiWithSetCallback(
286 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
287 $this->assertEquals( $value, $v[$keyA], "Value returned" );
288 $this->assertEquals( 1, $wasSet, "Value regenerated" );
289 $this->assertFalse( $priorValue, "No prior value" );
290 $this->assertNull( $priorAsOf, "No prior value" );
293 $cache->get( $keyA, $curTTL );
294 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
295 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
299 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
300 $v = $cache->getMultiWithSetCallback(
301 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
302 $this->assertEquals( $value, $v[$keyB], "Value returned" );
303 $this->assertEquals( 1, $wasSet, "Value regenerated" );
304 $v = $cache->getMultiWithSetCallback(
305 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
306 $this->assertEquals( $value, $v[$keyB], "Value returned" );
307 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
309 $priorTime = microtime( true );
312 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
313 $v = $cache->getMultiWithSetCallback(
314 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
316 $this->assertEquals( $value, $v[$keyB], "Value returned" );
317 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
318 $this->assertEquals( $value, $priorValue, "Has prior value" );
319 $this->assertType( 'float', $priorAsOf, "Has prior value" );
320 $t1 = $cache->getCheckKeyTime( $cKey1 );
321 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
322 $t2 = $cache->getCheckKeyTime( $cKey2 );
323 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
325 $priorTime = microtime( true );
328 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
329 $v = $cache->getMultiWithSetCallback(
330 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
332 $this->assertEquals( $value, $v[$keyC], "Value returned" );
333 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
334 $t1 = $cache->getCheckKeyTime( $cKey1 );
335 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
336 $t2 = $cache->getCheckKeyTime( $cKey2 );
337 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
340 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
342 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
344 $this->assertEquals( $value, $v, "Value returned" );
346 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
349 $key = wfRandomString();
350 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
351 $v = $cache->getMultiWithSetCallback(
352 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
353 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
354 $cache->delete( $key );
355 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
356 $v = $cache->getMultiWithSetCallback(
357 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
358 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
359 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
362 $ids = [ 1, 2, 3, 4, 5, 6 ];
363 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
364 return $wanCache->makeKey( 'test', $id );
366 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
367 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
372 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
375 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
376 array_values( $values ),
377 "Correct values in correct order"
380 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
381 array_keys( $values ),
382 "Correct keys in correct order"
384 $this->assertEquals( count( $ids ), $calls );
386 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
387 $this->assertEquals( count( $ids ), $calls, "Values cached" );
390 public static function getMultiWithSetCallback_provider() {
393 [ [ 'version' => 1 ], true ]
398 * @covers WANObjectCache::getWithSetCallback()
399 * @covers WANObjectCache::doGetWithSetCallback()
401 public function testLockTSE() {
402 $cache = $this->cache
;
403 $key = wfRandomString();
404 $value = wfRandomString();
407 $func = function() use ( &$calls, $value, $cache, $key ) {
409 // Immediately kill any mutex rather than waiting a second
410 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
414 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
415 $this->assertEquals( $value, $ret );
416 $this->assertEquals( 1, $calls, 'Value was populated' );
418 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
419 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
421 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
422 $ret = $cache->getWithSetCallback( $key, 30, $func,
423 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
424 $this->assertEquals( $value, $ret, 'Old value used' );
425 $this->assertEquals( 1, $calls, 'Callback was not used' );
427 $cache->delete( $key );
428 $ret = $cache->getWithSetCallback( $key, 30, $func,
429 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
430 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
431 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
433 $ret = $cache->getWithSetCallback( $key, 30, $func,
434 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
435 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
436 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
440 * @covers WANObjectCache::getWithSetCallback()
441 * @covers WANObjectCache::doGetWithSetCallback()
443 public function testLockTSESlow() {
444 $cache = $this->cache
;
445 $key = wfRandomString();
446 $value = wfRandomString();
449 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
451 $setOpts['since'] = microtime( true ) - 10;
452 // Immediately kill any mutex rather than waiting a second
453 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
457 // Value should be marked as stale due to snapshot lag
459 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
460 $this->assertEquals( $value, $ret );
461 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
462 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
463 $this->assertEquals( 1, $calls, 'Value was generated' );
465 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
466 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
467 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
468 $this->assertEquals( $value, $ret );
469 $this->assertEquals( 1, $calls, 'Callback was not used' );
473 * @covers WANObjectCache::getWithSetCallback()
474 * @covers WANObjectCache::doGetWithSetCallback()
476 public function testBusyValue() {
477 $cache = $this->cache
;
478 $key = wfRandomString();
479 $value = wfRandomString();
480 $busyValue = wfRandomString();
483 $func = function() use ( &$calls, $value, $cache, $key ) {
485 // Immediately kill any mutex rather than waiting a second
486 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
490 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
491 $this->assertEquals( $value, $ret );
492 $this->assertEquals( 1, $calls, 'Value was populated' );
494 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
495 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
497 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
498 $ret = $cache->getWithSetCallback( $key, 30, $func,
499 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
500 $this->assertEquals( $value, $ret, 'Callback used' );
501 $this->assertEquals( 2, $calls, 'Callback used' );
503 $ret = $cache->getWithSetCallback( $key, 30, $func,
504 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
505 $this->assertEquals( $value, $ret, 'Old value used' );
506 $this->assertEquals( 2, $calls, 'Callback was not used' );
508 $cache->delete( $key ); // no value at all anymore and still locked
509 $ret = $cache->getWithSetCallback( $key, 30, $func,
510 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
511 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
512 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
514 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
515 $ret = $cache->getWithSetCallback( $key, 30, $func,
516 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
517 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
518 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
520 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
521 $ret = $cache->getWithSetCallback( $key, 30, $func,
522 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
523 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
524 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
528 * @covers WANObjectCache::getMulti()
530 public function testGetMulti() {
531 $cache = $this->cache
;
533 $value1 = [ 'this' => 'is', 'a' => 'test' ];
534 $value2 = [ 'this' => 'is', 'another' => 'test' ];
536 $key1 = wfRandomString();
537 $key2 = wfRandomString();
538 $key3 = wfRandomString();
540 $cache->set( $key1, $value1, 5 );
541 $cache->set( $key2, $value2, 10 );
545 [ $key1 => $value1, $key2 => $value2 ],
546 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
547 'Result array populated'
550 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
551 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
552 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
554 $cKey1 = wfRandomString();
555 $cKey2 = wfRandomString();
557 $priorTime = microtime( true );
561 [ $key1 => $value1, $key2 => $value2 ],
562 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
563 "Result array populated even with new check keys"
565 $t1 = $cache->getCheckKeyTime( $cKey1 );
566 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
567 $t2 = $cache->getCheckKeyTime( $cKey2 );
568 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
569 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
570 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
571 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
576 [ $key1 => $value1, $key2 => $value2 ],
577 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
578 "Result array still populated even with new check keys"
580 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
581 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
582 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
586 * @covers WANObjectCache::getMulti()
587 * @covers WANObjectCache::processCheckKeys()
589 public function testGetMultiCheckKeys() {
590 $cache = $this->cache
;
592 $checkAll = wfRandomString();
593 $check1 = wfRandomString();
594 $check2 = wfRandomString();
595 $check3 = wfRandomString();
596 $value1 = wfRandomString();
597 $value2 = wfRandomString();
599 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
600 // several seconds during the test to assert the behaviour.
601 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
602 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
606 $cache->set( 'key1', $value1, 10 );
607 $cache->set( 'key2', $value2, 10 );
610 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
617 [ 'key1' => $value1, 'key2' => $value2 ],
621 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
622 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
623 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
624 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
626 $cache->touchCheckKey( $check1 );
629 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
636 [ 'key1' => $value1, 'key2' => $value2 ],
638 'key1 expired by check1, but value still provided'
640 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
641 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
643 $cache->touchCheckKey( $checkAll );
646 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
653 [ 'key1' => $value1, 'key2' => $value2 ],
655 'All keys expired by checkAll, but value still provided'
657 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
658 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
662 * @covers WANObjectCache::get()
663 * @covers WANObjectCache::processCheckKeys()
665 public function testCheckKeyInitHoldoff() {
666 $cache = $this->cache
;
668 for ( $i = 0; $i < 500; ++
$i ) {
669 $key = wfRandomString();
670 $checkKey = wfRandomString();
672 $cache->get( $key, $curTTL, [ $checkKey ] );
673 $cache->set( $key, 'val', 10 );
675 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
677 $this->assertEquals( 'val', $v );
678 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
681 for ( $i = 0; $i < 500; ++
$i ) {
682 $key = wfRandomString();
683 $checkKey = wfRandomString();
685 $cache->set( $key, 'val', 10 );
687 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
689 $this->assertEquals( 'val', $v );
690 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
695 * @covers WANObjectCache::delete()
697 public function testDelete() {
698 $key = wfRandomString();
699 $value = wfRandomString();
700 $this->cache
->set( $key, $value );
703 $v = $this->cache
->get( $key, $curTTL );
704 $this->assertEquals( $value, $v, "Key was created with value" );
705 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
707 $this->cache
->delete( $key );
710 $v = $this->cache
->get( $key, $curTTL );
711 $this->assertFalse( $v, "Deleted key has false value" );
712 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
714 $this->cache
->set( $key, $value . 'more' );
715 $v = $this->cache
->get( $key, $curTTL );
716 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
717 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
719 $this->cache
->set( $key, $value );
720 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
723 $v = $this->cache
->get( $key, $curTTL );
724 $this->assertFalse( $v, "Deleted key has false value" );
725 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
727 $this->cache
->set( $key, $value );
728 $v = $this->cache
->get( $key, $curTTL );
729 $this->assertEquals( $value, $v, "Key was created with value" );
730 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
734 * @dataProvider getWithSetCallback_versions_provider
735 * @param array $extOpts
738 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
739 $cache = $this->cache
;
741 $key = wfRandomString();
742 $value = wfRandomString();
745 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
750 // Set the main key (version N if versioned)
752 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
753 $this->assertEquals( $value, $v, "Value returned" );
754 $this->assertEquals( 1, $wasSet, "Value regenerated" );
755 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
756 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
757 // Set the key for version N+1 (if versioned)
759 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
762 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
763 $this->assertEquals( $value, $v, "Value returned" );
764 $this->assertEquals( 1, $wasSet, "Value regenerated" );
767 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
768 $this->assertEquals( $value, $v, "Value returned" );
769 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
773 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
774 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
777 $cache->delete( $key );
778 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
779 $this->assertEquals( $value, $v, "Value returned" );
780 $this->assertEquals( 1, $wasSet, "Value regenerated" );
784 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
785 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
786 $this->assertEquals( $value, $v, "Value returned" );
787 $this->assertEquals( 1, $wasSet, "Value regenerated" );
791 public static function getWithSetCallback_versions_provider() {
794 [ [ 'version' => 1 ], true ]
799 * @covers WANObjectCache::touchCheckKey()
800 * @covers WANObjectCache::resetCheckKey()
801 * @covers WANObjectCache::getCheckKeyTime()
803 public function testTouchKeys() {
804 $key = wfRandomString();
806 $priorTime = microtime( true );
808 $t0 = $this->cache
->getCheckKeyTime( $key );
809 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
811 $priorTime = microtime( true );
813 $this->cache
->touchCheckKey( $key );
814 $t1 = $this->cache
->getCheckKeyTime( $key );
815 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
817 $t2 = $this->cache
->getCheckKeyTime( $key );
818 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
821 $this->cache
->touchCheckKey( $key );
822 $t3 = $this->cache
->getCheckKeyTime( $key );
823 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
825 $t4 = $this->cache
->getCheckKeyTime( $key );
826 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
829 $this->cache
->resetCheckKey( $key );
830 $t5 = $this->cache
->getCheckKeyTime( $key );
831 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
833 $t6 = $this->cache
->getCheckKeyTime( $key );
834 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
838 * @covers WANObjectCache::getMulti()
840 public function testGetWithSeveralCheckKeys() {
841 $key = wfRandomString();
842 $tKey1 = wfRandomString();
843 $tKey2 = wfRandomString();
846 // Two check keys are newer (given hold-off) than $key, another is older
847 $this->internalCache
->set(
848 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
849 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
851 $this->internalCache
->set(
852 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
853 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
855 $this->internalCache
->set(
856 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
857 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
859 $this->cache
->set( $key, $value, 30 );
862 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
863 $this->assertEquals( $value, $v, "Value matches" );
864 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
865 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
869 * @covers WANObjectCache::set()
871 public function testSetWithLag() {
874 $key = wfRandomString();
875 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
876 $this->cache
->set( $key, $value, 30, $opts );
877 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
879 $key = wfRandomString();
880 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
881 $this->cache
->set( $key, $value, 30, $opts );
882 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
884 $key = wfRandomString();
885 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
886 $this->cache
->set( $key, $value, 30, $opts );
887 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
891 * @covers WANObjectCache::set()
893 public function testWritePending() {
896 $key = wfRandomString();
897 $opts = [ 'pending' => true ];
898 $this->cache
->set( $key, $value, 30, $opts );
899 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
902 public function testMcRouterSupport() {
903 $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
904 $localBag->expects( $this->never() )->method( 'set' );
905 $localBag->expects( $this->never() )->method( 'delete' );
906 $wanCache = new WANObjectCache( [
907 'cache' => $localBag,
908 'pool' => 'testcache-hash',
909 'relayer' => new EventRelayerNull( [] )
911 $valFunc = function () {
915 // None of these should use broadcasting commands (e.g. SET, DELETE)
916 $wanCache->get( 'x' );
917 $wanCache->get( 'x', $ctl, [ 'check1' ] );
918 $wanCache->getMulti( [ 'x', 'y' ] );
919 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
920 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
921 $wanCache->getCheckKeyTime( 'zzz' );
925 * @dataProvider provideAdaptiveTTL
926 * @covers WANObjectCache::adaptiveTTL()
927 * @param float|int $ago
930 * @param float $factor
931 * @param int $adaptiveTTL
933 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
934 $mtime = $ago ?
time() - $ago : $ago;
936 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
938 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
939 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
941 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
943 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
944 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
947 public static function provideAdaptiveTTL() {
949 [ 3600, 900, 30, .2, 720 ],
950 [ 3600, 500, 30, .2, 500 ],
951 [ 3600, 86400, 800, .2, 800 ],
952 [ false, 86400, 800, .2, 800 ],
953 [ null, 86400, 800, .2, 800 ]