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();
167 $func = function( $old, &$ttl, &$opts, $asOf )
168 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
173 $ttl = 20; // override with another value
178 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
179 $this->assertEquals( $value, $v, "Value returned" );
180 $this->assertEquals( 1, $wasSet, "Value regenerated" );
181 $this->assertFalse( $priorValue, "No prior value" );
182 $this->assertNull( $priorAsOf, "No prior value" );
185 $cache->get( $key, $curTTL );
186 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
187 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
190 $v = $cache->getWithSetCallback( $key, 30, $func, [
194 $this->assertEquals( $value, $v, "Value returned" );
195 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
197 $priorTime = microtime( true );
200 $v = $cache->getWithSetCallback(
201 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
203 $this->assertEquals( $value, $v, "Value returned" );
204 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
205 $this->assertEquals( $value, $priorValue, "Has prior value" );
206 $this->assertType( 'float', $priorAsOf, "Has prior value" );
207 $t1 = $cache->getCheckKeyTime( $cKey1 );
208 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
209 $t2 = $cache->getCheckKeyTime( $cKey2 );
210 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
212 $priorTime = microtime( true );
214 $v = $cache->getWithSetCallback(
215 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
217 $this->assertEquals( $value, $v, "Value returned" );
218 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
219 $t1 = $cache->getCheckKeyTime( $cKey1 );
220 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
221 $t2 = $cache->getCheckKeyTime( $cKey2 );
222 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
225 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
227 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
229 $this->assertEquals( $value, $v, "Value returned" );
231 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
234 $key = wfRandomString();
235 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
236 $this->assertEquals( $value, $v, "Value returned" );
237 $cache->delete( $key );
238 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
239 $this->assertEquals( $value, $v, "Value still returned after deleted" );
240 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
243 public static function getWithSetCallback_provider() {
246 [ [ 'version' => 1 ], true ]
251 * @covers WANObjectCache::getWithSetCallback()
252 * @covers WANObjectCache::doGetWithSetCallback()
254 public function testLockTSE() {
255 $cache = $this->cache
;
256 $key = wfRandomString();
257 $value = wfRandomString();
260 $func = function() use ( &$calls, $value, $cache, $key ) {
262 // Immediately kill any mutex rather than waiting a second
263 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
267 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
268 $this->assertEquals( $value, $ret );
269 $this->assertEquals( 1, $calls, 'Value was populated' );
271 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
272 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
274 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
275 $ret = $cache->getWithSetCallback( $key, 30, $func,
276 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
277 $this->assertEquals( $value, $ret, 'Old value used' );
278 $this->assertEquals( 1, $calls, 'Callback was not used' );
280 $cache->delete( $key );
281 $ret = $cache->getWithSetCallback( $key, 30, $func,
282 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
283 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
284 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
286 $ret = $cache->getWithSetCallback( $key, 30, $func,
287 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
288 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
289 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
293 * @covers WANObjectCache::getWithSetCallback()
294 * @covers WANObjectCache::doGetWithSetCallback()
296 public function testLockTSESlow() {
297 $cache = $this->cache
;
298 $key = wfRandomString();
299 $value = wfRandomString();
302 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
304 $setOpts['since'] = microtime( true ) - 10;
305 // Immediately kill any mutex rather than waiting a second
306 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
310 // Value should be marked as stale due to snapshot lag
312 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
313 $this->assertEquals( $value, $ret );
314 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
315 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
316 $this->assertEquals( 1, $calls, 'Value was generated' );
318 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
319 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
320 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
321 $this->assertEquals( $value, $ret );
322 $this->assertEquals( 1, $calls, 'Callback was not used' );
326 * @covers WANObjectCache::getWithSetCallback()
327 * @covers WANObjectCache::doGetWithSetCallback()
329 public function testBusyValue() {
330 $cache = $this->cache
;
331 $key = wfRandomString();
332 $value = wfRandomString();
333 $busyValue = wfRandomString();
336 $func = function() use ( &$calls, $value, $cache, $key ) {
338 // Immediately kill any mutex rather than waiting a second
339 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
343 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
344 $this->assertEquals( $value, $ret );
345 $this->assertEquals( 1, $calls, 'Value was populated' );
347 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
348 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
350 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
351 $ret = $cache->getWithSetCallback( $key, 30, $func,
352 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
353 $this->assertEquals( $value, $ret, 'Callback used' );
354 $this->assertEquals( 2, $calls, 'Callback used' );
356 $ret = $cache->getWithSetCallback( $key, 30, $func,
357 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
358 $this->assertEquals( $value, $ret, 'Old value used' );
359 $this->assertEquals( 2, $calls, 'Callback was not used' );
361 $cache->delete( $key ); // no value at all anymore and still locked
362 $ret = $cache->getWithSetCallback( $key, 30, $func,
363 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
364 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
365 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
367 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
368 $ret = $cache->getWithSetCallback( $key, 30, $func,
369 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
370 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
371 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
373 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
374 $ret = $cache->getWithSetCallback( $key, 30, $func,
375 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
376 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
377 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
381 * @covers WANObjectCache::getMulti()
383 public function testGetMulti() {
384 $cache = $this->cache
;
386 $value1 = [ 'this' => 'is', 'a' => 'test' ];
387 $value2 = [ 'this' => 'is', 'another' => 'test' ];
389 $key1 = wfRandomString();
390 $key2 = wfRandomString();
391 $key3 = wfRandomString();
393 $cache->set( $key1, $value1, 5 );
394 $cache->set( $key2, $value2, 10 );
398 [ $key1 => $value1, $key2 => $value2 ],
399 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
400 'Result array populated'
403 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
404 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
405 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
407 $cKey1 = wfRandomString();
408 $cKey2 = wfRandomString();
410 $priorTime = microtime( true );
414 [ $key1 => $value1, $key2 => $value2 ],
415 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
416 "Result array populated even with new check keys"
418 $t1 = $cache->getCheckKeyTime( $cKey1 );
419 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
420 $t2 = $cache->getCheckKeyTime( $cKey2 );
421 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
422 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
423 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
424 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
429 [ $key1 => $value1, $key2 => $value2 ],
430 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
431 "Result array still populated even with new check keys"
433 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
434 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
435 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
439 * @covers WANObjectCache::getMulti()
440 * @covers WANObjectCache::processCheckKeys()
442 public function testGetMultiCheckKeys() {
443 $cache = $this->cache
;
445 $checkAll = wfRandomString();
446 $check1 = wfRandomString();
447 $check2 = wfRandomString();
448 $check3 = wfRandomString();
449 $value1 = wfRandomString();
450 $value2 = wfRandomString();
452 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
453 // several seconds during the test to assert the behaviour.
454 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
455 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
459 $cache->set( 'key1', $value1, 10 );
460 $cache->set( 'key2', $value2, 10 );
463 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
470 [ 'key1' => $value1, 'key2' => $value2 ],
474 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
475 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
476 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
477 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
479 $cache->touchCheckKey( $check1 );
482 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
489 [ 'key1' => $value1, 'key2' => $value2 ],
491 'key1 expired by check1, but value still provided'
493 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
494 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
496 $cache->touchCheckKey( $checkAll );
499 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
506 [ 'key1' => $value1, 'key2' => $value2 ],
508 'All keys expired by checkAll, but value still provided'
510 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
511 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
515 * @covers WANObjectCache::get()
516 * @covers WANObjectCache::processCheckKeys()
518 public function testCheckKeyInitHoldoff() {
519 $cache = $this->cache
;
521 for ( $i = 0; $i < 500; ++
$i ) {
522 $key = wfRandomString();
523 $checkKey = wfRandomString();
525 $cache->get( $key, $curTTL, [ $checkKey ] );
526 $cache->set( $key, 'val', 10 );
528 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
530 $this->assertEquals( 'val', $v );
531 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
534 for ( $i = 0; $i < 500; ++
$i ) {
535 $key = wfRandomString();
536 $checkKey = wfRandomString();
538 $cache->set( $key, 'val', 10 );
540 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
542 $this->assertEquals( 'val', $v );
543 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
548 * @covers WANObjectCache::delete()
550 public function testDelete() {
551 $key = wfRandomString();
552 $value = wfRandomString();
553 $this->cache
->set( $key, $value );
556 $v = $this->cache
->get( $key, $curTTL );
557 $this->assertEquals( $value, $v, "Key was created with value" );
558 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
560 $this->cache
->delete( $key );
563 $v = $this->cache
->get( $key, $curTTL );
564 $this->assertFalse( $v, "Deleted key has false value" );
565 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
567 $this->cache
->set( $key, $value . 'more' );
568 $v = $this->cache
->get( $key, $curTTL );
569 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
570 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
572 $this->cache
->set( $key, $value );
573 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
576 $v = $this->cache
->get( $key, $curTTL );
577 $this->assertFalse( $v, "Deleted key has false value" );
578 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
580 $this->cache
->set( $key, $value );
581 $v = $this->cache
->get( $key, $curTTL );
582 $this->assertEquals( $value, $v, "Key was created with value" );
583 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
587 * @dataProvider getWithSetCallback_versions_provider
588 * @param array $extOpts
591 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
592 $cache = $this->cache
;
594 $key = wfRandomString();
595 $value = wfRandomString();
598 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
603 // Set the main key (version N if versioned)
605 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
606 $this->assertEquals( $value, $v, "Value returned" );
607 $this->assertEquals( 1, $wasSet, "Value regenerated" );
608 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
609 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
610 // Set the key for version N+1 (if versioned)
612 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
615 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
616 $this->assertEquals( $value, $v, "Value returned" );
617 $this->assertEquals( 1, $wasSet, "Value regenerated" );
620 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
621 $this->assertEquals( $value, $v, "Value returned" );
622 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
626 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
627 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
630 $cache->delete( $key );
631 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
632 $this->assertEquals( $value, $v, "Value returned" );
633 $this->assertEquals( 1, $wasSet, "Value regenerated" );
637 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
638 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
639 $this->assertEquals( $value, $v, "Value returned" );
640 $this->assertEquals( 1, $wasSet, "Value regenerated" );
644 public static function getWithSetCallback_versions_provider() {
647 [ [ 'version' => 1 ], true ]
652 * @covers WANObjectCache::touchCheckKey()
653 * @covers WANObjectCache::resetCheckKey()
654 * @covers WANObjectCache::getCheckKeyTime()
656 public function testTouchKeys() {
657 $key = wfRandomString();
659 $priorTime = microtime( true );
661 $t0 = $this->cache
->getCheckKeyTime( $key );
662 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
664 $priorTime = microtime( true );
666 $this->cache
->touchCheckKey( $key );
667 $t1 = $this->cache
->getCheckKeyTime( $key );
668 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
670 $t2 = $this->cache
->getCheckKeyTime( $key );
671 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
674 $this->cache
->touchCheckKey( $key );
675 $t3 = $this->cache
->getCheckKeyTime( $key );
676 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
678 $t4 = $this->cache
->getCheckKeyTime( $key );
679 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
682 $this->cache
->resetCheckKey( $key );
683 $t5 = $this->cache
->getCheckKeyTime( $key );
684 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
686 $t6 = $this->cache
->getCheckKeyTime( $key );
687 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
691 * @covers WANObjectCache::getMulti()
693 public function testGetWithSeveralCheckKeys() {
694 $key = wfRandomString();
695 $tKey1 = wfRandomString();
696 $tKey2 = wfRandomString();
699 // Two check keys are newer (given hold-off) than $key, another is older
700 $this->internalCache
->set(
701 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
702 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
704 $this->internalCache
->set(
705 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
706 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
708 $this->internalCache
->set(
709 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
710 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
712 $this->cache
->set( $key, $value, 30 );
715 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
716 $this->assertEquals( $value, $v, "Value matches" );
717 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
718 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
722 * @covers WANObjectCache::set()
724 public function testSetWithLag() {
727 $key = wfRandomString();
728 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
729 $this->cache
->set( $key, $value, 30, $opts );
730 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
732 $key = wfRandomString();
733 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
734 $this->cache
->set( $key, $value, 30, $opts );
735 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
737 $key = wfRandomString();
738 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
739 $this->cache
->set( $key, $value, 30, $opts );
740 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
744 * @covers WANObjectCache::set()
746 public function testWritePending() {
749 $key = wfRandomString();
750 $opts = [ 'pending' => true ];
751 $this->cache
->set( $key, $value, 30, $opts );
752 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
755 public function testMcRouterSupport() {
756 $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
757 $localBag->expects( $this->never() )->method( 'set' );
758 $localBag->expects( $this->never() )->method( 'delete' );
759 $wanCache = new WANObjectCache( [
760 'cache' => $localBag,
761 'pool' => 'testcache-hash',
762 'relayer' => new EventRelayerNull( [] )
764 $valFunc = function () {
768 // None of these should use broadcasting commands (e.g. SET, DELETE)
769 $wanCache->get( 'x' );
770 $wanCache->get( 'x', $ctl, [ 'check1' ] );
771 $wanCache->getMulti( [ 'x', 'y' ] );
772 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
773 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
774 $wanCache->getCheckKeyTime( 'zzz' );
778 * @dataProvider provideAdaptiveTTL
779 * @covers WANObjectCache::adaptiveTTL()
781 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
782 $mtime = is_int( $ago ) ?
time() - $ago : $ago;
784 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
786 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
787 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
789 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
791 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
792 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
795 public static function provideAdaptiveTTL() {
797 [ 3600, 900, 30, .2, 720 ],
798 [ 3600, 500, 30, .2, 500 ],
799 [ 3600, 86400, 800, .2, 800 ],
800 [ false, 86400, 800, .2, 800 ],
801 [ null, 86400, 800, .2, 800 ]