3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 private $internalCache;
9 protected function setUp() {
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
15 $this->cache
= ObjectCache
::getWANInstance( $name );
17 $this->cache
= new WANObjectCache( [
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( [] )
24 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
25 $this->internalCache
= $wanCache->cache
;
29 * @dataProvider provideSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
35 public function testSetAndGet( $value, $ttl ) {
38 $key = wfRandomString();
40 $this->cache
->get( $key, $curTTL, [], $asOf );
41 $this->assertNull( $curTTL, "Current TTL is null" );
42 $this->assertNull( $asOf, "Current as-of-time is infinite" );
44 $t = microtime( true );
45 $this->cache
->set( $key, $value, $ttl );
47 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
48 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
49 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
51 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
52 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
54 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
55 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
58 public static function provideSetAndGet() {
65 [ (object)[ 'meow' ], 3 ],
73 * @covers WANObjectCache::get()
75 public function testGetNotExists() {
76 $key = wfRandomString();
78 $value = $this->cache
->get( $key, $curTTL );
80 $this->assertFalse( $value, "Non-existing key has false value" );
81 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
85 * @covers WANObjectCache::set()
87 public function testSetOver() {
88 $key = wfRandomString();
89 for ( $i = 0; $i < 3; ++
$i ) {
90 $value = wfRandomString();
91 $this->cache
->set( $key, $value, 3 );
93 $this->assertEquals( $this->cache
->get( $key ), $value );
98 * @covers WANObjectCache::set()
100 public function testStaleSet() {
101 $key = wfRandomString();
102 $value = wfRandomString();
103 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
105 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
108 public function testProcessCache() {
110 $callback = function () use ( &$hit ) {
114 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
115 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
117 foreach ( $keys as $i => $key ) {
118 $this->cache
->getWithSetCallback(
119 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
121 $this->assertEquals( 3, $hit );
123 foreach ( $keys as $i => $key ) {
124 $this->cache
->getWithSetCallback(
125 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
127 $this->assertEquals( 3, $hit, "Values cached" );
129 foreach ( $keys as $i => $key ) {
130 $this->cache
->getWithSetCallback(
131 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
133 $this->assertEquals( 6, $hit );
135 foreach ( $keys as $i => $key ) {
136 $this->cache
->getWithSetCallback(
137 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
139 $this->assertEquals( 6, $hit, "New values cached" );
141 foreach ( $keys as $i => $key ) {
142 $this->cache
->delete( $key );
143 $this->cache
->getWithSetCallback(
144 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
146 $this->assertEquals( 9, $hit, "Values evicted" );
150 * @dataProvider getWithSetCallback_provider
151 * @covers WANObjectCache::getWithSetCallback()
152 * @covers WANObjectCache::doGetWithSetCallback()
153 * @param array $extOpts
154 * @param bool $versioned
156 public function testGetWithSetCallback( array $extOpts, $versioned ) {
157 $cache = $this->cache
;
159 $key = wfRandomString();
160 $value = wfRandomString();
161 $cKey1 = wfRandomString();
162 $cKey2 = wfRandomString();
165 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
167 $ttl = 20; // override with another value
172 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
173 $this->assertEquals( $value, $v, "Value returned" );
174 $this->assertEquals( 1, $wasSet, "Value regenerated" );
177 $cache->get( $key, $curTTL );
178 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
179 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
182 $v = $cache->getWithSetCallback( $key, 30, $func, [
186 $this->assertEquals( $value, $v, "Value returned" );
187 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
189 $priorTime = microtime( true );
192 $v = $cache->getWithSetCallback(
193 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
195 $this->assertEquals( $value, $v, "Value returned" );
196 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
197 $t1 = $cache->getCheckKeyTime( $cKey1 );
198 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
199 $t2 = $cache->getCheckKeyTime( $cKey2 );
200 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
202 $priorTime = microtime( true );
204 $v = $cache->getWithSetCallback(
205 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
207 $this->assertEquals( $value, $v, "Value returned" );
208 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
209 $t1 = $cache->getCheckKeyTime( $cKey1 );
210 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
211 $t2 = $cache->getCheckKeyTime( $cKey2 );
212 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
215 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
217 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
219 $this->assertEquals( $value, $v, "Value returned" );
221 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
224 $key = wfRandomString();
225 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
226 $this->assertEquals( $value, $v, "Value returned" );
227 $cache->delete( $key );
228 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
229 $this->assertEquals( $value, $v, "Value still returned after deleted" );
230 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
233 public static function getWithSetCallback_provider() {
236 [ [ 'version' => 1 ], true ]
241 * @covers WANObjectCache::getWithSetCallback()
242 * @covers WANObjectCache::doGetWithSetCallback()
244 public function testLockTSE() {
245 $cache = $this->cache
;
246 $key = wfRandomString();
247 $value = wfRandomString();
250 $func = function() use ( &$calls, $value, $cache, $key ) {
252 // Immediately kill any mutex rather than waiting a second
253 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
257 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
258 $this->assertEquals( $value, $ret );
259 $this->assertEquals( 1, $calls, 'Value was populated' );
261 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
262 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
264 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
265 $ret = $cache->getWithSetCallback( $key, 30, $func,
266 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
267 $this->assertEquals( $value, $ret, 'Old value used' );
268 $this->assertEquals( 1, $calls, 'Callback was not used' );
270 $cache->delete( $key );
271 $ret = $cache->getWithSetCallback( $key, 30, $func,
272 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
273 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
274 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
276 $ret = $cache->getWithSetCallback( $key, 30, $func,
277 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
278 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
279 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
283 * @covers WANObjectCache::getWithSetCallback()
284 * @covers WANObjectCache::doGetWithSetCallback()
286 public function testLockTSESlow() {
287 $cache = $this->cache
;
288 $key = wfRandomString();
289 $value = wfRandomString();
292 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
294 $setOpts['since'] = microtime( true ) - 10;
295 // Immediately kill any mutex rather than waiting a second
296 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
300 // Value should be marked as stale due to snapshot lag
302 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
303 $this->assertEquals( $value, $ret );
304 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
305 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
306 $this->assertEquals( 1, $calls, 'Value was generated' );
308 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
309 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
310 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
311 $this->assertEquals( $value, $ret );
312 $this->assertEquals( 1, $calls, 'Callback was not used' );
316 * @covers WANObjectCache::getWithSetCallback()
317 * @covers WANObjectCache::doGetWithSetCallback()
319 public function testBusyValue() {
320 $cache = $this->cache
;
321 $key = wfRandomString();
322 $value = wfRandomString();
323 $busyValue = wfRandomString();
326 $func = function() use ( &$calls, $value, $cache, $key ) {
328 // Immediately kill any mutex rather than waiting a second
329 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
333 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
334 $this->assertEquals( $value, $ret );
335 $this->assertEquals( 1, $calls, 'Value was populated' );
337 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
338 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
340 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
341 $ret = $cache->getWithSetCallback( $key, 30, $func,
342 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
343 $this->assertEquals( $value, $ret, 'Callback used' );
344 $this->assertEquals( 2, $calls, 'Callback used' );
346 $ret = $cache->getWithSetCallback( $key, 30, $func,
347 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
348 $this->assertEquals( $value, $ret, 'Old value used' );
349 $this->assertEquals( 2, $calls, 'Callback was not used' );
351 $cache->delete( $key ); // no value at all anymore and still locked
352 $ret = $cache->getWithSetCallback( $key, 30, $func,
353 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
354 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
355 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
357 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
358 $ret = $cache->getWithSetCallback( $key, 30, $func,
359 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
360 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
361 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
363 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
364 $ret = $cache->getWithSetCallback( $key, 30, $func,
365 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
366 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
367 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
371 * @covers WANObjectCache::getMulti()
373 public function testGetMulti() {
374 $cache = $this->cache
;
376 $value1 = [ 'this' => 'is', 'a' => 'test' ];
377 $value2 = [ 'this' => 'is', 'another' => 'test' ];
379 $key1 = wfRandomString();
380 $key2 = wfRandomString();
381 $key3 = wfRandomString();
383 $cache->set( $key1, $value1, 5 );
384 $cache->set( $key2, $value2, 10 );
388 [ $key1 => $value1, $key2 => $value2 ],
389 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
390 'Result array populated'
393 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
394 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
395 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
397 $cKey1 = wfRandomString();
398 $cKey2 = wfRandomString();
400 $priorTime = microtime( true );
404 [ $key1 => $value1, $key2 => $value2 ],
405 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
406 "Result array populated even with new check keys"
408 $t1 = $cache->getCheckKeyTime( $cKey1 );
409 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
410 $t2 = $cache->getCheckKeyTime( $cKey2 );
411 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
412 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
413 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
414 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
419 [ $key1 => $value1, $key2 => $value2 ],
420 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
421 "Result array still populated even with new check keys"
423 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
424 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
425 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
429 * @covers WANObjectCache::getMulti()
430 * @covers WANObjectCache::processCheckKeys()
432 public function testGetMultiCheckKeys() {
433 $cache = $this->cache
;
435 $checkAll = wfRandomString();
436 $check1 = wfRandomString();
437 $check2 = wfRandomString();
438 $check3 = wfRandomString();
439 $value1 = wfRandomString();
440 $value2 = wfRandomString();
442 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
443 // several seconds during the test to assert the behaviour.
444 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
445 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
449 $cache->set( 'key1', $value1, 10 );
450 $cache->set( 'key2', $value2, 10 );
453 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
460 [ 'key1' => $value1, 'key2' => $value2 ],
464 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
465 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
466 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
467 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
469 $cache->touchCheckKey( $check1 );
472 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
479 [ 'key1' => $value1, 'key2' => $value2 ],
481 'key1 expired by check1, but value still provided'
483 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
484 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
486 $cache->touchCheckKey( $checkAll );
489 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
496 [ 'key1' => $value1, 'key2' => $value2 ],
498 'All keys expired by checkAll, but value still provided'
500 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
501 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
505 * @covers WANObjectCache::get()
506 * @covers WANObjectCache::processCheckKeys()
508 public function testCheckKeyInitHoldoff() {
509 $cache = $this->cache
;
511 for ( $i = 0; $i < 500; ++
$i ) {
512 $key = wfRandomString();
513 $checkKey = wfRandomString();
515 $cache->get( $key, $curTTL, [ $checkKey ] );
516 $cache->set( $key, 'val', 10 );
518 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
520 $this->assertEquals( 'val', $v );
521 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
524 for ( $i = 0; $i < 500; ++
$i ) {
525 $key = wfRandomString();
526 $checkKey = wfRandomString();
528 $cache->set( $key, 'val', 10 );
530 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
532 $this->assertEquals( 'val', $v );
533 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
538 * @covers WANObjectCache::delete()
540 public function testDelete() {
541 $key = wfRandomString();
542 $value = wfRandomString();
543 $this->cache
->set( $key, $value );
546 $v = $this->cache
->get( $key, $curTTL );
547 $this->assertEquals( $value, $v, "Key was created with value" );
548 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
550 $this->cache
->delete( $key );
553 $v = $this->cache
->get( $key, $curTTL );
554 $this->assertFalse( $v, "Deleted key has false value" );
555 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
557 $this->cache
->set( $key, $value . 'more' );
558 $v = $this->cache
->get( $key, $curTTL );
559 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
560 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
562 $this->cache
->set( $key, $value );
563 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
566 $v = $this->cache
->get( $key, $curTTL );
567 $this->assertFalse( $v, "Deleted key has false value" );
568 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
570 $this->cache
->set( $key, $value );
571 $v = $this->cache
->get( $key, $curTTL );
572 $this->assertEquals( $value, $v, "Key was created with value" );
573 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
577 * @dataProvider getWithSetCallback_versions_provider
578 * @param array $extOpts
581 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
582 $cache = $this->cache
;
584 $key = wfRandomString();
585 $value = wfRandomString();
588 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
593 // Set the main key (version N if versioned)
595 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
596 $this->assertEquals( $value, $v, "Value returned" );
597 $this->assertEquals( 1, $wasSet, "Value regenerated" );
598 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
599 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
600 // Set the key for version N+1 (if versioned)
602 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
605 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
606 $this->assertEquals( $value, $v, "Value returned" );
607 $this->assertEquals( 1, $wasSet, "Value regenerated" );
610 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
611 $this->assertEquals( $value, $v, "Value returned" );
612 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
616 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
617 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
620 $cache->delete( $key );
621 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
622 $this->assertEquals( $value, $v, "Value returned" );
623 $this->assertEquals( 1, $wasSet, "Value regenerated" );
627 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
628 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
629 $this->assertEquals( $value, $v, "Value returned" );
630 $this->assertEquals( 1, $wasSet, "Value regenerated" );
634 public static function getWithSetCallback_versions_provider() {
637 [ [ 'version' => 1 ], true ]
642 * @covers WANObjectCache::touchCheckKey()
643 * @covers WANObjectCache::resetCheckKey()
644 * @covers WANObjectCache::getCheckKeyTime()
646 public function testTouchKeys() {
647 $key = wfRandomString();
649 $priorTime = microtime( true );
651 $t0 = $this->cache
->getCheckKeyTime( $key );
652 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
654 $priorTime = microtime( true );
656 $this->cache
->touchCheckKey( $key );
657 $t1 = $this->cache
->getCheckKeyTime( $key );
658 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
660 $t2 = $this->cache
->getCheckKeyTime( $key );
661 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
664 $this->cache
->touchCheckKey( $key );
665 $t3 = $this->cache
->getCheckKeyTime( $key );
666 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
668 $t4 = $this->cache
->getCheckKeyTime( $key );
669 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
672 $this->cache
->resetCheckKey( $key );
673 $t5 = $this->cache
->getCheckKeyTime( $key );
674 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
676 $t6 = $this->cache
->getCheckKeyTime( $key );
677 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
681 * @covers WANObjectCache::getMulti()
683 public function testGetWithSeveralCheckKeys() {
684 $key = wfRandomString();
685 $tKey1 = wfRandomString();
686 $tKey2 = wfRandomString();
689 // Two check keys are newer (given hold-off) than $key, another is older
690 $this->internalCache
->set(
691 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
692 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
694 $this->internalCache
->set(
695 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
696 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
698 $this->internalCache
->set(
699 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
700 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
702 $this->cache
->set( $key, $value, 30 );
705 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
706 $this->assertEquals( $value, $v, "Value matches" );
707 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
708 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
712 * @covers WANObjectCache::set()
714 public function testSetWithLag() {
717 $key = wfRandomString();
718 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
719 $this->cache
->set( $key, $value, 30, $opts );
720 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
722 $key = wfRandomString();
723 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
724 $this->cache
->set( $key, $value, 30, $opts );
725 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
727 $key = wfRandomString();
728 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
729 $this->cache
->set( $key, $value, 30, $opts );
730 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
734 * @covers WANObjectCache::set()
736 public function testWritePending() {
739 $key = wfRandomString();
740 $opts = [ 'pending' => true ];
741 $this->cache
->set( $key, $value, 30, $opts );
742 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
745 public function testMcRouterSupport() {
746 $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
747 $localBag->expects( $this->never() )->method( 'set' );
748 $localBag->expects( $this->never() )->method( 'delete' );
749 $wanCache = new WANObjectCache( [
750 'cache' => $localBag,
751 'pool' => 'testcache-hash',
752 'relayer' => new EventRelayerNull( [] )
754 $valFunc = function () {
758 // None of these should use broadcasting commands (e.g. SET, DELETE)
759 $wanCache->get( 'x' );
760 $wanCache->get( 'x', $ctl, [ 'check1' ] );
761 $wanCache->getMulti( [ 'x', 'y' ] );
762 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
763 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
764 $wanCache->getCheckKeyTime( 'zzz' );
768 * @dataProvider provideAdaptiveTTL
769 * @covers WANObjectCache::adaptiveTTL()
771 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
772 $mtime = is_int( $ago ) ?
time() - $ago : $ago;
774 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
776 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
777 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
779 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
781 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
782 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
785 public static function provideAdaptiveTTL() {
787 [ 3600, 900, 30, .2, 720 ],
788 [ 3600, 500, 30, .2, 500 ],
789 [ 3600, 86400, 800, .2, 800 ],
790 [ false, 86400, 800, .2, 800 ],
791 [ null, 86400, 800, .2, 800 ]