3 use Wikimedia\TestingAccessWrapper
;
6 * @covers WANObjectCache::wrap
7 * @covers WANObjectCache::unwrap
8 * @covers WANObjectCache::worthRefreshExpiring
9 * @covers WANObjectCache::worthRefreshPopular
10 * @covers WANObjectCache::isValid
11 * @covers WANObjectCache::getWarmupKeyMisses
12 * @covers WANObjectCache::prefixCacheKeys
13 * @covers WANObjectCache::getProcessCache
14 * @covers WANObjectCache::getNonProcessCachedKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
19 class WANObjectCacheTest
extends PHPUnit_Framework_TestCase
{
20 /** @var WANObjectCache */
23 private $internalCache;
25 protected function setUp() {
28 $this->cache
= new WANObjectCache( [
29 'cache' => new HashBagOStuff(),
30 'pool' => 'testcache-hash',
31 'relayer' => new EventRelayerNull( [] )
34 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
35 /** @noinspection PhpUndefinedFieldInspection */
36 $this->internalCache
= $wanCache->cache
;
40 * @dataProvider provideSetAndGet
41 * @covers WANObjectCache::set()
42 * @covers WANObjectCache::get()
43 * @covers WANObjectCache::makeKey()
47 public function testSetAndGet( $value, $ttl ) {
50 $key = $this->cache
->makeKey( 'x', wfRandomString() );
52 $this->cache
->get( $key, $curTTL, [], $asOf );
53 $this->assertNull( $curTTL, "Current TTL is null" );
54 $this->assertNull( $asOf, "Current as-of-time is infinite" );
56 $t = microtime( true );
57 $this->cache
->set( $key, $value, $ttl );
59 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
60 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
61 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
63 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
64 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
66 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
67 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
70 public static function provideSetAndGet() {
77 [ (object)[ 'meow' ], 3 ],
85 * @covers WANObjectCache::get()
86 * @covers WANObjectCache::makeGlobalKey()
88 public function testGetNotExists() {
89 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
91 $value = $this->cache
->get( $key, $curTTL );
93 $this->assertFalse( $value, "Non-existing key has false value" );
94 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
98 * @covers WANObjectCache::set()
100 public function testSetOver() {
101 $key = wfRandomString();
102 for ( $i = 0; $i < 3; ++
$i ) {
103 $value = wfRandomString();
104 $this->cache
->set( $key, $value, 3 );
106 $this->assertEquals( $this->cache
->get( $key ), $value );
111 * @covers WANObjectCache::set()
113 public function testStaleSet() {
114 $key = wfRandomString();
115 $value = wfRandomString();
116 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
118 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
121 public function testProcessCache() {
123 $callback = function () use ( &$hit ) {
127 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
128 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
130 foreach ( $keys as $i => $key ) {
131 $this->cache
->getWithSetCallback(
132 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
134 $this->assertEquals( 3, $hit );
136 foreach ( $keys as $i => $key ) {
137 $this->cache
->getWithSetCallback(
138 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
140 $this->assertEquals( 3, $hit, "Values cached" );
142 foreach ( $keys as $i => $key ) {
143 $this->cache
->getWithSetCallback(
144 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
146 $this->assertEquals( 6, $hit );
148 foreach ( $keys as $i => $key ) {
149 $this->cache
->getWithSetCallback(
150 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
152 $this->assertEquals( 6, $hit, "New values cached" );
154 foreach ( $keys as $i => $key ) {
155 $this->cache
->delete( $key );
156 $this->cache
->getWithSetCallback(
157 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
159 $this->assertEquals( 9, $hit, "Values evicted" );
161 $key = reset( $keys );
162 // Get into cache (default process cache group)
163 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
164 $this->assertEquals( 10, $hit, "Value calculated" );
165 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
166 $this->assertEquals( 10, $hit, "Value cached" );
167 $outerCallback = function () use ( &$callback, $key ) {
168 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
172 // Outer key misses and refuses inner key process cache value
173 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
174 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
178 * @dataProvider getWithSetCallback_provider
179 * @covers WANObjectCache::getWithSetCallback()
180 * @covers WANObjectCache::doGetWithSetCallback()
181 * @param array $extOpts
182 * @param bool $versioned
184 public function testGetWithSetCallback( array $extOpts, $versioned ) {
185 $cache = $this->cache
;
187 $key = wfRandomString();
188 $value = wfRandomString();
189 $cKey1 = wfRandomString();
190 $cKey2 = wfRandomString();
195 $func = function ( $old, &$ttl, &$opts, $asOf )
196 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
201 $ttl = 20; // override with another value
206 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
207 $this->assertEquals( $value, $v, "Value returned" );
208 $this->assertEquals( 1, $wasSet, "Value regenerated" );
209 $this->assertFalse( $priorValue, "No prior value" );
210 $this->assertNull( $priorAsOf, "No prior value" );
213 $cache->get( $key, $curTTL );
214 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
215 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
218 $v = $cache->getWithSetCallback( $key, 30, $func, [
222 $this->assertEquals( $value, $v, "Value returned" );
223 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
225 $priorTime = microtime( true );
228 $v = $cache->getWithSetCallback(
229 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
231 $this->assertEquals( $value, $v, "Value returned" );
232 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
233 $this->assertEquals( $value, $priorValue, "Has prior value" );
234 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
235 $t1 = $cache->getCheckKeyTime( $cKey1 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
237 $t2 = $cache->getCheckKeyTime( $cKey2 );
238 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
240 $priorTime = microtime( true );
242 $v = $cache->getWithSetCallback(
243 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
245 $this->assertEquals( $value, $v, "Value returned" );
246 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
247 $t1 = $cache->getCheckKeyTime( $cKey1 );
248 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
249 $t2 = $cache->getCheckKeyTime( $cKey2 );
250 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
253 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
255 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
257 $this->assertEquals( $value, $v, "Value returned" );
259 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
262 $key = wfRandomString();
263 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
264 $this->assertEquals( $value, $v, "Value returned" );
265 $cache->delete( $key );
266 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
267 $this->assertEquals( $value, $v, "Value still returned after deleted" );
268 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
271 public static function getWithSetCallback_provider() {
274 [ [ 'version' => 1 ], true ]
279 * @covers WANObjectCache::getWithSetCallback()
280 * @covers WANObjectCache::doGetWithSetCallback()
282 public function testGetWithSetCallback_invalidCallback() {
283 $this->setExpectedException( InvalidArgumentException
::class );
284 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
288 * @dataProvider getMultiWithSetCallback_provider
289 * @covers WANObjectCache::getMultiWithSetCallback
290 * @covers WANObjectCache::makeMultiKeys
291 * @covers WANObjectCache::getMulti
292 * @param array $extOpts
293 * @param bool $versioned
295 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
296 $cache = $this->cache
;
298 $keyA = wfRandomString();
299 $keyB = wfRandomString();
300 $keyC = wfRandomString();
301 $cKey1 = wfRandomString();
302 $cKey2 = wfRandomString();
307 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
308 &$wasSet, &$priorValue, &$priorAsOf
313 $ttl = 20; // override with another value
318 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
320 $v = $cache->getMultiWithSetCallback(
321 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
322 $this->assertEquals( $value, $v[$keyA], "Value returned" );
323 $this->assertEquals( 1, $wasSet, "Value regenerated" );
324 $this->assertFalse( $priorValue, "No prior value" );
325 $this->assertNull( $priorAsOf, "No prior value" );
328 $cache->get( $keyA, $curTTL );
329 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
330 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
334 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
335 $v = $cache->getMultiWithSetCallback(
336 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
337 $this->assertEquals( $value, $v[$keyB], "Value returned" );
338 $this->assertEquals( 1, $wasSet, "Value regenerated" );
339 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
340 $v = $cache->getMultiWithSetCallback(
341 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
342 $this->assertEquals( $value, $v[$keyB], "Value returned" );
343 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
344 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
346 $priorTime = microtime( true );
349 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
350 $v = $cache->getMultiWithSetCallback(
351 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
353 $this->assertEquals( $value, $v[$keyB], "Value returned" );
354 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
355 $this->assertEquals( $value, $priorValue, "Has prior value" );
356 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
357 $t1 = $cache->getCheckKeyTime( $cKey1 );
358 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
359 $t2 = $cache->getCheckKeyTime( $cKey2 );
360 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
362 $priorTime = microtime( true );
365 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
366 $v = $cache->getMultiWithSetCallback(
367 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
369 $this->assertEquals( $value, $v[$keyC], "Value returned" );
370 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
371 $t1 = $cache->getCheckKeyTime( $cKey1 );
372 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
373 $t2 = $cache->getCheckKeyTime( $cKey2 );
374 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
377 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
379 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
381 $this->assertEquals( $value, $v, "Value returned" );
383 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
386 $key = wfRandomString();
387 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
388 $v = $cache->getMultiWithSetCallback(
389 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
390 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
391 $cache->delete( $key );
392 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
393 $v = $cache->getMultiWithSetCallback(
394 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
395 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
396 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
399 $ids = [ 1, 2, 3, 4, 5, 6 ];
400 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
401 return $wanCache->makeKey( 'test', $id );
403 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
404 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
409 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
412 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
413 array_values( $values ),
414 "Correct values in correct order"
417 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
418 array_keys( $values ),
419 "Correct keys in correct order"
421 $this->assertEquals( count( $ids ), $calls );
423 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
424 $this->assertEquals( count( $ids ), $calls, "Values cached" );
426 // Mock the BagOStuff to assure only one getMulti() call given process caching
427 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
428 ->setMethods( [ 'getMulti' ] )->getMock();
429 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
430 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
431 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
433 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
435 // Warm the process cache
436 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
438 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
439 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
441 // Use the process cache
443 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
444 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
448 public static function getMultiWithSetCallback_provider() {
451 [ [ 'version' => 1 ], true ]
456 * @dataProvider getMultiWithUnionSetCallback_provider
457 * @covers WANObjectCache::getMultiWithUnionSetCallback()
458 * @covers WANObjectCache::makeMultiKeys()
459 * @param array $extOpts
460 * @param bool $versioned
462 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
463 $cache = $this->cache
;
465 $keyA = wfRandomString();
466 $keyB = wfRandomString();
467 $keyC = wfRandomString();
468 $cKey1 = wfRandomString();
469 $cKey2 = wfRandomString();
472 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
473 &$wasSet, &$priorValue, &$priorAsOf
476 foreach ( $ids as $id ) {
478 $newValues[$id] = "@$id$";
479 $ttls[$id] = 20; // override with another value
486 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
488 $v = $cache->getMultiWithUnionSetCallback(
489 $keyedIds, 30, $genFunc, $extOpts );
490 $this->assertEquals( $value, $v[$keyA], "Value returned" );
491 $this->assertEquals( 1, $wasSet, "Value regenerated" );
494 $cache->get( $keyA, $curTTL );
495 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
496 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
500 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
501 $v = $cache->getMultiWithUnionSetCallback(
502 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
503 $this->assertEquals( $value, $v[$keyB], "Value returned" );
504 $this->assertEquals( 1, $wasSet, "Value regenerated" );
505 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
506 $v = $cache->getMultiWithUnionSetCallback(
507 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
508 $this->assertEquals( $value, $v[$keyB], "Value returned" );
509 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
510 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
512 $priorTime = microtime( true );
515 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
516 $v = $cache->getMultiWithUnionSetCallback(
517 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
519 $this->assertEquals( $value, $v[$keyB], "Value returned" );
520 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
521 $t1 = $cache->getCheckKeyTime( $cKey1 );
522 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
523 $t2 = $cache->getCheckKeyTime( $cKey2 );
524 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
526 $priorTime = microtime( true );
529 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
530 $v = $cache->getMultiWithUnionSetCallback(
531 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
533 $this->assertEquals( $value, $v[$keyC], "Value returned" );
534 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
535 $t1 = $cache->getCheckKeyTime( $cKey1 );
536 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
537 $t2 = $cache->getCheckKeyTime( $cKey2 );
538 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
541 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
543 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
545 $this->assertEquals( $value, $v, "Value returned" );
547 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
550 $key = wfRandomString();
551 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
552 $v = $cache->getMultiWithUnionSetCallback(
553 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
554 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
555 $cache->delete( $key );
556 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
557 $v = $cache->getMultiWithUnionSetCallback(
558 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
559 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
560 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
563 $ids = [ 1, 2, 3, 4, 5, 6 ];
564 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
565 return $wanCache->makeKey( 'test', $id );
567 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
568 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
570 foreach ( $ids as $id ) {
572 $newValues[$id] = "val-{$id}";
577 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
580 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
581 array_values( $values ),
582 "Correct values in correct order"
585 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
586 array_keys( $values ),
587 "Correct keys in correct order"
589 $this->assertEquals( count( $ids ), $calls );
591 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
592 $this->assertEquals( count( $ids ), $calls, "Values cached" );
595 public static function getMultiWithUnionSetCallback_provider() {
598 [ [ 'version' => 1 ], true ]
603 * @covers WANObjectCache::getWithSetCallback()
604 * @covers WANObjectCache::doGetWithSetCallback()
606 public function testLockTSE() {
607 $cache = $this->cache
;
608 $key = wfRandomString();
609 $value = wfRandomString();
612 $func = function () use ( &$calls, $value, $cache, $key ) {
614 // Immediately kill any mutex rather than waiting a second
615 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
619 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
620 $this->assertEquals( $value, $ret );
621 $this->assertEquals( 1, $calls, 'Value was populated' );
623 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
624 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
626 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
627 $ret = $cache->getWithSetCallback( $key, 30, $func,
628 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
629 $this->assertEquals( $value, $ret, 'Old value used' );
630 $this->assertEquals( 1, $calls, 'Callback was not used' );
632 $cache->delete( $key );
633 $ret = $cache->getWithSetCallback( $key, 30, $func,
634 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
635 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
636 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
638 $ret = $cache->getWithSetCallback( $key, 30, $func,
639 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
640 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
641 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
645 * @covers WANObjectCache::getWithSetCallback()
646 * @covers WANObjectCache::doGetWithSetCallback()
647 * @covers WANObjectCache::set()
649 public function testLockTSESlow() {
650 $cache = $this->cache
;
651 $key = wfRandomString();
652 $value = wfRandomString();
655 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
657 $setOpts['since'] = microtime( true ) - 10;
658 // Immediately kill any mutex rather than waiting a second
659 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
663 // Value should be marked as stale due to snapshot lag
665 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
666 $this->assertEquals( $value, $ret );
667 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
668 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
669 $this->assertEquals( 1, $calls, 'Value was generated' );
671 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
672 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
673 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
674 $this->assertEquals( $value, $ret );
675 $this->assertEquals( 1, $calls, 'Callback was not used' );
679 * @covers WANObjectCache::getWithSetCallback()
680 * @covers WANObjectCache::doGetWithSetCallback()
682 public function testBusyValue() {
683 $cache = $this->cache
;
684 $key = wfRandomString();
685 $value = wfRandomString();
686 $busyValue = wfRandomString();
689 $func = function () use ( &$calls, $value, $cache, $key ) {
691 // Immediately kill any mutex rather than waiting a second
692 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
696 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
697 $this->assertEquals( $value, $ret );
698 $this->assertEquals( 1, $calls, 'Value was populated' );
700 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
701 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
703 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
704 $ret = $cache->getWithSetCallback( $key, 30, $func,
705 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
706 $this->assertEquals( $value, $ret, 'Callback used' );
707 $this->assertEquals( 2, $calls, 'Callback used' );
709 $ret = $cache->getWithSetCallback( $key, 30, $func,
710 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
711 $this->assertEquals( $value, $ret, 'Old value used' );
712 $this->assertEquals( 2, $calls, 'Callback was not used' );
714 $cache->delete( $key ); // no value at all anymore and still locked
715 $ret = $cache->getWithSetCallback( $key, 30, $func,
716 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
717 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
718 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
720 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
721 $ret = $cache->getWithSetCallback( $key, 30, $func,
722 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
723 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
724 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
726 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
727 $ret = $cache->getWithSetCallback( $key, 30, $func,
728 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
729 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
730 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
734 * @covers WANObjectCache::getMulti()
736 public function testGetMulti() {
737 $cache = $this->cache
;
739 $value1 = [ 'this' => 'is', 'a' => 'test' ];
740 $value2 = [ 'this' => 'is', 'another' => 'test' ];
742 $key1 = wfRandomString();
743 $key2 = wfRandomString();
744 $key3 = wfRandomString();
746 $cache->set( $key1, $value1, 5 );
747 $cache->set( $key2, $value2, 10 );
751 [ $key1 => $value1, $key2 => $value2 ],
752 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
753 'Result array populated'
756 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
757 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
758 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
760 $cKey1 = wfRandomString();
761 $cKey2 = wfRandomString();
763 $priorTime = microtime( true );
767 [ $key1 => $value1, $key2 => $value2 ],
768 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
769 "Result array populated even with new check keys"
771 $t1 = $cache->getCheckKeyTime( $cKey1 );
772 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
773 $t2 = $cache->getCheckKeyTime( $cKey2 );
774 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
775 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
776 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
777 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
782 [ $key1 => $value1, $key2 => $value2 ],
783 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
784 "Result array still populated even with new check keys"
786 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
787 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
788 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
792 * @covers WANObjectCache::getMulti()
793 * @covers WANObjectCache::processCheckKeys()
795 public function testGetMultiCheckKeys() {
796 $cache = $this->cache
;
798 $checkAll = wfRandomString();
799 $check1 = wfRandomString();
800 $check2 = wfRandomString();
801 $check3 = wfRandomString();
802 $value1 = wfRandomString();
803 $value2 = wfRandomString();
805 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
806 // several seconds during the test to assert the behaviour.
807 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
808 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
812 $cache->set( 'key1', $value1, 10 );
813 $cache->set( 'key2', $value2, 10 );
816 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
823 [ 'key1' => $value1, 'key2' => $value2 ],
827 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
828 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
829 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
830 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
832 $cache->touchCheckKey( $check1 );
835 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
842 [ 'key1' => $value1, 'key2' => $value2 ],
844 'key1 expired by check1, but value still provided'
846 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
847 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
849 $cache->touchCheckKey( $checkAll );
852 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
859 [ 'key1' => $value1, 'key2' => $value2 ],
861 'All keys expired by checkAll, but value still provided'
863 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
864 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
868 * @covers WANObjectCache::get()
869 * @covers WANObjectCache::processCheckKeys()
871 public function testCheckKeyInitHoldoff() {
872 $cache = $this->cache
;
874 for ( $i = 0; $i < 500; ++
$i ) {
875 $key = wfRandomString();
876 $checkKey = wfRandomString();
878 $cache->get( $key, $curTTL, [ $checkKey ] );
879 $cache->set( $key, 'val', 10 );
881 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
883 $this->assertEquals( 'val', $v );
884 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
887 for ( $i = 0; $i < 500; ++
$i ) {
888 $key = wfRandomString();
889 $checkKey = wfRandomString();
891 $cache->set( $key, 'val', 10 );
893 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
895 $this->assertEquals( 'val', $v );
896 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
901 * @covers WANObjectCache::delete
902 * @covers WANObjectCache::relayDelete
903 * @covers WANObjectCache::relayPurge
905 public function testDelete() {
906 $key = wfRandomString();
907 $value = wfRandomString();
908 $this->cache
->set( $key, $value );
911 $v = $this->cache
->get( $key, $curTTL );
912 $this->assertEquals( $value, $v, "Key was created with value" );
913 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
915 $this->cache
->delete( $key );
918 $v = $this->cache
->get( $key, $curTTL );
919 $this->assertFalse( $v, "Deleted key has false value" );
920 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
922 $this->cache
->set( $key, $value . 'more' );
923 $v = $this->cache
->get( $key, $curTTL );
924 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
925 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
927 $this->cache
->set( $key, $value );
928 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
931 $v = $this->cache
->get( $key, $curTTL );
932 $this->assertFalse( $v, "Deleted key has false value" );
933 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
935 $this->cache
->set( $key, $value );
936 $v = $this->cache
->get( $key, $curTTL );
937 $this->assertEquals( $value, $v, "Key was created with value" );
938 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
942 * @dataProvider getWithSetCallback_versions_provider
943 * @covers WANObjectCache::getWithSetCallback()
944 * @covers WANObjectCache::doGetWithSetCallback()
945 * @param array $extOpts
946 * @param bool $versioned
948 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
949 $cache = $this->cache
;
951 $key = wfRandomString();
952 $value = wfRandomString();
955 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
960 // Set the main key (version N if versioned)
962 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
963 $this->assertEquals( $value, $v, "Value returned" );
964 $this->assertEquals( 1, $wasSet, "Value regenerated" );
965 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
966 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
967 // Set the key for version N+1 (if versioned)
969 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
972 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
973 $this->assertEquals( $value, $v, "Value returned" );
974 $this->assertEquals( 1, $wasSet, "Value regenerated" );
977 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
978 $this->assertEquals( $value, $v, "Value returned" );
979 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
983 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
984 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
987 $cache->delete( $key );
988 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
989 $this->assertEquals( $value, $v, "Value returned" );
990 $this->assertEquals( 1, $wasSet, "Value regenerated" );
994 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
995 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
996 $this->assertEquals( $value, $v, "Value returned" );
997 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1001 public static function getWithSetCallback_versions_provider() {
1004 [ [ 'version' => 1 ], true ]
1009 * @covers WANObjectCache::touchCheckKey
1010 * @covers WANObjectCache::resetCheckKey
1011 * @covers WANObjectCache::getCheckKeyTime
1012 * @covers WANObjectCache::makePurgeValue
1013 * @covers WANObjectCache::parsePurgeValue
1015 public function testTouchKeys() {
1016 $key = wfRandomString();
1018 $priorTime = microtime( true );
1020 $t0 = $this->cache
->getCheckKeyTime( $key );
1021 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1023 $priorTime = microtime( true );
1025 $this->cache
->touchCheckKey( $key );
1026 $t1 = $this->cache
->getCheckKeyTime( $key );
1027 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1029 $t2 = $this->cache
->getCheckKeyTime( $key );
1030 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1033 $this->cache
->touchCheckKey( $key );
1034 $t3 = $this->cache
->getCheckKeyTime( $key );
1035 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1037 $t4 = $this->cache
->getCheckKeyTime( $key );
1038 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1041 $this->cache
->resetCheckKey( $key );
1042 $t5 = $this->cache
->getCheckKeyTime( $key );
1043 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1045 $t6 = $this->cache
->getCheckKeyTime( $key );
1046 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1050 * @covers WANObjectCache::getMulti()
1052 public function testGetWithSeveralCheckKeys() {
1053 $key = wfRandomString();
1054 $tKey1 = wfRandomString();
1055 $tKey2 = wfRandomString();
1058 // Two check keys are newer (given hold-off) than $key, another is older
1059 $this->internalCache
->set(
1060 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1061 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1063 $this->internalCache
->set(
1064 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1065 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1067 $this->internalCache
->set(
1068 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1069 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1071 $this->cache
->set( $key, $value, 30 );
1074 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1075 $this->assertEquals( $value, $v, "Value matches" );
1076 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1077 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1081 * @covers WANObjectCache::reap()
1082 * @covers WANObjectCache::reapCheckKey()
1084 public function testReap() {
1085 $vKey1 = wfRandomString();
1086 $vKey2 = wfRandomString();
1087 $tKey1 = wfRandomString();
1088 $tKey2 = wfRandomString();
1091 $knownPurge = time() - 60;
1092 $goodTime = microtime( true ) - 5;
1093 $badTime = microtime( true ) - 300;
1095 $this->internalCache
->set(
1096 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1098 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1099 WANObjectCache
::FLD_VALUE
=> $value,
1100 WANObjectCache
::FLD_TTL
=> 3600,
1101 WANObjectCache
::FLD_TIME
=> $goodTime
1104 $this->internalCache
->set(
1105 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1107 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1108 WANObjectCache
::FLD_VALUE
=> $value,
1109 WANObjectCache
::FLD_TTL
=> 3600,
1110 WANObjectCache
::FLD_TIME
=> $badTime
1113 $this->internalCache
->set(
1114 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1115 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1117 $this->internalCache
->set(
1118 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1119 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1122 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1123 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1124 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1125 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1127 $this->assertFalse( $bad1 );
1128 $this->assertTrue( $bad2 );
1130 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1131 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1132 $this->assertFalse( $tBad1 );
1133 $this->assertTrue( $tBad2 );
1137 * @covers WANObjectCache::reap()
1139 public function testReap_fail() {
1140 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1141 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1142 $backend->expects( $this->once() )->method( 'get' )
1144 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1145 WANObjectCache
::FLD_VALUE
=> 'value',
1146 WANObjectCache
::FLD_TTL
=> 3600,
1147 WANObjectCache
::FLD_TIME
=> 300,
1149 $backend->expects( $this->once() )->method( 'changeTTL' )
1150 ->willReturn( false );
1152 $wanCache = new WANObjectCache( [
1153 'cache' => $backend,
1154 'pool' => 'testcache-hash',
1155 'relayer' => new EventRelayerNull( [] )
1159 $ret = $wanCache->reap( 'key', 360, $isStale );
1160 $this->assertTrue( $isStale, 'value was stale' );
1161 $this->assertFalse( $ret, 'changeTTL failed' );
1165 * @covers WANObjectCache::set()
1167 public function testSetWithLag() {
1170 $key = wfRandomString();
1171 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1172 $this->cache
->set( $key, $value, 30, $opts );
1173 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1175 $key = wfRandomString();
1176 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1177 $this->cache
->set( $key, $value, 30, $opts );
1178 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1180 $key = wfRandomString();
1181 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1182 $this->cache
->set( $key, $value, 30, $opts );
1183 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1187 * @covers WANObjectCache::set()
1189 public function testWritePending() {
1192 $key = wfRandomString();
1193 $opts = [ 'pending' => true ];
1194 $this->cache
->set( $key, $value, 30, $opts );
1195 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1198 public function testMcRouterSupport() {
1199 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1200 ->setMethods( [ 'set', 'delete' ] )->getMock();
1201 $localBag->expects( $this->never() )->method( 'set' );
1202 $localBag->expects( $this->never() )->method( 'delete' );
1203 $wanCache = new WANObjectCache( [
1204 'cache' => $localBag,
1205 'pool' => 'testcache-hash',
1206 'relayer' => new EventRelayerNull( [] )
1208 $valFunc = function () {
1212 // None of these should use broadcasting commands (e.g. SET, DELETE)
1213 $wanCache->get( 'x' );
1214 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1215 $wanCache->getMulti( [ 'x', 'y' ] );
1216 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1217 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1218 $wanCache->getCheckKeyTime( 'zzz' );
1219 $wanCache->reap( 'x', time() - 300 );
1220 $wanCache->reap( 'zzz', time() - 300 );
1224 * @dataProvider provideAdaptiveTTL
1225 * @covers WANObjectCache::adaptiveTTL()
1226 * @param float|int $ago
1227 * @param int $maxTTL
1228 * @param int $minTTL
1229 * @param float $factor
1230 * @param int $adaptiveTTL
1232 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1233 $mtime = $ago ?
time() - $ago : $ago;
1235 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1237 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1238 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1240 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1242 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1243 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1246 public static function provideAdaptiveTTL() {
1248 [ 3600, 900, 30, 0.2, 720 ],
1249 [ 3600, 500, 30, 0.2, 500 ],
1250 [ 3600, 86400, 800, 0.2, 800 ],
1251 [ false, 86400, 800, 0.2, 800 ],
1252 [ null, 86400, 800, 0.2, 800 ]
1257 * @covers WANObjectCache::__construct
1258 * @covers WANObjectCache::newEmpty
1260 public function testNewEmpty() {
1261 $this->assertInstanceOf(
1262 WANObjectCache
::class,
1263 WANObjectCache
::newEmpty()
1268 * @covers WANObjectCache::setLogger
1270 public function testSetLogger() {
1271 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1275 * @covers WANObjectCache::getQoS
1277 public function testGetQoS() {
1278 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1279 ->setMethods( [ 'getQoS' ] )->getMock();
1280 $backend->expects( $this->once() )->method( 'getQoS' )
1281 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1282 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1285 $wanCache::QOS_UNKNOWN
,
1286 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1291 * @covers WANObjectCache::makeKey
1293 public function testMakeKey() {
1294 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1295 ->setMethods( [ 'makeKey' ] )->getMock();
1296 $backend->expects( $this->once() )->method( 'makeKey' )
1297 ->willReturn( 'special' );
1299 $wanCache = new WANObjectCache( [
1300 'cache' => $backend,
1301 'pool' => 'testcache-hash',
1302 'relayer' => new EventRelayerNull( [] )
1305 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1309 * @covers WANObjectCache::makeGlobalKey
1311 public function testMakeGlobalKey() {
1312 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1313 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1314 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1315 ->willReturn( 'special' );
1317 $wanCache = new WANObjectCache( [
1318 'cache' => $backend,
1319 'pool' => 'testcache-hash',
1320 'relayer' => new EventRelayerNull( [] )
1323 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );