3 use Wikimedia\TestingAccessWrapper
;
5 class WANObjectCacheTest
extends PHPUnit_Framework_TestCase
{
6 /** @var WANObjectCache */
9 private $internalCache;
11 protected function setUp() {
14 $this->cache
= new WANObjectCache( [
15 'cache' => new HashBagOStuff(),
16 'pool' => 'testcache-hash',
17 'relayer' => new EventRelayerNull( [] )
20 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
21 /** @noinspection PhpUndefinedFieldInspection */
22 $this->internalCache
= $wanCache->cache
;
26 * @dataProvider provideSetAndGet
27 * @covers WANObjectCache::set()
28 * @covers WANObjectCache::get()
29 * @covers WANObjectCache::makeKey()
33 public function testSetAndGet( $value, $ttl ) {
36 $key = $this->cache
->makeKey( 'x', wfRandomString() );
38 $this->cache
->get( $key, $curTTL, [], $asOf );
39 $this->assertNull( $curTTL, "Current TTL is null" );
40 $this->assertNull( $asOf, "Current as-of-time is infinite" );
42 $t = microtime( true );
43 $this->cache
->set( $key, $value, $ttl );
45 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
46 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
47 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
49 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
50 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
52 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
53 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
56 public static function provideSetAndGet() {
63 [ (object)[ 'meow' ], 3 ],
71 * @covers WANObjectCache::get()
72 * @covers WANObjectCache::makeGlobalKey()
74 public function testGetNotExists() {
75 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
77 $value = $this->cache
->get( $key, $curTTL );
79 $this->assertFalse( $value, "Non-existing key has false value" );
80 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
84 * @covers WANObjectCache::set()
86 public function testSetOver() {
87 $key = wfRandomString();
88 for ( $i = 0; $i < 3; ++
$i ) {
89 $value = wfRandomString();
90 $this->cache
->set( $key, $value, 3 );
92 $this->assertEquals( $this->cache
->get( $key ), $value );
97 * @covers WANObjectCache::set()
99 public function testStaleSet() {
100 $key = wfRandomString();
101 $value = wfRandomString();
102 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
104 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
107 public function testProcessCache() {
109 $callback = function () use ( &$hit ) {
113 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
114 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
116 foreach ( $keys as $i => $key ) {
117 $this->cache
->getWithSetCallback(
118 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
120 $this->assertEquals( 3, $hit );
122 foreach ( $keys as $i => $key ) {
123 $this->cache
->getWithSetCallback(
124 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
126 $this->assertEquals( 3, $hit, "Values cached" );
128 foreach ( $keys as $i => $key ) {
129 $this->cache
->getWithSetCallback(
130 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
132 $this->assertEquals( 6, $hit );
134 foreach ( $keys as $i => $key ) {
135 $this->cache
->getWithSetCallback(
136 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
138 $this->assertEquals( 6, $hit, "New values cached" );
140 foreach ( $keys as $i => $key ) {
141 $this->cache
->delete( $key );
142 $this->cache
->getWithSetCallback(
143 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
145 $this->assertEquals( 9, $hit, "Values evicted" );
147 $key = reset( $keys );
149 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
150 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
151 $this->assertEquals( 10, $hit, "Value cached" );
152 $outerCallback = function () use ( &$callback, $key ) {
153 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
157 $this->cache
->getWithSetCallback( $key, 100, $outerCallback );
158 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
162 * @dataProvider getWithSetCallback_provider
163 * @covers WANObjectCache::getWithSetCallback()
164 * @covers WANObjectCache::doGetWithSetCallback()
165 * @param array $extOpts
166 * @param bool $versioned
168 public function testGetWithSetCallback( array $extOpts, $versioned ) {
169 $cache = $this->cache
;
171 $key = wfRandomString();
172 $value = wfRandomString();
173 $cKey1 = wfRandomString();
174 $cKey2 = wfRandomString();
179 $func = function ( $old, &$ttl, &$opts, $asOf )
180 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
185 $ttl = 20; // override with another value
190 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
191 $this->assertEquals( $value, $v, "Value returned" );
192 $this->assertEquals( 1, $wasSet, "Value regenerated" );
193 $this->assertFalse( $priorValue, "No prior value" );
194 $this->assertNull( $priorAsOf, "No prior value" );
197 $cache->get( $key, $curTTL );
198 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
199 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
202 $v = $cache->getWithSetCallback( $key, 30, $func, [
206 $this->assertEquals( $value, $v, "Value returned" );
207 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
209 $priorTime = microtime( true );
212 $v = $cache->getWithSetCallback(
213 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
215 $this->assertEquals( $value, $v, "Value returned" );
216 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
217 $this->assertEquals( $value, $priorValue, "Has prior value" );
218 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
219 $t1 = $cache->getCheckKeyTime( $cKey1 );
220 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
221 $t2 = $cache->getCheckKeyTime( $cKey2 );
222 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
224 $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 still-recent check keys" );
231 $t1 = $cache->getCheckKeyTime( $cKey1 );
232 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
233 $t2 = $cache->getCheckKeyTime( $cKey2 );
234 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
237 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
239 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
241 $this->assertEquals( $value, $v, "Value returned" );
243 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
246 $key = wfRandomString();
247 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
248 $this->assertEquals( $value, $v, "Value returned" );
249 $cache->delete( $key );
250 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
251 $this->assertEquals( $value, $v, "Value still returned after deleted" );
252 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
255 public static function getWithSetCallback_provider() {
258 [ [ 'version' => 1 ], true ]
263 * @covers WANObjectCache::getWithSetCallback()
264 * @covers WANObjectCache::doGetWithSetCallback()
266 public function testGetWithSetCallback_invalidCallback() {
267 $this->setExpectedException( InvalidArgumentException
::class );
268 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
272 * @dataProvider getMultiWithSetCallback_provider
273 * @covers WANObjectCache::getMultiWithSetCallback()
274 * @covers WANObjectCache::makeMultiKeys()
275 * @covers WANObjectCache::getMulti()
276 * @param array $extOpts
277 * @param bool $versioned
279 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
280 $cache = $this->cache
;
282 $keyA = wfRandomString();
283 $keyB = wfRandomString();
284 $keyC = wfRandomString();
285 $cKey1 = wfRandomString();
286 $cKey2 = wfRandomString();
291 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
292 &$wasSet, &$priorValue, &$priorAsOf
297 $ttl = 20; // override with another value
302 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
304 $v = $cache->getMultiWithSetCallback(
305 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
306 $this->assertEquals( $value, $v[$keyA], "Value returned" );
307 $this->assertEquals( 1, $wasSet, "Value regenerated" );
308 $this->assertFalse( $priorValue, "No prior value" );
309 $this->assertNull( $priorAsOf, "No prior value" );
312 $cache->get( $keyA, $curTTL );
313 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
314 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
318 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
319 $v = $cache->getMultiWithSetCallback(
320 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
321 $this->assertEquals( $value, $v[$keyB], "Value returned" );
322 $this->assertEquals( 1, $wasSet, "Value regenerated" );
323 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
324 $v = $cache->getMultiWithSetCallback(
325 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
326 $this->assertEquals( $value, $v[$keyB], "Value returned" );
327 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
328 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
330 $priorTime = microtime( true );
333 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
334 $v = $cache->getMultiWithSetCallback(
335 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
337 $this->assertEquals( $value, $v[$keyB], "Value returned" );
338 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
339 $this->assertEquals( $value, $priorValue, "Has prior value" );
340 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
341 $t1 = $cache->getCheckKeyTime( $cKey1 );
342 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
343 $t2 = $cache->getCheckKeyTime( $cKey2 );
344 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
346 $priorTime = microtime( true );
349 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
350 $v = $cache->getMultiWithSetCallback(
351 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
353 $this->assertEquals( $value, $v[$keyC], "Value returned" );
354 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
355 $t1 = $cache->getCheckKeyTime( $cKey1 );
356 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
357 $t2 = $cache->getCheckKeyTime( $cKey2 );
358 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
361 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
363 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
365 $this->assertEquals( $value, $v, "Value returned" );
367 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
370 $key = wfRandomString();
371 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
372 $v = $cache->getMultiWithSetCallback(
373 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
374 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
375 $cache->delete( $key );
376 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
377 $v = $cache->getMultiWithSetCallback(
378 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
379 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
380 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
383 $ids = [ 1, 2, 3, 4, 5, 6 ];
384 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
385 return $wanCache->makeKey( 'test', $id );
387 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
388 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
393 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
396 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
397 array_values( $values ),
398 "Correct values in correct order"
401 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
402 array_keys( $values ),
403 "Correct keys in correct order"
405 $this->assertEquals( count( $ids ), $calls );
407 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
408 $this->assertEquals( count( $ids ), $calls, "Values cached" );
410 // Mock the BagOStuff to assure only one getMulti() call given process caching
411 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
412 ->setMethods( [ 'getMulti' ] )->getMock();
413 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
414 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
415 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
417 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
419 // Warm the process cache
420 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
422 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
423 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
425 // Use the process cache
427 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
428 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
432 public static function getMultiWithSetCallback_provider() {
435 [ [ 'version' => 1 ], true ]
440 * @dataProvider getMultiWithUnionSetCallback_provider
441 * @covers WANObjectCache::getMultiWithUnionSetCallback()
442 * @covers WANObjectCache::makeMultiKeys()
443 * @param array $extOpts
444 * @param bool $versioned
446 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
447 $cache = $this->cache
;
449 $keyA = wfRandomString();
450 $keyB = wfRandomString();
451 $keyC = wfRandomString();
452 $cKey1 = wfRandomString();
453 $cKey2 = wfRandomString();
456 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
457 &$wasSet, &$priorValue, &$priorAsOf
460 foreach ( $ids as $id ) {
462 $newValues[$id] = "@$id$";
463 $ttls[$id] = 20; // override with another value
470 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
472 $v = $cache->getMultiWithUnionSetCallback(
473 $keyedIds, 30, $genFunc, $extOpts );
474 $this->assertEquals( $value, $v[$keyA], "Value returned" );
475 $this->assertEquals( 1, $wasSet, "Value regenerated" );
478 $cache->get( $keyA, $curTTL );
479 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
480 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
484 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
485 $v = $cache->getMultiWithUnionSetCallback(
486 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
487 $this->assertEquals( $value, $v[$keyB], "Value returned" );
488 $this->assertEquals( 1, $wasSet, "Value regenerated" );
489 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
490 $v = $cache->getMultiWithUnionSetCallback(
491 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
492 $this->assertEquals( $value, $v[$keyB], "Value returned" );
493 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
494 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
496 $priorTime = microtime( true );
499 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
500 $v = $cache->getMultiWithUnionSetCallback(
501 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
503 $this->assertEquals( $value, $v[$keyB], "Value returned" );
504 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
505 $t1 = $cache->getCheckKeyTime( $cKey1 );
506 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
507 $t2 = $cache->getCheckKeyTime( $cKey2 );
508 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
510 $priorTime = microtime( true );
513 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
514 $v = $cache->getMultiWithUnionSetCallback(
515 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
517 $this->assertEquals( $value, $v[$keyC], "Value returned" );
518 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
519 $t1 = $cache->getCheckKeyTime( $cKey1 );
520 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
521 $t2 = $cache->getCheckKeyTime( $cKey2 );
522 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
525 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
527 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
529 $this->assertEquals( $value, $v, "Value returned" );
531 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
534 $key = wfRandomString();
535 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
536 $v = $cache->getMultiWithUnionSetCallback(
537 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
538 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
539 $cache->delete( $key );
540 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
541 $v = $cache->getMultiWithUnionSetCallback(
542 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
543 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
544 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
547 $ids = [ 1, 2, 3, 4, 5, 6 ];
548 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
549 return $wanCache->makeKey( 'test', $id );
551 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
552 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
554 foreach ( $ids as $id ) {
556 $newValues[$id] = "val-{$id}";
561 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
564 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
565 array_values( $values ),
566 "Correct values in correct order"
569 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
570 array_keys( $values ),
571 "Correct keys in correct order"
573 $this->assertEquals( count( $ids ), $calls );
575 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
576 $this->assertEquals( count( $ids ), $calls, "Values cached" );
579 public static function getMultiWithUnionSetCallback_provider() {
582 [ [ 'version' => 1 ], true ]
587 * @covers WANObjectCache::getWithSetCallback()
588 * @covers WANObjectCache::doGetWithSetCallback()
590 public function testLockTSE() {
591 $cache = $this->cache
;
592 $key = wfRandomString();
593 $value = wfRandomString();
596 $func = function () use ( &$calls, $value, $cache, $key ) {
598 // Immediately kill any mutex rather than waiting a second
599 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
603 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
604 $this->assertEquals( $value, $ret );
605 $this->assertEquals( 1, $calls, 'Value was populated' );
607 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
608 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
610 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
611 $ret = $cache->getWithSetCallback( $key, 30, $func,
612 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
613 $this->assertEquals( $value, $ret, 'Old value used' );
614 $this->assertEquals( 1, $calls, 'Callback was not used' );
616 $cache->delete( $key );
617 $ret = $cache->getWithSetCallback( $key, 30, $func,
618 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
619 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
620 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
622 $ret = $cache->getWithSetCallback( $key, 30, $func,
623 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
624 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
625 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
629 * @covers WANObjectCache::getWithSetCallback()
630 * @covers WANObjectCache::doGetWithSetCallback()
631 * @covers WANObjectCache::set()
633 public function testLockTSESlow() {
634 $cache = $this->cache
;
635 $key = wfRandomString();
636 $value = wfRandomString();
639 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
641 $setOpts['since'] = microtime( true ) - 10;
642 // Immediately kill any mutex rather than waiting a second
643 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
647 // Value should be marked as stale due to snapshot lag
649 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
650 $this->assertEquals( $value, $ret );
651 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
652 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
653 $this->assertEquals( 1, $calls, 'Value was generated' );
655 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
656 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
657 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
658 $this->assertEquals( $value, $ret );
659 $this->assertEquals( 1, $calls, 'Callback was not used' );
663 * @covers WANObjectCache::getWithSetCallback()
664 * @covers WANObjectCache::doGetWithSetCallback()
666 public function testBusyValue() {
667 $cache = $this->cache
;
668 $key = wfRandomString();
669 $value = wfRandomString();
670 $busyValue = wfRandomString();
673 $func = function () use ( &$calls, $value, $cache, $key ) {
675 // Immediately kill any mutex rather than waiting a second
676 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
680 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
681 $this->assertEquals( $value, $ret );
682 $this->assertEquals( 1, $calls, 'Value was populated' );
684 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
685 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
687 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
688 $ret = $cache->getWithSetCallback( $key, 30, $func,
689 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
690 $this->assertEquals( $value, $ret, 'Callback used' );
691 $this->assertEquals( 2, $calls, 'Callback used' );
693 $ret = $cache->getWithSetCallback( $key, 30, $func,
694 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
695 $this->assertEquals( $value, $ret, 'Old value used' );
696 $this->assertEquals( 2, $calls, 'Callback was not used' );
698 $cache->delete( $key ); // no value at all anymore and still locked
699 $ret = $cache->getWithSetCallback( $key, 30, $func,
700 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
701 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
702 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
704 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
705 $ret = $cache->getWithSetCallback( $key, 30, $func,
706 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
707 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
708 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
710 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
711 $ret = $cache->getWithSetCallback( $key, 30, $func,
712 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
713 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
714 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
718 * @covers WANObjectCache::getMulti()
720 public function testGetMulti() {
721 $cache = $this->cache
;
723 $value1 = [ 'this' => 'is', 'a' => 'test' ];
724 $value2 = [ 'this' => 'is', 'another' => 'test' ];
726 $key1 = wfRandomString();
727 $key2 = wfRandomString();
728 $key3 = wfRandomString();
730 $cache->set( $key1, $value1, 5 );
731 $cache->set( $key2, $value2, 10 );
735 [ $key1 => $value1, $key2 => $value2 ],
736 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
737 'Result array populated'
740 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
741 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
742 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
744 $cKey1 = wfRandomString();
745 $cKey2 = wfRandomString();
747 $priorTime = microtime( true );
751 [ $key1 => $value1, $key2 => $value2 ],
752 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
753 "Result array populated even with new check keys"
755 $t1 = $cache->getCheckKeyTime( $cKey1 );
756 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
757 $t2 = $cache->getCheckKeyTime( $cKey2 );
758 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
759 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
760 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
761 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
766 [ $key1 => $value1, $key2 => $value2 ],
767 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
768 "Result array still populated even with new check keys"
770 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
771 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
772 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
776 * @covers WANObjectCache::getMulti()
777 * @covers WANObjectCache::processCheckKeys()
779 public function testGetMultiCheckKeys() {
780 $cache = $this->cache
;
782 $checkAll = wfRandomString();
783 $check1 = wfRandomString();
784 $check2 = wfRandomString();
785 $check3 = wfRandomString();
786 $value1 = wfRandomString();
787 $value2 = wfRandomString();
789 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
790 // several seconds during the test to assert the behaviour.
791 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
792 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
796 $cache->set( 'key1', $value1, 10 );
797 $cache->set( 'key2', $value2, 10 );
800 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
807 [ 'key1' => $value1, 'key2' => $value2 ],
811 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
812 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
813 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
814 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
816 $cache->touchCheckKey( $check1 );
819 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
826 [ 'key1' => $value1, 'key2' => $value2 ],
828 'key1 expired by check1, but value still provided'
830 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
831 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
833 $cache->touchCheckKey( $checkAll );
836 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
843 [ 'key1' => $value1, 'key2' => $value2 ],
845 'All keys expired by checkAll, but value still provided'
847 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
848 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
852 * @covers WANObjectCache::get()
853 * @covers WANObjectCache::processCheckKeys()
855 public function testCheckKeyInitHoldoff() {
856 $cache = $this->cache
;
858 for ( $i = 0; $i < 500; ++
$i ) {
859 $key = wfRandomString();
860 $checkKey = wfRandomString();
862 $cache->get( $key, $curTTL, [ $checkKey ] );
863 $cache->set( $key, 'val', 10 );
865 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
867 $this->assertEquals( 'val', $v );
868 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
871 for ( $i = 0; $i < 500; ++
$i ) {
872 $key = wfRandomString();
873 $checkKey = wfRandomString();
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 (set/hit)" );
885 * @covers WANObjectCache::delete()
887 public function testDelete() {
888 $key = wfRandomString();
889 $value = wfRandomString();
890 $this->cache
->set( $key, $value );
893 $v = $this->cache
->get( $key, $curTTL );
894 $this->assertEquals( $value, $v, "Key was created with value" );
895 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
897 $this->cache
->delete( $key );
900 $v = $this->cache
->get( $key, $curTTL );
901 $this->assertFalse( $v, "Deleted key has false value" );
902 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
904 $this->cache
->set( $key, $value . 'more' );
905 $v = $this->cache
->get( $key, $curTTL );
906 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
907 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
909 $this->cache
->set( $key, $value );
910 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
913 $v = $this->cache
->get( $key, $curTTL );
914 $this->assertFalse( $v, "Deleted key has false value" );
915 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
917 $this->cache
->set( $key, $value );
918 $v = $this->cache
->get( $key, $curTTL );
919 $this->assertEquals( $value, $v, "Key was created with value" );
920 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
924 * @dataProvider getWithSetCallback_versions_provider
925 * @covers WANObjectCache::getWithSetCallback()
926 * @covers WANObjectCache::doGetWithSetCallback()
927 * @param array $extOpts
928 * @param bool $versioned
930 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
931 $cache = $this->cache
;
933 $key = wfRandomString();
934 $value = wfRandomString();
937 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
942 // Set the main key (version N if versioned)
944 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
945 $this->assertEquals( $value, $v, "Value returned" );
946 $this->assertEquals( 1, $wasSet, "Value regenerated" );
947 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
948 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
949 // Set the key for version N+1 (if versioned)
951 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
954 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
955 $this->assertEquals( $value, $v, "Value returned" );
956 $this->assertEquals( 1, $wasSet, "Value regenerated" );
959 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
960 $this->assertEquals( $value, $v, "Value returned" );
961 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
965 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
966 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
969 $cache->delete( $key );
970 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
971 $this->assertEquals( $value, $v, "Value returned" );
972 $this->assertEquals( 1, $wasSet, "Value regenerated" );
976 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
977 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
978 $this->assertEquals( $value, $v, "Value returned" );
979 $this->assertEquals( 1, $wasSet, "Value regenerated" );
983 public static function getWithSetCallback_versions_provider() {
986 [ [ 'version' => 1 ], true ]
991 * @covers WANObjectCache::touchCheckKey()
992 * @covers WANObjectCache::resetCheckKey()
993 * @covers WANObjectCache::getCheckKeyTime()
995 public function testTouchKeys() {
996 $key = wfRandomString();
998 $priorTime = microtime( true );
1000 $t0 = $this->cache
->getCheckKeyTime( $key );
1001 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1003 $priorTime = microtime( true );
1005 $this->cache
->touchCheckKey( $key );
1006 $t1 = $this->cache
->getCheckKeyTime( $key );
1007 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1009 $t2 = $this->cache
->getCheckKeyTime( $key );
1010 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1013 $this->cache
->touchCheckKey( $key );
1014 $t3 = $this->cache
->getCheckKeyTime( $key );
1015 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1017 $t4 = $this->cache
->getCheckKeyTime( $key );
1018 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1021 $this->cache
->resetCheckKey( $key );
1022 $t5 = $this->cache
->getCheckKeyTime( $key );
1023 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1025 $t6 = $this->cache
->getCheckKeyTime( $key );
1026 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1030 * @covers WANObjectCache::getMulti()
1032 public function testGetWithSeveralCheckKeys() {
1033 $key = wfRandomString();
1034 $tKey1 = wfRandomString();
1035 $tKey2 = wfRandomString();
1038 // Two check keys are newer (given hold-off) than $key, another is older
1039 $this->internalCache
->set(
1040 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1041 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1043 $this->internalCache
->set(
1044 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1045 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1047 $this->internalCache
->set(
1048 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1049 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1051 $this->cache
->set( $key, $value, 30 );
1054 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1055 $this->assertEquals( $value, $v, "Value matches" );
1056 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1057 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1061 * @covers WANObjectCache::reap()
1062 * @covers WANObjectCache::reapCheckKey()
1064 public function testReap() {
1065 $vKey1 = wfRandomString();
1066 $vKey2 = wfRandomString();
1067 $tKey1 = wfRandomString();
1068 $tKey2 = wfRandomString();
1071 $knownPurge = time() - 60;
1072 $goodTime = microtime( true ) - 5;
1073 $badTime = microtime( true ) - 300;
1075 $this->internalCache
->set(
1076 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1078 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1079 WANObjectCache
::FLD_VALUE
=> $value,
1080 WANObjectCache
::FLD_TTL
=> 3600,
1081 WANObjectCache
::FLD_TIME
=> $goodTime
1084 $this->internalCache
->set(
1085 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1087 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1088 WANObjectCache
::FLD_VALUE
=> $value,
1089 WANObjectCache
::FLD_TTL
=> 3600,
1090 WANObjectCache
::FLD_TIME
=> $badTime
1093 $this->internalCache
->set(
1094 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1095 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1097 $this->internalCache
->set(
1098 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1099 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1102 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1103 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1104 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1105 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1107 $this->assertFalse( $bad1 );
1108 $this->assertTrue( $bad2 );
1110 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1111 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1112 $this->assertFalse( $tBad1 );
1113 $this->assertTrue( $tBad2 );
1117 * @covers WANObjectCache::reap()
1119 public function testReap_fail() {
1120 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1121 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1122 $backend->expects( $this->once() )->method( 'get' )
1124 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1125 WANObjectCache
::FLD_VALUE
=> 'value',
1126 WANObjectCache
::FLD_TTL
=> 3600,
1127 WANObjectCache
::FLD_TIME
=> 300,
1129 $backend->expects( $this->once() )->method( 'changeTTL' )
1130 ->willReturn( false );
1132 $wanCache = new WANObjectCache( [
1133 'cache' => $backend,
1134 'pool' => 'testcache-hash',
1135 'relayer' => new EventRelayerNull( [] )
1139 $ret = $wanCache->reap( 'key', 360, $isStale );
1140 $this->assertTrue( $isStale, 'value was stale' );
1141 $this->assertFalse( $ret, 'changeTTL failed' );
1145 * @covers WANObjectCache::set()
1147 public function testSetWithLag() {
1150 $key = wfRandomString();
1151 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1152 $this->cache
->set( $key, $value, 30, $opts );
1153 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1155 $key = wfRandomString();
1156 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1157 $this->cache
->set( $key, $value, 30, $opts );
1158 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1160 $key = wfRandomString();
1161 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1162 $this->cache
->set( $key, $value, 30, $opts );
1163 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1167 * @covers WANObjectCache::set()
1169 public function testWritePending() {
1172 $key = wfRandomString();
1173 $opts = [ 'pending' => true ];
1174 $this->cache
->set( $key, $value, 30, $opts );
1175 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1178 public function testMcRouterSupport() {
1179 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1180 ->setMethods( [ 'set', 'delete' ] )->getMock();
1181 $localBag->expects( $this->never() )->method( 'set' );
1182 $localBag->expects( $this->never() )->method( 'delete' );
1183 $wanCache = new WANObjectCache( [
1184 'cache' => $localBag,
1185 'pool' => 'testcache-hash',
1186 'relayer' => new EventRelayerNull( [] )
1188 $valFunc = function () {
1192 // None of these should use broadcasting commands (e.g. SET, DELETE)
1193 $wanCache->get( 'x' );
1194 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1195 $wanCache->getMulti( [ 'x', 'y' ] );
1196 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1197 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1198 $wanCache->getCheckKeyTime( 'zzz' );
1199 $wanCache->reap( 'x', time() - 300 );
1200 $wanCache->reap( 'zzz', time() - 300 );
1204 * @dataProvider provideAdaptiveTTL
1205 * @covers WANObjectCache::adaptiveTTL()
1206 * @param float|int $ago
1207 * @param int $maxTTL
1208 * @param int $minTTL
1209 * @param float $factor
1210 * @param int $adaptiveTTL
1212 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1213 $mtime = $ago ?
time() - $ago : $ago;
1215 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1217 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1218 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1220 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1222 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1223 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1226 public static function provideAdaptiveTTL() {
1228 [ 3600, 900, 30, 0.2, 720 ],
1229 [ 3600, 500, 30, 0.2, 500 ],
1230 [ 3600, 86400, 800, 0.2, 800 ],
1231 [ false, 86400, 800, 0.2, 800 ],
1232 [ null, 86400, 800, 0.2, 800 ]
1237 * @covers WANObjectCache::makeKey
1239 public function testMakeKey() {
1240 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1241 ->setMethods( [ 'makeKey' ] )->getMock();
1242 $backend->expects( $this->once() )->method( 'makeKey' )
1243 ->willReturn( 'special' );
1245 $wanCache = new WANObjectCache( [
1246 'cache' => $backend,
1247 'pool' => 'testcache-hash',
1248 'relayer' => new EventRelayerNull( [] )
1251 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1255 * @covers WANObjectCache::makeGlobalKey
1257 public function testMakeGlobalKey() {
1258 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1259 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1260 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1261 ->willReturn( 'special' );
1263 $wanCache = new WANObjectCache( [
1264 'cache' => $backend,
1265 'pool' => 'testcache-hash',
1266 'relayer' => new EventRelayerNull( [] )
1269 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );