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 );
163 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
164 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
165 $this->assertEquals( 10, $hit, "Value cached" );
166 $outerCallback = function () use ( &$callback, $key ) {
167 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
171 $this->cache
->getWithSetCallback( $key, 100, $outerCallback );
172 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
176 * @dataProvider getWithSetCallback_provider
177 * @covers WANObjectCache::getWithSetCallback()
178 * @covers WANObjectCache::doGetWithSetCallback()
179 * @param array $extOpts
180 * @param bool $versioned
182 public function testGetWithSetCallback( array $extOpts, $versioned ) {
183 $cache = $this->cache
;
185 $key = wfRandomString();
186 $value = wfRandomString();
187 $cKey1 = wfRandomString();
188 $cKey2 = wfRandomString();
193 $func = function ( $old, &$ttl, &$opts, $asOf )
194 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
199 $ttl = 20; // override with another value
204 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
205 $this->assertEquals( $value, $v, "Value returned" );
206 $this->assertEquals( 1, $wasSet, "Value regenerated" );
207 $this->assertFalse( $priorValue, "No prior value" );
208 $this->assertNull( $priorAsOf, "No prior value" );
211 $cache->get( $key, $curTTL );
212 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
213 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
216 $v = $cache->getWithSetCallback( $key, 30, $func, [
220 $this->assertEquals( $value, $v, "Value returned" );
221 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
223 $priorTime = microtime( true );
226 $v = $cache->getWithSetCallback(
227 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
229 $this->assertEquals( $value, $v, "Value returned" );
230 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
231 $this->assertEquals( $value, $priorValue, "Has prior value" );
232 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
233 $t1 = $cache->getCheckKeyTime( $cKey1 );
234 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
235 $t2 = $cache->getCheckKeyTime( $cKey2 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
238 $priorTime = microtime( true );
240 $v = $cache->getWithSetCallback(
241 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
243 $this->assertEquals( $value, $v, "Value returned" );
244 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
245 $t1 = $cache->getCheckKeyTime( $cKey1 );
246 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
247 $t2 = $cache->getCheckKeyTime( $cKey2 );
248 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
251 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
253 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
255 $this->assertEquals( $value, $v, "Value returned" );
257 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
260 $key = wfRandomString();
261 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
262 $this->assertEquals( $value, $v, "Value returned" );
263 $cache->delete( $key );
264 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
265 $this->assertEquals( $value, $v, "Value still returned after deleted" );
266 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
269 public static function getWithSetCallback_provider() {
272 [ [ 'version' => 1 ], true ]
277 * @covers WANObjectCache::getWithSetCallback()
278 * @covers WANObjectCache::doGetWithSetCallback()
280 public function testGetWithSetCallback_invalidCallback() {
281 $this->setExpectedException( InvalidArgumentException
::class );
282 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
286 * @dataProvider getMultiWithSetCallback_provider
287 * @covers WANObjectCache::getMultiWithSetCallback
288 * @covers WANObjectCache::makeMultiKeys
289 * @covers WANObjectCache::getMulti
290 * @param array $extOpts
291 * @param bool $versioned
293 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
294 $cache = $this->cache
;
296 $keyA = wfRandomString();
297 $keyB = wfRandomString();
298 $keyC = wfRandomString();
299 $cKey1 = wfRandomString();
300 $cKey2 = wfRandomString();
305 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
306 &$wasSet, &$priorValue, &$priorAsOf
311 $ttl = 20; // override with another value
316 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
318 $v = $cache->getMultiWithSetCallback(
319 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
320 $this->assertEquals( $value, $v[$keyA], "Value returned" );
321 $this->assertEquals( 1, $wasSet, "Value regenerated" );
322 $this->assertFalse( $priorValue, "No prior value" );
323 $this->assertNull( $priorAsOf, "No prior value" );
326 $cache->get( $keyA, $curTTL );
327 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
328 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
332 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
333 $v = $cache->getMultiWithSetCallback(
334 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
335 $this->assertEquals( $value, $v[$keyB], "Value returned" );
336 $this->assertEquals( 1, $wasSet, "Value regenerated" );
337 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
338 $v = $cache->getMultiWithSetCallback(
339 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
340 $this->assertEquals( $value, $v[$keyB], "Value returned" );
341 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
342 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
344 $priorTime = microtime( true );
347 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
348 $v = $cache->getMultiWithSetCallback(
349 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
351 $this->assertEquals( $value, $v[$keyB], "Value returned" );
352 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
353 $this->assertEquals( $value, $priorValue, "Has prior value" );
354 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
355 $t1 = $cache->getCheckKeyTime( $cKey1 );
356 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
357 $t2 = $cache->getCheckKeyTime( $cKey2 );
358 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
360 $priorTime = microtime( true );
363 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
364 $v = $cache->getMultiWithSetCallback(
365 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
367 $this->assertEquals( $value, $v[$keyC], "Value returned" );
368 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
369 $t1 = $cache->getCheckKeyTime( $cKey1 );
370 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
371 $t2 = $cache->getCheckKeyTime( $cKey2 );
372 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
375 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
377 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
379 $this->assertEquals( $value, $v, "Value returned" );
381 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
384 $key = wfRandomString();
385 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
386 $v = $cache->getMultiWithSetCallback(
387 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
388 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
389 $cache->delete( $key );
390 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
391 $v = $cache->getMultiWithSetCallback(
392 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
393 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
394 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
397 $ids = [ 1, 2, 3, 4, 5, 6 ];
398 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
399 return $wanCache->makeKey( 'test', $id );
401 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
402 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
407 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
410 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
411 array_values( $values ),
412 "Correct values in correct order"
415 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
416 array_keys( $values ),
417 "Correct keys in correct order"
419 $this->assertEquals( count( $ids ), $calls );
421 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
422 $this->assertEquals( count( $ids ), $calls, "Values cached" );
424 // Mock the BagOStuff to assure only one getMulti() call given process caching
425 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
426 ->setMethods( [ 'getMulti' ] )->getMock();
427 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
428 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
429 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
431 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
433 // Warm the process cache
434 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
436 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
437 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
439 // Use the process cache
441 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
442 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
446 public static function getMultiWithSetCallback_provider() {
449 [ [ 'version' => 1 ], true ]
454 * @dataProvider getMultiWithUnionSetCallback_provider
455 * @covers WANObjectCache::getMultiWithUnionSetCallback()
456 * @covers WANObjectCache::makeMultiKeys()
457 * @param array $extOpts
458 * @param bool $versioned
460 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
461 $cache = $this->cache
;
463 $keyA = wfRandomString();
464 $keyB = wfRandomString();
465 $keyC = wfRandomString();
466 $cKey1 = wfRandomString();
467 $cKey2 = wfRandomString();
470 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
471 &$wasSet, &$priorValue, &$priorAsOf
474 foreach ( $ids as $id ) {
476 $newValues[$id] = "@$id$";
477 $ttls[$id] = 20; // override with another value
484 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
486 $v = $cache->getMultiWithUnionSetCallback(
487 $keyedIds, 30, $genFunc, $extOpts );
488 $this->assertEquals( $value, $v[$keyA], "Value returned" );
489 $this->assertEquals( 1, $wasSet, "Value regenerated" );
492 $cache->get( $keyA, $curTTL );
493 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
494 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
498 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
499 $v = $cache->getMultiWithUnionSetCallback(
500 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
501 $this->assertEquals( $value, $v[$keyB], "Value returned" );
502 $this->assertEquals( 1, $wasSet, "Value regenerated" );
503 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
504 $v = $cache->getMultiWithUnionSetCallback(
505 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
506 $this->assertEquals( $value, $v[$keyB], "Value returned" );
507 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
508 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
510 $priorTime = microtime( true );
513 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
514 $v = $cache->getMultiWithUnionSetCallback(
515 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
517 $this->assertEquals( $value, $v[$keyB], "Value returned" );
518 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
519 $t1 = $cache->getCheckKeyTime( $cKey1 );
520 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
521 $t2 = $cache->getCheckKeyTime( $cKey2 );
522 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
524 $priorTime = microtime( true );
527 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
528 $v = $cache->getMultiWithUnionSetCallback(
529 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
531 $this->assertEquals( $value, $v[$keyC], "Value returned" );
532 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
533 $t1 = $cache->getCheckKeyTime( $cKey1 );
534 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
535 $t2 = $cache->getCheckKeyTime( $cKey2 );
536 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
539 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
541 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
543 $this->assertEquals( $value, $v, "Value returned" );
545 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
548 $key = wfRandomString();
549 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
550 $v = $cache->getMultiWithUnionSetCallback(
551 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
552 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
553 $cache->delete( $key );
554 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
555 $v = $cache->getMultiWithUnionSetCallback(
556 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
557 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
558 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
561 $ids = [ 1, 2, 3, 4, 5, 6 ];
562 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
563 return $wanCache->makeKey( 'test', $id );
565 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
566 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
568 foreach ( $ids as $id ) {
570 $newValues[$id] = "val-{$id}";
575 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
578 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
579 array_values( $values ),
580 "Correct values in correct order"
583 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
584 array_keys( $values ),
585 "Correct keys in correct order"
587 $this->assertEquals( count( $ids ), $calls );
589 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
590 $this->assertEquals( count( $ids ), $calls, "Values cached" );
593 public static function getMultiWithUnionSetCallback_provider() {
596 [ [ 'version' => 1 ], true ]
601 * @covers WANObjectCache::getWithSetCallback()
602 * @covers WANObjectCache::doGetWithSetCallback()
604 public function testLockTSE() {
605 $cache = $this->cache
;
606 $key = wfRandomString();
607 $value = wfRandomString();
610 $func = function () use ( &$calls, $value, $cache, $key ) {
612 // Immediately kill any mutex rather than waiting a second
613 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
617 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
618 $this->assertEquals( $value, $ret );
619 $this->assertEquals( 1, $calls, 'Value was populated' );
621 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
622 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
624 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
625 $ret = $cache->getWithSetCallback( $key, 30, $func,
626 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
627 $this->assertEquals( $value, $ret, 'Old value used' );
628 $this->assertEquals( 1, $calls, 'Callback was not used' );
630 $cache->delete( $key );
631 $ret = $cache->getWithSetCallback( $key, 30, $func,
632 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
633 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
634 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
636 $ret = $cache->getWithSetCallback( $key, 30, $func,
637 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
638 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
639 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
643 * @covers WANObjectCache::getWithSetCallback()
644 * @covers WANObjectCache::doGetWithSetCallback()
645 * @covers WANObjectCache::set()
647 public function testLockTSESlow() {
648 $cache = $this->cache
;
649 $key = wfRandomString();
650 $value = wfRandomString();
653 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
655 $setOpts['since'] = microtime( true ) - 10;
656 // Immediately kill any mutex rather than waiting a second
657 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
661 // Value should be marked as stale due to snapshot lag
663 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
664 $this->assertEquals( $value, $ret );
665 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
666 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
667 $this->assertEquals( 1, $calls, 'Value was generated' );
669 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
670 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
671 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
672 $this->assertEquals( $value, $ret );
673 $this->assertEquals( 1, $calls, 'Callback was not used' );
677 * @covers WANObjectCache::getWithSetCallback()
678 * @covers WANObjectCache::doGetWithSetCallback()
680 public function testBusyValue() {
681 $cache = $this->cache
;
682 $key = wfRandomString();
683 $value = wfRandomString();
684 $busyValue = wfRandomString();
687 $func = function () use ( &$calls, $value, $cache, $key ) {
689 // Immediately kill any mutex rather than waiting a second
690 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
694 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
695 $this->assertEquals( $value, $ret );
696 $this->assertEquals( 1, $calls, 'Value was populated' );
698 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
699 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
701 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
702 $ret = $cache->getWithSetCallback( $key, 30, $func,
703 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
704 $this->assertEquals( $value, $ret, 'Callback used' );
705 $this->assertEquals( 2, $calls, 'Callback used' );
707 $ret = $cache->getWithSetCallback( $key, 30, $func,
708 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
709 $this->assertEquals( $value, $ret, 'Old value used' );
710 $this->assertEquals( 2, $calls, 'Callback was not used' );
712 $cache->delete( $key ); // no value at all anymore and still locked
713 $ret = $cache->getWithSetCallback( $key, 30, $func,
714 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
715 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
716 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
718 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
719 $ret = $cache->getWithSetCallback( $key, 30, $func,
720 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
721 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
722 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
724 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
725 $ret = $cache->getWithSetCallback( $key, 30, $func,
726 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
727 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
728 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
732 * @covers WANObjectCache::getMulti()
734 public function testGetMulti() {
735 $cache = $this->cache
;
737 $value1 = [ 'this' => 'is', 'a' => 'test' ];
738 $value2 = [ 'this' => 'is', 'another' => 'test' ];
740 $key1 = wfRandomString();
741 $key2 = wfRandomString();
742 $key3 = wfRandomString();
744 $cache->set( $key1, $value1, 5 );
745 $cache->set( $key2, $value2, 10 );
749 [ $key1 => $value1, $key2 => $value2 ],
750 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
751 'Result array populated'
754 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
755 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
756 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
758 $cKey1 = wfRandomString();
759 $cKey2 = wfRandomString();
761 $priorTime = microtime( true );
765 [ $key1 => $value1, $key2 => $value2 ],
766 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
767 "Result array populated even with new check keys"
769 $t1 = $cache->getCheckKeyTime( $cKey1 );
770 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
771 $t2 = $cache->getCheckKeyTime( $cKey2 );
772 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
773 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
774 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
775 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
780 [ $key1 => $value1, $key2 => $value2 ],
781 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
782 "Result array still populated even with new check keys"
784 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
785 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
786 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
790 * @covers WANObjectCache::getMulti()
791 * @covers WANObjectCache::processCheckKeys()
793 public function testGetMultiCheckKeys() {
794 $cache = $this->cache
;
796 $checkAll = wfRandomString();
797 $check1 = wfRandomString();
798 $check2 = wfRandomString();
799 $check3 = wfRandomString();
800 $value1 = wfRandomString();
801 $value2 = wfRandomString();
803 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
804 // several seconds during the test to assert the behaviour.
805 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
806 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
810 $cache->set( 'key1', $value1, 10 );
811 $cache->set( 'key2', $value2, 10 );
814 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
821 [ 'key1' => $value1, 'key2' => $value2 ],
825 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
826 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
827 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
828 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
830 $cache->touchCheckKey( $check1 );
833 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
840 [ 'key1' => $value1, 'key2' => $value2 ],
842 'key1 expired by check1, but value still provided'
844 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
845 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
847 $cache->touchCheckKey( $checkAll );
850 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
857 [ 'key1' => $value1, 'key2' => $value2 ],
859 'All keys expired by checkAll, but value still provided'
861 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
862 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
866 * @covers WANObjectCache::get()
867 * @covers WANObjectCache::processCheckKeys()
869 public function testCheckKeyInitHoldoff() {
870 $cache = $this->cache
;
872 for ( $i = 0; $i < 500; ++
$i ) {
873 $key = wfRandomString();
874 $checkKey = wfRandomString();
876 $cache->get( $key, $curTTL, [ $checkKey ] );
877 $cache->set( $key, 'val', 10 );
879 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
881 $this->assertEquals( 'val', $v );
882 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
885 for ( $i = 0; $i < 500; ++
$i ) {
886 $key = wfRandomString();
887 $checkKey = wfRandomString();
889 $cache->set( $key, 'val', 10 );
891 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
893 $this->assertEquals( 'val', $v );
894 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
899 * @covers WANObjectCache::delete
900 * @covers WANObjectCache::relayDelete
901 * @covers WANObjectCache::relayPurge
903 public function testDelete() {
904 $key = wfRandomString();
905 $value = wfRandomString();
906 $this->cache
->set( $key, $value );
909 $v = $this->cache
->get( $key, $curTTL );
910 $this->assertEquals( $value, $v, "Key was created with value" );
911 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
913 $this->cache
->delete( $key );
916 $v = $this->cache
->get( $key, $curTTL );
917 $this->assertFalse( $v, "Deleted key has false value" );
918 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
920 $this->cache
->set( $key, $value . 'more' );
921 $v = $this->cache
->get( $key, $curTTL );
922 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
923 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
925 $this->cache
->set( $key, $value );
926 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
929 $v = $this->cache
->get( $key, $curTTL );
930 $this->assertFalse( $v, "Deleted key has false value" );
931 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
933 $this->cache
->set( $key, $value );
934 $v = $this->cache
->get( $key, $curTTL );
935 $this->assertEquals( $value, $v, "Key was created with value" );
936 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
940 * @dataProvider getWithSetCallback_versions_provider
941 * @covers WANObjectCache::getWithSetCallback()
942 * @covers WANObjectCache::doGetWithSetCallback()
943 * @param array $extOpts
944 * @param bool $versioned
946 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
947 $cache = $this->cache
;
949 $key = wfRandomString();
950 $value = wfRandomString();
953 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
958 // Set the main key (version N if versioned)
960 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
961 $this->assertEquals( $value, $v, "Value returned" );
962 $this->assertEquals( 1, $wasSet, "Value regenerated" );
963 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
964 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
965 // Set the key for version N+1 (if versioned)
967 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
970 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
971 $this->assertEquals( $value, $v, "Value returned" );
972 $this->assertEquals( 1, $wasSet, "Value regenerated" );
975 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
976 $this->assertEquals( $value, $v, "Value returned" );
977 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
981 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
982 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
985 $cache->delete( $key );
986 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
987 $this->assertEquals( $value, $v, "Value returned" );
988 $this->assertEquals( 1, $wasSet, "Value regenerated" );
992 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
993 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
994 $this->assertEquals( $value, $v, "Value returned" );
995 $this->assertEquals( 1, $wasSet, "Value regenerated" );
999 public static function getWithSetCallback_versions_provider() {
1002 [ [ 'version' => 1 ], true ]
1007 * @covers WANObjectCache::touchCheckKey
1008 * @covers WANObjectCache::resetCheckKey
1009 * @covers WANObjectCache::getCheckKeyTime
1010 * @covers WANObjectCache::makePurgeValue
1011 * @covers WANObjectCache::parsePurgeValue
1013 public function testTouchKeys() {
1014 $key = wfRandomString();
1016 $priorTime = microtime( true );
1018 $t0 = $this->cache
->getCheckKeyTime( $key );
1019 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1021 $priorTime = microtime( true );
1023 $this->cache
->touchCheckKey( $key );
1024 $t1 = $this->cache
->getCheckKeyTime( $key );
1025 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1027 $t2 = $this->cache
->getCheckKeyTime( $key );
1028 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1031 $this->cache
->touchCheckKey( $key );
1032 $t3 = $this->cache
->getCheckKeyTime( $key );
1033 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1035 $t4 = $this->cache
->getCheckKeyTime( $key );
1036 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1039 $this->cache
->resetCheckKey( $key );
1040 $t5 = $this->cache
->getCheckKeyTime( $key );
1041 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1043 $t6 = $this->cache
->getCheckKeyTime( $key );
1044 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1048 * @covers WANObjectCache::getMulti()
1050 public function testGetWithSeveralCheckKeys() {
1051 $key = wfRandomString();
1052 $tKey1 = wfRandomString();
1053 $tKey2 = wfRandomString();
1056 // Two check keys are newer (given hold-off) than $key, another is older
1057 $this->internalCache
->set(
1058 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1059 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1061 $this->internalCache
->set(
1062 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1063 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1065 $this->internalCache
->set(
1066 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1067 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1069 $this->cache
->set( $key, $value, 30 );
1072 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1073 $this->assertEquals( $value, $v, "Value matches" );
1074 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1075 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1079 * @covers WANObjectCache::reap()
1080 * @covers WANObjectCache::reapCheckKey()
1082 public function testReap() {
1083 $vKey1 = wfRandomString();
1084 $vKey2 = wfRandomString();
1085 $tKey1 = wfRandomString();
1086 $tKey2 = wfRandomString();
1089 $knownPurge = time() - 60;
1090 $goodTime = microtime( true ) - 5;
1091 $badTime = microtime( true ) - 300;
1093 $this->internalCache
->set(
1094 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1096 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1097 WANObjectCache
::FLD_VALUE
=> $value,
1098 WANObjectCache
::FLD_TTL
=> 3600,
1099 WANObjectCache
::FLD_TIME
=> $goodTime
1102 $this->internalCache
->set(
1103 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1105 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1106 WANObjectCache
::FLD_VALUE
=> $value,
1107 WANObjectCache
::FLD_TTL
=> 3600,
1108 WANObjectCache
::FLD_TIME
=> $badTime
1111 $this->internalCache
->set(
1112 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1113 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1115 $this->internalCache
->set(
1116 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1117 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1120 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1121 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1122 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1123 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1125 $this->assertFalse( $bad1 );
1126 $this->assertTrue( $bad2 );
1128 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1129 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1130 $this->assertFalse( $tBad1 );
1131 $this->assertTrue( $tBad2 );
1135 * @covers WANObjectCache::reap()
1137 public function testReap_fail() {
1138 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1139 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1140 $backend->expects( $this->once() )->method( 'get' )
1142 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1143 WANObjectCache
::FLD_VALUE
=> 'value',
1144 WANObjectCache
::FLD_TTL
=> 3600,
1145 WANObjectCache
::FLD_TIME
=> 300,
1147 $backend->expects( $this->once() )->method( 'changeTTL' )
1148 ->willReturn( false );
1150 $wanCache = new WANObjectCache( [
1151 'cache' => $backend,
1152 'pool' => 'testcache-hash',
1153 'relayer' => new EventRelayerNull( [] )
1157 $ret = $wanCache->reap( 'key', 360, $isStale );
1158 $this->assertTrue( $isStale, 'value was stale' );
1159 $this->assertFalse( $ret, 'changeTTL failed' );
1163 * @covers WANObjectCache::set()
1165 public function testSetWithLag() {
1168 $key = wfRandomString();
1169 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1170 $this->cache
->set( $key, $value, 30, $opts );
1171 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1173 $key = wfRandomString();
1174 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1175 $this->cache
->set( $key, $value, 30, $opts );
1176 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1178 $key = wfRandomString();
1179 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1180 $this->cache
->set( $key, $value, 30, $opts );
1181 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1185 * @covers WANObjectCache::set()
1187 public function testWritePending() {
1190 $key = wfRandomString();
1191 $opts = [ 'pending' => true ];
1192 $this->cache
->set( $key, $value, 30, $opts );
1193 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1196 public function testMcRouterSupport() {
1197 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1198 ->setMethods( [ 'set', 'delete' ] )->getMock();
1199 $localBag->expects( $this->never() )->method( 'set' );
1200 $localBag->expects( $this->never() )->method( 'delete' );
1201 $wanCache = new WANObjectCache( [
1202 'cache' => $localBag,
1203 'pool' => 'testcache-hash',
1204 'relayer' => new EventRelayerNull( [] )
1206 $valFunc = function () {
1210 // None of these should use broadcasting commands (e.g. SET, DELETE)
1211 $wanCache->get( 'x' );
1212 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1213 $wanCache->getMulti( [ 'x', 'y' ] );
1214 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1215 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1216 $wanCache->getCheckKeyTime( 'zzz' );
1217 $wanCache->reap( 'x', time() - 300 );
1218 $wanCache->reap( 'zzz', time() - 300 );
1222 * @dataProvider provideAdaptiveTTL
1223 * @covers WANObjectCache::adaptiveTTL()
1224 * @param float|int $ago
1225 * @param int $maxTTL
1226 * @param int $minTTL
1227 * @param float $factor
1228 * @param int $adaptiveTTL
1230 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1231 $mtime = $ago ?
time() - $ago : $ago;
1233 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1235 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1236 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1238 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1240 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1241 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1244 public static function provideAdaptiveTTL() {
1246 [ 3600, 900, 30, 0.2, 720 ],
1247 [ 3600, 500, 30, 0.2, 500 ],
1248 [ 3600, 86400, 800, 0.2, 800 ],
1249 [ false, 86400, 800, 0.2, 800 ],
1250 [ null, 86400, 800, 0.2, 800 ]
1255 * @covers WANObjectCache::__construct
1256 * @covers WANObjectCache::newEmpty
1258 public function testNewEmpty() {
1259 $this->assertInstanceOf(
1260 WANObjectCache
::class,
1261 WANObjectCache
::newEmpty()
1266 * @covers WANObjectCache::setLogger
1268 public function testSetLogger() {
1269 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1273 * @covers WANObjectCache::getQoS
1275 public function testGetQoS() {
1276 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1277 ->setMethods( [ 'getQoS' ] )->getMock();
1278 $backend->expects( $this->once() )->method( 'getQoS' )
1279 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1280 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1283 $wanCache::QOS_UNKNOWN
,
1284 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1289 * @covers WANObjectCache::makeKey
1291 public function testMakeKey() {
1292 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1293 ->setMethods( [ 'makeKey' ] )->getMock();
1294 $backend->expects( $this->once() )->method( 'makeKey' )
1295 ->willReturn( 'special' );
1297 $wanCache = new WANObjectCache( [
1298 'cache' => $backend,
1299 'pool' => 'testcache-hash',
1300 'relayer' => new EventRelayerNull( [] )
1303 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1307 * @covers WANObjectCache::makeGlobalKey
1309 public function testMakeGlobalKey() {
1310 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1311 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1312 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1313 ->willReturn( 'special' );
1315 $wanCache = new WANObjectCache( [
1316 'cache' => $backend,
1317 'pool' => 'testcache-hash',
1318 'relayer' => new EventRelayerNull( [] )
1321 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );