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
17 class WANObjectCacheTest
extends PHPUnit_Framework_TestCase
{
18 /** @var WANObjectCache */
21 private $internalCache;
23 protected function setUp() {
26 $this->cache
= new WANObjectCache( [
27 'cache' => new HashBagOStuff(),
28 'pool' => 'testcache-hash',
29 'relayer' => new EventRelayerNull( [] )
32 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
33 /** @noinspection PhpUndefinedFieldInspection */
34 $this->internalCache
= $wanCache->cache
;
38 * @dataProvider provideSetAndGet
39 * @covers WANObjectCache::set()
40 * @covers WANObjectCache::get()
41 * @covers WANObjectCache::makeKey()
45 public function testSetAndGet( $value, $ttl ) {
48 $key = $this->cache
->makeKey( 'x', wfRandomString() );
50 $this->cache
->get( $key, $curTTL, [], $asOf );
51 $this->assertNull( $curTTL, "Current TTL is null" );
52 $this->assertNull( $asOf, "Current as-of-time is infinite" );
54 $t = microtime( true );
55 $this->cache
->set( $key, $value, $ttl );
57 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
58 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
59 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
61 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
62 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
64 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
65 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
68 public static function provideSetAndGet() {
75 [ (object)[ 'meow' ], 3 ],
83 * @covers WANObjectCache::get()
84 * @covers WANObjectCache::makeGlobalKey()
86 public function testGetNotExists() {
87 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
89 $value = $this->cache
->get( $key, $curTTL );
91 $this->assertFalse( $value, "Non-existing key has false value" );
92 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
96 * @covers WANObjectCache::set()
98 public function testSetOver() {
99 $key = wfRandomString();
100 for ( $i = 0; $i < 3; ++
$i ) {
101 $value = wfRandomString();
102 $this->cache
->set( $key, $value, 3 );
104 $this->assertEquals( $this->cache
->get( $key ), $value );
109 * @covers WANObjectCache::set()
111 public function testStaleSet() {
112 $key = wfRandomString();
113 $value = wfRandomString();
114 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
116 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
119 public function testProcessCache() {
121 $callback = function () use ( &$hit ) {
125 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
126 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
128 foreach ( $keys as $i => $key ) {
129 $this->cache
->getWithSetCallback(
130 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
132 $this->assertEquals( 3, $hit );
134 foreach ( $keys as $i => $key ) {
135 $this->cache
->getWithSetCallback(
136 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
138 $this->assertEquals( 3, $hit, "Values cached" );
140 foreach ( $keys as $i => $key ) {
141 $this->cache
->getWithSetCallback(
142 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
144 $this->assertEquals( 6, $hit );
146 foreach ( $keys as $i => $key ) {
147 $this->cache
->getWithSetCallback(
148 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
150 $this->assertEquals( 6, $hit, "New values cached" );
152 foreach ( $keys as $i => $key ) {
153 $this->cache
->delete( $key );
154 $this->cache
->getWithSetCallback(
155 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
157 $this->assertEquals( 9, $hit, "Values evicted" );
159 $key = reset( $keys );
161 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
162 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
163 $this->assertEquals( 10, $hit, "Value cached" );
164 $outerCallback = function () use ( &$callback, $key ) {
165 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
169 $this->cache
->getWithSetCallback( $key, 100, $outerCallback );
170 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
174 * @dataProvider getWithSetCallback_provider
175 * @covers WANObjectCache::getWithSetCallback()
176 * @covers WANObjectCache::doGetWithSetCallback()
177 * @param array $extOpts
178 * @param bool $versioned
180 public function testGetWithSetCallback( array $extOpts, $versioned ) {
181 $cache = $this->cache
;
183 $key = wfRandomString();
184 $value = wfRandomString();
185 $cKey1 = wfRandomString();
186 $cKey2 = wfRandomString();
191 $func = function ( $old, &$ttl, &$opts, $asOf )
192 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
197 $ttl = 20; // override with another value
202 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
203 $this->assertEquals( $value, $v, "Value returned" );
204 $this->assertEquals( 1, $wasSet, "Value regenerated" );
205 $this->assertFalse( $priorValue, "No prior value" );
206 $this->assertNull( $priorAsOf, "No prior value" );
209 $cache->get( $key, $curTTL );
210 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
211 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
214 $v = $cache->getWithSetCallback( $key, 30, $func, [
218 $this->assertEquals( $value, $v, "Value returned" );
219 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
221 $priorTime = microtime( true );
224 $v = $cache->getWithSetCallback(
225 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
227 $this->assertEquals( $value, $v, "Value returned" );
228 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
229 $this->assertEquals( $value, $priorValue, "Has prior value" );
230 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
231 $t1 = $cache->getCheckKeyTime( $cKey1 );
232 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
233 $t2 = $cache->getCheckKeyTime( $cKey2 );
234 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
236 $priorTime = microtime( true );
238 $v = $cache->getWithSetCallback(
239 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
241 $this->assertEquals( $value, $v, "Value returned" );
242 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
243 $t1 = $cache->getCheckKeyTime( $cKey1 );
244 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
245 $t2 = $cache->getCheckKeyTime( $cKey2 );
246 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
249 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
251 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
253 $this->assertEquals( $value, $v, "Value returned" );
255 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
258 $key = wfRandomString();
259 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
260 $this->assertEquals( $value, $v, "Value returned" );
261 $cache->delete( $key );
262 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
263 $this->assertEquals( $value, $v, "Value still returned after deleted" );
264 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
267 public static function getWithSetCallback_provider() {
270 [ [ 'version' => 1 ], true ]
275 * @covers WANObjectCache::getWithSetCallback()
276 * @covers WANObjectCache::doGetWithSetCallback()
278 public function testGetWithSetCallback_invalidCallback() {
279 $this->setExpectedException( InvalidArgumentException
::class );
280 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
284 * @dataProvider getMultiWithSetCallback_provider
285 * @covers WANObjectCache::getMultiWithSetCallback
286 * @covers WANObjectCache::makeMultiKeys
287 * @covers WANObjectCache::getMulti
288 * @param array $extOpts
289 * @param bool $versioned
291 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
292 $cache = $this->cache
;
294 $keyA = wfRandomString();
295 $keyB = wfRandomString();
296 $keyC = wfRandomString();
297 $cKey1 = wfRandomString();
298 $cKey2 = wfRandomString();
303 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
304 &$wasSet, &$priorValue, &$priorAsOf
309 $ttl = 20; // override with another value
314 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
316 $v = $cache->getMultiWithSetCallback(
317 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
318 $this->assertEquals( $value, $v[$keyA], "Value returned" );
319 $this->assertEquals( 1, $wasSet, "Value regenerated" );
320 $this->assertFalse( $priorValue, "No prior value" );
321 $this->assertNull( $priorAsOf, "No prior value" );
324 $cache->get( $keyA, $curTTL );
325 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
326 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
330 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
331 $v = $cache->getMultiWithSetCallback(
332 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
333 $this->assertEquals( $value, $v[$keyB], "Value returned" );
334 $this->assertEquals( 1, $wasSet, "Value regenerated" );
335 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
336 $v = $cache->getMultiWithSetCallback(
337 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
338 $this->assertEquals( $value, $v[$keyB], "Value returned" );
339 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
340 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
342 $priorTime = microtime( true );
345 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
346 $v = $cache->getMultiWithSetCallback(
347 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
349 $this->assertEquals( $value, $v[$keyB], "Value returned" );
350 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
351 $this->assertEquals( $value, $priorValue, "Has prior value" );
352 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
353 $t1 = $cache->getCheckKeyTime( $cKey1 );
354 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
355 $t2 = $cache->getCheckKeyTime( $cKey2 );
356 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
358 $priorTime = microtime( true );
361 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
362 $v = $cache->getMultiWithSetCallback(
363 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
365 $this->assertEquals( $value, $v[$keyC], "Value returned" );
366 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
367 $t1 = $cache->getCheckKeyTime( $cKey1 );
368 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
369 $t2 = $cache->getCheckKeyTime( $cKey2 );
370 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
373 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
375 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
377 $this->assertEquals( $value, $v, "Value returned" );
379 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
382 $key = wfRandomString();
383 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
384 $v = $cache->getMultiWithSetCallback(
385 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
386 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
387 $cache->delete( $key );
388 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
389 $v = $cache->getMultiWithSetCallback(
390 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
391 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
392 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
395 $ids = [ 1, 2, 3, 4, 5, 6 ];
396 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
397 return $wanCache->makeKey( 'test', $id );
399 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
400 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
405 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
408 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
409 array_values( $values ),
410 "Correct values in correct order"
413 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
414 array_keys( $values ),
415 "Correct keys in correct order"
417 $this->assertEquals( count( $ids ), $calls );
419 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
420 $this->assertEquals( count( $ids ), $calls, "Values cached" );
422 // Mock the BagOStuff to assure only one getMulti() call given process caching
423 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
424 ->setMethods( [ 'getMulti' ] )->getMock();
425 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
426 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
427 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
429 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
431 // Warm the process cache
432 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
434 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
435 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
437 // Use the process cache
439 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
440 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
444 public static function getMultiWithSetCallback_provider() {
447 [ [ 'version' => 1 ], true ]
452 * @dataProvider getMultiWithUnionSetCallback_provider
453 * @covers WANObjectCache::getMultiWithUnionSetCallback()
454 * @covers WANObjectCache::makeMultiKeys()
455 * @param array $extOpts
456 * @param bool $versioned
458 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
459 $cache = $this->cache
;
461 $keyA = wfRandomString();
462 $keyB = wfRandomString();
463 $keyC = wfRandomString();
464 $cKey1 = wfRandomString();
465 $cKey2 = wfRandomString();
468 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
469 &$wasSet, &$priorValue, &$priorAsOf
472 foreach ( $ids as $id ) {
474 $newValues[$id] = "@$id$";
475 $ttls[$id] = 20; // override with another value
482 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
484 $v = $cache->getMultiWithUnionSetCallback(
485 $keyedIds, 30, $genFunc, $extOpts );
486 $this->assertEquals( $value, $v[$keyA], "Value returned" );
487 $this->assertEquals( 1, $wasSet, "Value regenerated" );
490 $cache->get( $keyA, $curTTL );
491 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
492 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
496 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
497 $v = $cache->getMultiWithUnionSetCallback(
498 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
499 $this->assertEquals( $value, $v[$keyB], "Value returned" );
500 $this->assertEquals( 1, $wasSet, "Value regenerated" );
501 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
502 $v = $cache->getMultiWithUnionSetCallback(
503 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
504 $this->assertEquals( $value, $v[$keyB], "Value returned" );
505 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
506 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
508 $priorTime = microtime( true );
511 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
512 $v = $cache->getMultiWithUnionSetCallback(
513 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
515 $this->assertEquals( $value, $v[$keyB], "Value returned" );
516 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
517 $t1 = $cache->getCheckKeyTime( $cKey1 );
518 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
519 $t2 = $cache->getCheckKeyTime( $cKey2 );
520 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
522 $priorTime = microtime( true );
525 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
526 $v = $cache->getMultiWithUnionSetCallback(
527 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
529 $this->assertEquals( $value, $v[$keyC], "Value returned" );
530 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
531 $t1 = $cache->getCheckKeyTime( $cKey1 );
532 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
533 $t2 = $cache->getCheckKeyTime( $cKey2 );
534 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
537 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
539 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
541 $this->assertEquals( $value, $v, "Value returned" );
543 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
546 $key = wfRandomString();
547 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
548 $v = $cache->getMultiWithUnionSetCallback(
549 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
550 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
551 $cache->delete( $key );
552 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
553 $v = $cache->getMultiWithUnionSetCallback(
554 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
555 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
556 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
559 $ids = [ 1, 2, 3, 4, 5, 6 ];
560 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
561 return $wanCache->makeKey( 'test', $id );
563 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
564 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
566 foreach ( $ids as $id ) {
568 $newValues[$id] = "val-{$id}";
573 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
576 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
577 array_values( $values ),
578 "Correct values in correct order"
581 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
582 array_keys( $values ),
583 "Correct keys in correct order"
585 $this->assertEquals( count( $ids ), $calls );
587 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
588 $this->assertEquals( count( $ids ), $calls, "Values cached" );
591 public static function getMultiWithUnionSetCallback_provider() {
594 [ [ 'version' => 1 ], true ]
599 * @covers WANObjectCache::getWithSetCallback()
600 * @covers WANObjectCache::doGetWithSetCallback()
602 public function testLockTSE() {
603 $cache = $this->cache
;
604 $key = wfRandomString();
605 $value = wfRandomString();
608 $func = function () use ( &$calls, $value, $cache, $key ) {
610 // Immediately kill any mutex rather than waiting a second
611 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
615 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
616 $this->assertEquals( $value, $ret );
617 $this->assertEquals( 1, $calls, 'Value was populated' );
619 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
620 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
622 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
623 $ret = $cache->getWithSetCallback( $key, 30, $func,
624 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
625 $this->assertEquals( $value, $ret, 'Old value used' );
626 $this->assertEquals( 1, $calls, 'Callback was not used' );
628 $cache->delete( $key );
629 $ret = $cache->getWithSetCallback( $key, 30, $func,
630 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
631 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
632 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
634 $ret = $cache->getWithSetCallback( $key, 30, $func,
635 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
636 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
637 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
641 * @covers WANObjectCache::getWithSetCallback()
642 * @covers WANObjectCache::doGetWithSetCallback()
643 * @covers WANObjectCache::set()
645 public function testLockTSESlow() {
646 $cache = $this->cache
;
647 $key = wfRandomString();
648 $value = wfRandomString();
651 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
653 $setOpts['since'] = microtime( true ) - 10;
654 // Immediately kill any mutex rather than waiting a second
655 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
659 // Value should be marked as stale due to snapshot lag
661 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
662 $this->assertEquals( $value, $ret );
663 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
664 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
665 $this->assertEquals( 1, $calls, 'Value was generated' );
667 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
668 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
669 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
670 $this->assertEquals( $value, $ret );
671 $this->assertEquals( 1, $calls, 'Callback was not used' );
675 * @covers WANObjectCache::getWithSetCallback()
676 * @covers WANObjectCache::doGetWithSetCallback()
678 public function testBusyValue() {
679 $cache = $this->cache
;
680 $key = wfRandomString();
681 $value = wfRandomString();
682 $busyValue = wfRandomString();
685 $func = function () use ( &$calls, $value, $cache, $key ) {
687 // Immediately kill any mutex rather than waiting a second
688 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
692 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
693 $this->assertEquals( $value, $ret );
694 $this->assertEquals( 1, $calls, 'Value was populated' );
696 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
697 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
699 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
700 $ret = $cache->getWithSetCallback( $key, 30, $func,
701 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
702 $this->assertEquals( $value, $ret, 'Callback used' );
703 $this->assertEquals( 2, $calls, 'Callback used' );
705 $ret = $cache->getWithSetCallback( $key, 30, $func,
706 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
707 $this->assertEquals( $value, $ret, 'Old value used' );
708 $this->assertEquals( 2, $calls, 'Callback was not used' );
710 $cache->delete( $key ); // no value at all anymore and still locked
711 $ret = $cache->getWithSetCallback( $key, 30, $func,
712 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
713 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
714 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
716 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
717 $ret = $cache->getWithSetCallback( $key, 30, $func,
718 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
719 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
720 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
722 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
723 $ret = $cache->getWithSetCallback( $key, 30, $func,
724 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
725 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
726 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
730 * @covers WANObjectCache::getMulti()
732 public function testGetMulti() {
733 $cache = $this->cache
;
735 $value1 = [ 'this' => 'is', 'a' => 'test' ];
736 $value2 = [ 'this' => 'is', 'another' => 'test' ];
738 $key1 = wfRandomString();
739 $key2 = wfRandomString();
740 $key3 = wfRandomString();
742 $cache->set( $key1, $value1, 5 );
743 $cache->set( $key2, $value2, 10 );
747 [ $key1 => $value1, $key2 => $value2 ],
748 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
749 'Result array populated'
752 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
753 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
754 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
756 $cKey1 = wfRandomString();
757 $cKey2 = wfRandomString();
759 $priorTime = microtime( true );
763 [ $key1 => $value1, $key2 => $value2 ],
764 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
765 "Result array populated even with new check keys"
767 $t1 = $cache->getCheckKeyTime( $cKey1 );
768 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
769 $t2 = $cache->getCheckKeyTime( $cKey2 );
770 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
771 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
772 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
773 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
778 [ $key1 => $value1, $key2 => $value2 ],
779 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
780 "Result array still populated even with new check keys"
782 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
783 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
784 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
788 * @covers WANObjectCache::getMulti()
789 * @covers WANObjectCache::processCheckKeys()
791 public function testGetMultiCheckKeys() {
792 $cache = $this->cache
;
794 $checkAll = wfRandomString();
795 $check1 = wfRandomString();
796 $check2 = wfRandomString();
797 $check3 = wfRandomString();
798 $value1 = wfRandomString();
799 $value2 = wfRandomString();
801 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
802 // several seconds during the test to assert the behaviour.
803 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
804 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
808 $cache->set( 'key1', $value1, 10 );
809 $cache->set( 'key2', $value2, 10 );
812 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
819 [ 'key1' => $value1, 'key2' => $value2 ],
823 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
824 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
825 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
826 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
828 $cache->touchCheckKey( $check1 );
831 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
838 [ 'key1' => $value1, 'key2' => $value2 ],
840 'key1 expired by check1, but value still provided'
842 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
843 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
845 $cache->touchCheckKey( $checkAll );
848 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
855 [ 'key1' => $value1, 'key2' => $value2 ],
857 'All keys expired by checkAll, but value still provided'
859 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
860 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
864 * @covers WANObjectCache::get()
865 * @covers WANObjectCache::processCheckKeys()
867 public function testCheckKeyInitHoldoff() {
868 $cache = $this->cache
;
870 for ( $i = 0; $i < 500; ++
$i ) {
871 $key = wfRandomString();
872 $checkKey = wfRandomString();
874 $cache->get( $key, $curTTL, [ $checkKey ] );
875 $cache->set( $key, 'val', 10 );
877 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
879 $this->assertEquals( 'val', $v );
880 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
883 for ( $i = 0; $i < 500; ++
$i ) {
884 $key = wfRandomString();
885 $checkKey = wfRandomString();
887 $cache->set( $key, 'val', 10 );
889 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
891 $this->assertEquals( 'val', $v );
892 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
897 * @covers WANObjectCache::delete
898 * @covers WANObjectCache::relayDelete
899 * @covers WANObjectCache::relayPurge
901 public function testDelete() {
902 $key = wfRandomString();
903 $value = wfRandomString();
904 $this->cache
->set( $key, $value );
907 $v = $this->cache
->get( $key, $curTTL );
908 $this->assertEquals( $value, $v, "Key was created with value" );
909 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
911 $this->cache
->delete( $key );
914 $v = $this->cache
->get( $key, $curTTL );
915 $this->assertFalse( $v, "Deleted key has false value" );
916 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
918 $this->cache
->set( $key, $value . 'more' );
919 $v = $this->cache
->get( $key, $curTTL );
920 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
921 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
923 $this->cache
->set( $key, $value );
924 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
927 $v = $this->cache
->get( $key, $curTTL );
928 $this->assertFalse( $v, "Deleted key has false value" );
929 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
931 $this->cache
->set( $key, $value );
932 $v = $this->cache
->get( $key, $curTTL );
933 $this->assertEquals( $value, $v, "Key was created with value" );
934 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
938 * @dataProvider getWithSetCallback_versions_provider
939 * @covers WANObjectCache::getWithSetCallback()
940 * @covers WANObjectCache::doGetWithSetCallback()
941 * @param array $extOpts
942 * @param bool $versioned
944 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
945 $cache = $this->cache
;
947 $key = wfRandomString();
948 $value = wfRandomString();
951 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
956 // Set the main key (version N if versioned)
958 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
959 $this->assertEquals( $value, $v, "Value returned" );
960 $this->assertEquals( 1, $wasSet, "Value regenerated" );
961 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
962 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
963 // Set the key for version N+1 (if versioned)
965 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
968 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
969 $this->assertEquals( $value, $v, "Value returned" );
970 $this->assertEquals( 1, $wasSet, "Value regenerated" );
973 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
974 $this->assertEquals( $value, $v, "Value returned" );
975 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
979 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
980 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
983 $cache->delete( $key );
984 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
985 $this->assertEquals( $value, $v, "Value returned" );
986 $this->assertEquals( 1, $wasSet, "Value regenerated" );
990 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
991 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
992 $this->assertEquals( $value, $v, "Value returned" );
993 $this->assertEquals( 1, $wasSet, "Value regenerated" );
997 public static function getWithSetCallback_versions_provider() {
1000 [ [ 'version' => 1 ], true ]
1005 * @covers WANObjectCache::touchCheckKey
1006 * @covers WANObjectCache::resetCheckKey
1007 * @covers WANObjectCache::getCheckKeyTime
1008 * @covers WANObjectCache::makePurgeValue
1009 * @covers WANObjectCache::parsePurgeValue
1011 public function testTouchKeys() {
1012 $key = wfRandomString();
1014 $priorTime = microtime( true );
1016 $t0 = $this->cache
->getCheckKeyTime( $key );
1017 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1019 $priorTime = microtime( true );
1021 $this->cache
->touchCheckKey( $key );
1022 $t1 = $this->cache
->getCheckKeyTime( $key );
1023 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1025 $t2 = $this->cache
->getCheckKeyTime( $key );
1026 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1029 $this->cache
->touchCheckKey( $key );
1030 $t3 = $this->cache
->getCheckKeyTime( $key );
1031 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1033 $t4 = $this->cache
->getCheckKeyTime( $key );
1034 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1037 $this->cache
->resetCheckKey( $key );
1038 $t5 = $this->cache
->getCheckKeyTime( $key );
1039 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1041 $t6 = $this->cache
->getCheckKeyTime( $key );
1042 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1046 * @covers WANObjectCache::getMulti()
1048 public function testGetWithSeveralCheckKeys() {
1049 $key = wfRandomString();
1050 $tKey1 = wfRandomString();
1051 $tKey2 = wfRandomString();
1054 // Two check keys are newer (given hold-off) than $key, another is older
1055 $this->internalCache
->set(
1056 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1057 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1059 $this->internalCache
->set(
1060 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1061 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1063 $this->internalCache
->set(
1064 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1065 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1067 $this->cache
->set( $key, $value, 30 );
1070 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1071 $this->assertEquals( $value, $v, "Value matches" );
1072 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1073 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1077 * @covers WANObjectCache::reap()
1078 * @covers WANObjectCache::reapCheckKey()
1080 public function testReap() {
1081 $vKey1 = wfRandomString();
1082 $vKey2 = wfRandomString();
1083 $tKey1 = wfRandomString();
1084 $tKey2 = wfRandomString();
1087 $knownPurge = time() - 60;
1088 $goodTime = microtime( true ) - 5;
1089 $badTime = microtime( true ) - 300;
1091 $this->internalCache
->set(
1092 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1094 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1095 WANObjectCache
::FLD_VALUE
=> $value,
1096 WANObjectCache
::FLD_TTL
=> 3600,
1097 WANObjectCache
::FLD_TIME
=> $goodTime
1100 $this->internalCache
->set(
1101 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1103 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1104 WANObjectCache
::FLD_VALUE
=> $value,
1105 WANObjectCache
::FLD_TTL
=> 3600,
1106 WANObjectCache
::FLD_TIME
=> $badTime
1109 $this->internalCache
->set(
1110 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1111 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1113 $this->internalCache
->set(
1114 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1115 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1118 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1119 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1120 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1121 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1123 $this->assertFalse( $bad1 );
1124 $this->assertTrue( $bad2 );
1126 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1127 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1128 $this->assertFalse( $tBad1 );
1129 $this->assertTrue( $tBad2 );
1133 * @covers WANObjectCache::reap()
1135 public function testReap_fail() {
1136 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1137 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1138 $backend->expects( $this->once() )->method( 'get' )
1140 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1141 WANObjectCache
::FLD_VALUE
=> 'value',
1142 WANObjectCache
::FLD_TTL
=> 3600,
1143 WANObjectCache
::FLD_TIME
=> 300,
1145 $backend->expects( $this->once() )->method( 'changeTTL' )
1146 ->willReturn( false );
1148 $wanCache = new WANObjectCache( [
1149 'cache' => $backend,
1150 'pool' => 'testcache-hash',
1151 'relayer' => new EventRelayerNull( [] )
1155 $ret = $wanCache->reap( 'key', 360, $isStale );
1156 $this->assertTrue( $isStale, 'value was stale' );
1157 $this->assertFalse( $ret, 'changeTTL failed' );
1161 * @covers WANObjectCache::set()
1163 public function testSetWithLag() {
1166 $key = wfRandomString();
1167 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1168 $this->cache
->set( $key, $value, 30, $opts );
1169 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1171 $key = wfRandomString();
1172 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1173 $this->cache
->set( $key, $value, 30, $opts );
1174 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1176 $key = wfRandomString();
1177 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1178 $this->cache
->set( $key, $value, 30, $opts );
1179 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1183 * @covers WANObjectCache::set()
1185 public function testWritePending() {
1188 $key = wfRandomString();
1189 $opts = [ 'pending' => true ];
1190 $this->cache
->set( $key, $value, 30, $opts );
1191 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1194 public function testMcRouterSupport() {
1195 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1196 ->setMethods( [ 'set', 'delete' ] )->getMock();
1197 $localBag->expects( $this->never() )->method( 'set' );
1198 $localBag->expects( $this->never() )->method( 'delete' );
1199 $wanCache = new WANObjectCache( [
1200 'cache' => $localBag,
1201 'pool' => 'testcache-hash',
1202 'relayer' => new EventRelayerNull( [] )
1204 $valFunc = function () {
1208 // None of these should use broadcasting commands (e.g. SET, DELETE)
1209 $wanCache->get( 'x' );
1210 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1211 $wanCache->getMulti( [ 'x', 'y' ] );
1212 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1213 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1214 $wanCache->getCheckKeyTime( 'zzz' );
1215 $wanCache->reap( 'x', time() - 300 );
1216 $wanCache->reap( 'zzz', time() - 300 );
1220 * @dataProvider provideAdaptiveTTL
1221 * @covers WANObjectCache::adaptiveTTL()
1222 * @param float|int $ago
1223 * @param int $maxTTL
1224 * @param int $minTTL
1225 * @param float $factor
1226 * @param int $adaptiveTTL
1228 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1229 $mtime = $ago ?
time() - $ago : $ago;
1231 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1233 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1234 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1236 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1238 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1239 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1242 public static function provideAdaptiveTTL() {
1244 [ 3600, 900, 30, 0.2, 720 ],
1245 [ 3600, 500, 30, 0.2, 500 ],
1246 [ 3600, 86400, 800, 0.2, 800 ],
1247 [ false, 86400, 800, 0.2, 800 ],
1248 [ null, 86400, 800, 0.2, 800 ]
1253 * @covers WANObjectCache::__construct
1254 * @covers WANObjectCache::newEmpty
1256 public function testNewEmpty() {
1257 $this->assertInstanceOf(
1258 WANObjectCache
::class,
1259 WANObjectCache
::newEmpty()
1264 * @covers WANObjectCache::setLogger
1266 public function testSetLogger() {
1267 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1271 * @covers WANObjectCache::getQoS
1273 public function testGetQoS() {
1274 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1275 ->setMethods( [ 'getQoS' ] )->getMock();
1276 $backend->expects( $this->once() )->method( 'getQoS' )
1277 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1278 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1281 $wanCache::QOS_UNKNOWN
,
1282 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1287 * @covers WANObjectCache::makeKey
1289 public function testMakeKey() {
1290 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1291 ->setMethods( [ 'makeKey' ] )->getMock();
1292 $backend->expects( $this->once() )->method( 'makeKey' )
1293 ->willReturn( 'special' );
1295 $wanCache = new WANObjectCache( [
1296 'cache' => $backend,
1297 'pool' => 'testcache-hash',
1298 'relayer' => new EventRelayerNull( [] )
1301 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1305 * @covers WANObjectCache::makeGlobalKey
1307 public function testMakeGlobalKey() {
1308 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1309 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1310 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1311 ->willReturn( 'special' );
1313 $wanCache = new WANObjectCache( [
1314 'cache' => $backend,
1315 'pool' => 'testcache-hash',
1316 'relayer' => new EventRelayerNull( [] )
1319 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );