Merge "Soft deprecate $wgStyleVersion (add @deprecated)"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
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
18 */
19 class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
20 /** @var WANObjectCache */
21 private $cache;
22 /** @var BagOStuff */
23 private $internalCache;
24
25 protected function setUp() {
26 parent::setUp();
27
28 $this->cache = new WANObjectCache( [
29 'cache' => new HashBagOStuff(),
30 'pool' => 'testcache-hash',
31 'relayer' => new EventRelayerNull( [] )
32 ] );
33
34 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
35 /** @noinspection PhpUndefinedFieldInspection */
36 $this->internalCache = $wanCache->cache;
37 }
38
39 /**
40 * @dataProvider provideSetAndGet
41 * @covers WANObjectCache::set()
42 * @covers WANObjectCache::get()
43 * @covers WANObjectCache::makeKey()
44 * @param mixed $value
45 * @param int $ttl
46 */
47 public function testSetAndGet( $value, $ttl ) {
48 $curTTL = null;
49 $asOf = null;
50 $key = $this->cache->makeKey( 'x', wfRandomString() );
51
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" );
55
56 $t = microtime( true );
57 $this->cache->set( $key, $value, $ttl );
58
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" );
62 } else {
63 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
64 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
65 }
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" );
68 }
69
70 public static function provideSetAndGet() {
71 return [
72 [ 14141, 3 ],
73 [ 3535.666, 3 ],
74 [ [], 3 ],
75 [ null, 3 ],
76 [ '0', 3 ],
77 [ (object)[ 'meow' ], 3 ],
78 [ INF, 3 ],
79 [ '', 3 ],
80 [ 'pizzacat', INF ],
81 ];
82 }
83
84 /**
85 * @covers WANObjectCache::get()
86 * @covers WANObjectCache::makeGlobalKey()
87 */
88 public function testGetNotExists() {
89 $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
90 $curTTL = null;
91 $value = $this->cache->get( $key, $curTTL );
92
93 $this->assertFalse( $value, "Non-existing key has false value" );
94 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
95 }
96
97 /**
98 * @covers WANObjectCache::set()
99 */
100 public function testSetOver() {
101 $key = wfRandomString();
102 for ( $i = 0; $i < 3; ++$i ) {
103 $value = wfRandomString();
104 $this->cache->set( $key, $value, 3 );
105
106 $this->assertEquals( $this->cache->get( $key ), $value );
107 }
108 }
109
110 /**
111 * @covers WANObjectCache::set()
112 */
113 public function testStaleSet() {
114 $key = wfRandomString();
115 $value = wfRandomString();
116 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
117
118 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
119 }
120
121 public function testProcessCache() {
122 $hit = 0;
123 $callback = function () use ( &$hit ) {
124 ++$hit;
125 return 42;
126 };
127 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
128 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
129
130 foreach ( $keys as $i => $key ) {
131 $this->cache->getWithSetCallback(
132 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
133 }
134 $this->assertEquals( 3, $hit );
135
136 foreach ( $keys as $i => $key ) {
137 $this->cache->getWithSetCallback(
138 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
139 }
140 $this->assertEquals( 3, $hit, "Values cached" );
141
142 foreach ( $keys as $i => $key ) {
143 $this->cache->getWithSetCallback(
144 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
145 }
146 $this->assertEquals( 6, $hit );
147
148 foreach ( $keys as $i => $key ) {
149 $this->cache->getWithSetCallback(
150 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
151 }
152 $this->assertEquals( 6, $hit, "New values cached" );
153
154 foreach ( $keys as $i => $key ) {
155 $this->cache->delete( $key );
156 $this->cache->getWithSetCallback(
157 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
158 }
159 $this->assertEquals( 9, $hit, "Values evicted" );
160
161 $key = reset( $keys );
162 // Get into cache (default process cache group)
163 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
164 $this->assertEquals( 10, $hit, "Value calculated" );
165 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
166 $this->assertEquals( 10, $hit, "Value cached" );
167 $outerCallback = function () use ( &$callback, $key ) {
168 $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
169
170 return 43 + $v;
171 };
172 // Outer key misses and refuses inner key process cache value
173 $this->cache->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
174 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
175 }
176
177 /**
178 * @dataProvider getWithSetCallback_provider
179 * @covers WANObjectCache::getWithSetCallback()
180 * @covers WANObjectCache::doGetWithSetCallback()
181 * @param array $extOpts
182 * @param bool $versioned
183 */
184 public function testGetWithSetCallback( array $extOpts, $versioned ) {
185 $cache = $this->cache;
186
187 $key = wfRandomString();
188 $value = wfRandomString();
189 $cKey1 = wfRandomString();
190 $cKey2 = wfRandomString();
191
192 $priorValue = null;
193 $priorAsOf = null;
194 $wasSet = 0;
195 $func = function ( $old, &$ttl, &$opts, $asOf )
196 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
197 {
198 ++$wasSet;
199 $priorValue = $old;
200 $priorAsOf = $asOf;
201 $ttl = 20; // override with another value
202 return $value;
203 };
204
205 $wasSet = 0;
206 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
207 $this->assertEquals( $value, $v, "Value returned" );
208 $this->assertEquals( 1, $wasSet, "Value regenerated" );
209 $this->assertFalse( $priorValue, "No prior value" );
210 $this->assertNull( $priorAsOf, "No prior value" );
211
212 $curTTL = null;
213 $cache->get( $key, $curTTL );
214 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
215 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
216
217 $wasSet = 0;
218 $v = $cache->getWithSetCallback( $key, 30, $func, [
219 'lowTTL' => 0,
220 'lockTSE' => 5,
221 ] + $extOpts );
222 $this->assertEquals( $value, $v, "Value returned" );
223 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
224
225 $priorTime = microtime( true );
226 usleep( 1 );
227 $wasSet = 0;
228 $v = $cache->getWithSetCallback(
229 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
230 );
231 $this->assertEquals( $value, $v, "Value returned" );
232 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
233 $this->assertEquals( $value, $priorValue, "Has prior value" );
234 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
235 $t1 = $cache->getCheckKeyTime( $cKey1 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
237 $t2 = $cache->getCheckKeyTime( $cKey2 );
238 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
239
240 $priorTime = microtime( true );
241 $wasSet = 0;
242 $v = $cache->getWithSetCallback(
243 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
244 );
245 $this->assertEquals( $value, $v, "Value returned" );
246 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
247 $t1 = $cache->getCheckKeyTime( $cKey1 );
248 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
249 $t2 = $cache->getCheckKeyTime( $cKey2 );
250 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
251
252 $curTTL = null;
253 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
254 if ( $versioned ) {
255 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
256 } else {
257 $this->assertEquals( $value, $v, "Value returned" );
258 }
259 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
260
261 $wasSet = 0;
262 $key = wfRandomString();
263 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
264 $this->assertEquals( $value, $v, "Value returned" );
265 $cache->delete( $key );
266 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
267 $this->assertEquals( $value, $v, "Value still returned after deleted" );
268 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
269
270 $backToTheFutureCache = new TimeAdjustableWANObjectCache( [
271 'cache' => new TimeAdjustableHashBagOStuff(),
272 'pool' => 'empty'
273 ] );
274
275 $oldValReceived = -1;
276 $oldAsOfReceived = -1;
277 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
278 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
279 ++$wasSet;
280 $oldValReceived = $oldVal;
281 $oldAsOfReceived = $oldAsOf;
282
283 return 'xxx' . $wasSet;
284 };
285
286 $wasSet = 0;
287 $key = wfRandomString();
288 $v = $backToTheFutureCache->getWithSetCallback(
289 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
290 $this->assertEquals( 'xxx1', $v, "Value returned" );
291 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
292 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
293
294 $backToTheFutureCache->setTime( microtime( true ) + 40 );
295 $v = $backToTheFutureCache->getWithSetCallback(
296 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
297 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
298 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
299 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
300 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
301
302 $backToTheFutureCache->setTime( microtime( true ) + 300 );
303 $v = $backToTheFutureCache->getWithSetCallback(
304 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
305 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
306 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
307 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
308 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
309 }
310
311 public static function getWithSetCallback_provider() {
312 return [
313 [ [], false ],
314 [ [ 'version' => 1 ], true ]
315 ];
316 }
317
318 public function testPreemtiveRefresh() {
319 $value = 'KatCafe';
320 $wasSet = 0;
321 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value )
322 {
323 ++$wasSet;
324 return $value;
325 };
326
327 $cache = new NearExpiringWANObjectCache( [
328 'cache' => new HashBagOStuff(),
329 'pool' => 'empty'
330 ] );
331
332 $wasSet = 0;
333 $key = wfRandomString();
334 $opts = [ 'lowTTL' => 30 ];
335 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
336 $this->assertEquals( $value, $v, "Value returned" );
337 $this->assertEquals( 1, $wasSet, "Value calculated" );
338 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
339 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
340
341 $wasSet = 0;
342 $key = wfRandomString();
343 $opts = [ 'lowTTL' => 1 ];
344 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
345 $this->assertEquals( $value, $v, "Value returned" );
346 $this->assertEquals( 1, $wasSet, "Value calculated" );
347 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
348 $this->assertEquals( 1, $wasSet, "Value cached" );
349
350 $cache = new PopularityRefreshingWANObjectCache( [
351 'cache' => new HashBagOStuff(),
352 'pool' => 'empty'
353 ] );
354
355 $now = microtime( true ); // reference time
356 $wasSet = 0;
357 $key = wfRandomString();
358 $opts = [ 'hotTTR' => 900 ];
359 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
360 $this->assertEquals( $value, $v, "Value returned" );
361 $this->assertEquals( 1, $wasSet, "Value calculated" );
362 $cache->setTime( $now + 30 );
363 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
364 $this->assertEquals( 1, $wasSet, "Value cached" );
365
366 $wasSet = 0;
367 $key = wfRandomString();
368 $opts = [ 'hotTTR' => 10 ];
369 $cache->setTime( $now );
370 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
371 $this->assertEquals( $value, $v, "Value returned" );
372 $this->assertEquals( 1, $wasSet, "Value calculated" );
373 $cache->setTime( $now + 30 );
374 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
375 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
376 }
377
378 /**
379 * @covers WANObjectCache::getWithSetCallback()
380 * @covers WANObjectCache::doGetWithSetCallback()
381 */
382 public function testGetWithSetCallback_invalidCallback() {
383 $this->setExpectedException( InvalidArgumentException::class );
384 $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
385 }
386
387 /**
388 * @dataProvider getMultiWithSetCallback_provider
389 * @covers WANObjectCache::getMultiWithSetCallback
390 * @covers WANObjectCache::makeMultiKeys
391 * @covers WANObjectCache::getMulti
392 * @param array $extOpts
393 * @param bool $versioned
394 */
395 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
396 $cache = $this->cache;
397
398 $keyA = wfRandomString();
399 $keyB = wfRandomString();
400 $keyC = wfRandomString();
401 $cKey1 = wfRandomString();
402 $cKey2 = wfRandomString();
403
404 $priorValue = null;
405 $priorAsOf = null;
406 $wasSet = 0;
407 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
408 &$wasSet, &$priorValue, &$priorAsOf
409 ) {
410 ++$wasSet;
411 $priorValue = $old;
412 $priorAsOf = $asOf;
413 $ttl = 20; // override with another value
414 return "@$id$";
415 };
416
417 $wasSet = 0;
418 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
419 $value = "@3353$";
420 $v = $cache->getMultiWithSetCallback(
421 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
422 $this->assertEquals( $value, $v[$keyA], "Value returned" );
423 $this->assertEquals( 1, $wasSet, "Value regenerated" );
424 $this->assertFalse( $priorValue, "No prior value" );
425 $this->assertNull( $priorAsOf, "No prior value" );
426
427 $curTTL = null;
428 $cache->get( $keyA, $curTTL );
429 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
430 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
431
432 $wasSet = 0;
433 $value = "@efef$";
434 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
435 $v = $cache->getMultiWithSetCallback(
436 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
437 $this->assertEquals( $value, $v[$keyB], "Value returned" );
438 $this->assertEquals( 1, $wasSet, "Value regenerated" );
439 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
440 $v = $cache->getMultiWithSetCallback(
441 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
442 $this->assertEquals( $value, $v[$keyB], "Value returned" );
443 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
444 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
445
446 $priorTime = microtime( true );
447 usleep( 1 );
448 $wasSet = 0;
449 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
450 $v = $cache->getMultiWithSetCallback(
451 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
452 );
453 $this->assertEquals( $value, $v[$keyB], "Value returned" );
454 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
455 $this->assertEquals( $value, $priorValue, "Has prior value" );
456 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
457 $t1 = $cache->getCheckKeyTime( $cKey1 );
458 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
459 $t2 = $cache->getCheckKeyTime( $cKey2 );
460 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
461
462 $priorTime = microtime( true );
463 $value = "@43636$";
464 $wasSet = 0;
465 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
466 $v = $cache->getMultiWithSetCallback(
467 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
468 );
469 $this->assertEquals( $value, $v[$keyC], "Value returned" );
470 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
471 $t1 = $cache->getCheckKeyTime( $cKey1 );
472 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
473 $t2 = $cache->getCheckKeyTime( $cKey2 );
474 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
475
476 $curTTL = null;
477 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
478 if ( $versioned ) {
479 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
480 } else {
481 $this->assertEquals( $value, $v, "Value returned" );
482 }
483 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
484
485 $wasSet = 0;
486 $key = wfRandomString();
487 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
488 $v = $cache->getMultiWithSetCallback(
489 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
490 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
491 $cache->delete( $key );
492 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
493 $v = $cache->getMultiWithSetCallback(
494 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
495 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
496 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
497
498 $calls = 0;
499 $ids = [ 1, 2, 3, 4, 5, 6 ];
500 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
501 return $wanCache->makeKey( 'test', $id );
502 };
503 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
504 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
505 ++$calls;
506
507 return "val-{$id}";
508 };
509 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
510
511 $this->assertEquals(
512 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
513 array_values( $values ),
514 "Correct values in correct order"
515 );
516 $this->assertEquals(
517 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
518 array_keys( $values ),
519 "Correct keys in correct order"
520 );
521 $this->assertEquals( count( $ids ), $calls );
522
523 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
524 $this->assertEquals( count( $ids ), $calls, "Values cached" );
525
526 // Mock the BagOStuff to assure only one getMulti() call given process caching
527 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
528 ->setMethods( [ 'getMulti' ] )->getMock();
529 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
530 WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
531 WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
532 ] );
533 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
534
535 // Warm the process cache
536 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
537 $this->assertEquals(
538 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
539 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
540 );
541 // Use the process cache
542 $this->assertEquals(
543 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
544 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
545 );
546 }
547
548 public static function getMultiWithSetCallback_provider() {
549 return [
550 [ [], false ],
551 [ [ 'version' => 1 ], true ]
552 ];
553 }
554
555 /**
556 * @dataProvider getMultiWithUnionSetCallback_provider
557 * @covers WANObjectCache::getMultiWithUnionSetCallback()
558 * @covers WANObjectCache::makeMultiKeys()
559 * @param array $extOpts
560 * @param bool $versioned
561 */
562 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
563 $cache = $this->cache;
564
565 $keyA = wfRandomString();
566 $keyB = wfRandomString();
567 $keyC = wfRandomString();
568 $cKey1 = wfRandomString();
569 $cKey2 = wfRandomString();
570
571 $wasSet = 0;
572 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
573 &$wasSet, &$priorValue, &$priorAsOf
574 ) {
575 $newValues = [];
576 foreach ( $ids as $id ) {
577 ++$wasSet;
578 $newValues[$id] = "@$id$";
579 $ttls[$id] = 20; // override with another value
580 }
581
582 return $newValues;
583 };
584
585 $wasSet = 0;
586 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
587 $value = "@3353$";
588 $v = $cache->getMultiWithUnionSetCallback(
589 $keyedIds, 30, $genFunc, $extOpts );
590 $this->assertEquals( $value, $v[$keyA], "Value returned" );
591 $this->assertEquals( 1, $wasSet, "Value regenerated" );
592
593 $curTTL = null;
594 $cache->get( $keyA, $curTTL );
595 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
596 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
597
598 $wasSet = 0;
599 $value = "@efef$";
600 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
601 $v = $cache->getMultiWithUnionSetCallback(
602 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
603 $this->assertEquals( $value, $v[$keyB], "Value returned" );
604 $this->assertEquals( 1, $wasSet, "Value regenerated" );
605 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
606 $v = $cache->getMultiWithUnionSetCallback(
607 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
608 $this->assertEquals( $value, $v[$keyB], "Value returned" );
609 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
610 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
611
612 $priorTime = microtime( true );
613 usleep( 1 );
614 $wasSet = 0;
615 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
616 $v = $cache->getMultiWithUnionSetCallback(
617 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
618 );
619 $this->assertEquals( $value, $v[$keyB], "Value returned" );
620 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
621 $t1 = $cache->getCheckKeyTime( $cKey1 );
622 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
623 $t2 = $cache->getCheckKeyTime( $cKey2 );
624 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
625
626 $priorTime = microtime( true );
627 $value = "@43636$";
628 $wasSet = 0;
629 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
630 $v = $cache->getMultiWithUnionSetCallback(
631 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
632 );
633 $this->assertEquals( $value, $v[$keyC], "Value returned" );
634 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
635 $t1 = $cache->getCheckKeyTime( $cKey1 );
636 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
637 $t2 = $cache->getCheckKeyTime( $cKey2 );
638 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
639
640 $curTTL = null;
641 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
642 if ( $versioned ) {
643 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
644 } else {
645 $this->assertEquals( $value, $v, "Value returned" );
646 }
647 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
648
649 $wasSet = 0;
650 $key = wfRandomString();
651 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
652 $v = $cache->getMultiWithUnionSetCallback(
653 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
654 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
655 $cache->delete( $key );
656 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
657 $v = $cache->getMultiWithUnionSetCallback(
658 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
659 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
660 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
661
662 $calls = 0;
663 $ids = [ 1, 2, 3, 4, 5, 6 ];
664 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
665 return $wanCache->makeKey( 'test', $id );
666 };
667 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
668 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
669 $newValues = [];
670 foreach ( $ids as $id ) {
671 ++$calls;
672 $newValues[$id] = "val-{$id}";
673 }
674
675 return $newValues;
676 };
677 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
678
679 $this->assertEquals(
680 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
681 array_values( $values ),
682 "Correct values in correct order"
683 );
684 $this->assertEquals(
685 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
686 array_keys( $values ),
687 "Correct keys in correct order"
688 );
689 $this->assertEquals( count( $ids ), $calls );
690
691 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
692 $this->assertEquals( count( $ids ), $calls, "Values cached" );
693 }
694
695 public static function getMultiWithUnionSetCallback_provider() {
696 return [
697 [ [], false ],
698 [ [ 'version' => 1 ], true ]
699 ];
700 }
701
702 /**
703 * @covers WANObjectCache::getWithSetCallback()
704 * @covers WANObjectCache::doGetWithSetCallback()
705 */
706 public function testLockTSE() {
707 $cache = $this->cache;
708 $key = wfRandomString();
709 $value = wfRandomString();
710
711 $calls = 0;
712 $func = function () use ( &$calls, $value, $cache, $key ) {
713 ++$calls;
714 // Immediately kill any mutex rather than waiting a second
715 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
716 return $value;
717 };
718
719 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
720 $this->assertEquals( $value, $ret );
721 $this->assertEquals( 1, $calls, 'Value was populated' );
722
723 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
724 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
725
726 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
727 $ret = $cache->getWithSetCallback( $key, 30, $func,
728 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
729 $this->assertEquals( $value, $ret, 'Old value used' );
730 $this->assertEquals( 1, $calls, 'Callback was not used' );
731
732 $cache->delete( $key );
733 $ret = $cache->getWithSetCallback( $key, 30, $func,
734 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
735 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
736 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
737
738 $ret = $cache->getWithSetCallback( $key, 30, $func,
739 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
740 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
741 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
742 }
743
744 /**
745 * @covers WANObjectCache::getWithSetCallback()
746 * @covers WANObjectCache::doGetWithSetCallback()
747 * @covers WANObjectCache::set()
748 */
749 public function testLockTSESlow() {
750 $cache = $this->cache;
751 $key = wfRandomString();
752 $value = wfRandomString();
753
754 $calls = 0;
755 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
756 ++$calls;
757 $setOpts['since'] = microtime( true ) - 10;
758 // Immediately kill any mutex rather than waiting a second
759 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
760 return $value;
761 };
762
763 // Value should be marked as stale due to snapshot lag
764 $curTTL = null;
765 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
766 $this->assertEquals( $value, $ret );
767 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
768 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
769 $this->assertEquals( 1, $calls, 'Value was generated' );
770
771 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
772 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
773 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
774 $this->assertEquals( $value, $ret );
775 $this->assertEquals( 1, $calls, 'Callback was not used' );
776 }
777
778 /**
779 * @covers WANObjectCache::getWithSetCallback()
780 * @covers WANObjectCache::doGetWithSetCallback()
781 */
782 public function testBusyValue() {
783 $cache = $this->cache;
784 $key = wfRandomString();
785 $value = wfRandomString();
786 $busyValue = wfRandomString();
787
788 $calls = 0;
789 $func = function () use ( &$calls, $value, $cache, $key ) {
790 ++$calls;
791 // Immediately kill any mutex rather than waiting a second
792 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
793 return $value;
794 };
795
796 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
797 $this->assertEquals( $value, $ret );
798 $this->assertEquals( 1, $calls, 'Value was populated' );
799
800 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
801 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
802
803 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
804 $ret = $cache->getWithSetCallback( $key, 30, $func,
805 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
806 $this->assertEquals( $value, $ret, 'Callback used' );
807 $this->assertEquals( 2, $calls, 'Callback used' );
808
809 $ret = $cache->getWithSetCallback( $key, 30, $func,
810 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
811 $this->assertEquals( $value, $ret, 'Old value used' );
812 $this->assertEquals( 2, $calls, 'Callback was not used' );
813
814 $cache->delete( $key ); // no value at all anymore and still locked
815 $ret = $cache->getWithSetCallback( $key, 30, $func,
816 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
817 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
818 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
819
820 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
821 $ret = $cache->getWithSetCallback( $key, 30, $func,
822 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
823 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
824 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
825
826 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
827 $ret = $cache->getWithSetCallback( $key, 30, $func,
828 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
829 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
830 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
831 }
832
833 /**
834 * @covers WANObjectCache::getMulti()
835 */
836 public function testGetMulti() {
837 $cache = $this->cache;
838
839 $value1 = [ 'this' => 'is', 'a' => 'test' ];
840 $value2 = [ 'this' => 'is', 'another' => 'test' ];
841
842 $key1 = wfRandomString();
843 $key2 = wfRandomString();
844 $key3 = wfRandomString();
845
846 $cache->set( $key1, $value1, 5 );
847 $cache->set( $key2, $value2, 10 );
848
849 $curTTLs = [];
850 $this->assertEquals(
851 [ $key1 => $value1, $key2 => $value2 ],
852 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
853 'Result array populated'
854 );
855
856 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
857 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
858 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
859
860 $cKey1 = wfRandomString();
861 $cKey2 = wfRandomString();
862
863 $priorTime = microtime( true );
864 usleep( 1 );
865 $curTTLs = [];
866 $this->assertEquals(
867 [ $key1 => $value1, $key2 => $value2 ],
868 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
869 "Result array populated even with new check keys"
870 );
871 $t1 = $cache->getCheckKeyTime( $cKey1 );
872 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
873 $t2 = $cache->getCheckKeyTime( $cKey2 );
874 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
875 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
876 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
877 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
878
879 usleep( 1 );
880 $curTTLs = [];
881 $this->assertEquals(
882 [ $key1 => $value1, $key2 => $value2 ],
883 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
884 "Result array still populated even with new check keys"
885 );
886 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
887 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
888 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
889 }
890
891 /**
892 * @covers WANObjectCache::getMulti()
893 * @covers WANObjectCache::processCheckKeys()
894 */
895 public function testGetMultiCheckKeys() {
896 $cache = $this->cache;
897
898 $checkAll = wfRandomString();
899 $check1 = wfRandomString();
900 $check2 = wfRandomString();
901 $check3 = wfRandomString();
902 $value1 = wfRandomString();
903 $value2 = wfRandomString();
904
905 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
906 // several seconds during the test to assert the behaviour.
907 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
908 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
909 }
910 usleep( 100 );
911
912 $cache->set( 'key1', $value1, 10 );
913 $cache->set( 'key2', $value2, 10 );
914
915 $curTTLs = [];
916 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
917 'key1' => $check1,
918 $checkAll,
919 'key2' => $check2,
920 'key3' => $check3,
921 ] );
922 $this->assertEquals(
923 [ 'key1' => $value1, 'key2' => $value2 ],
924 $result,
925 'Initial values'
926 );
927 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
928 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
929 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
930 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
931
932 $cache->touchCheckKey( $check1 );
933
934 $curTTLs = [];
935 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
936 'key1' => $check1,
937 $checkAll,
938 'key2' => $check2,
939 'key3' => $check3,
940 ] );
941 $this->assertEquals(
942 [ 'key1' => $value1, 'key2' => $value2 ],
943 $result,
944 'key1 expired by check1, but value still provided'
945 );
946 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
947 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
948
949 $cache->touchCheckKey( $checkAll );
950
951 $curTTLs = [];
952 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
953 'key1' => $check1,
954 $checkAll,
955 'key2' => $check2,
956 'key3' => $check3,
957 ] );
958 $this->assertEquals(
959 [ 'key1' => $value1, 'key2' => $value2 ],
960 $result,
961 'All keys expired by checkAll, but value still provided'
962 );
963 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
964 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
965 }
966
967 /**
968 * @covers WANObjectCache::get()
969 * @covers WANObjectCache::processCheckKeys()
970 */
971 public function testCheckKeyInitHoldoff() {
972 $cache = $this->cache;
973
974 for ( $i = 0; $i < 500; ++$i ) {
975 $key = wfRandomString();
976 $checkKey = wfRandomString();
977 // miss, set, hit
978 $cache->get( $key, $curTTL, [ $checkKey ] );
979 $cache->set( $key, 'val', 10 );
980 $curTTL = null;
981 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
982
983 $this->assertEquals( 'val', $v );
984 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
985 }
986
987 for ( $i = 0; $i < 500; ++$i ) {
988 $key = wfRandomString();
989 $checkKey = wfRandomString();
990 // set, hit
991 $cache->set( $key, 'val', 10 );
992 $curTTL = null;
993 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
994
995 $this->assertEquals( 'val', $v );
996 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
997 }
998 }
999
1000 /**
1001 * @covers WANObjectCache::delete
1002 * @covers WANObjectCache::relayDelete
1003 * @covers WANObjectCache::relayPurge
1004 */
1005 public function testDelete() {
1006 $key = wfRandomString();
1007 $value = wfRandomString();
1008 $this->cache->set( $key, $value );
1009
1010 $curTTL = null;
1011 $v = $this->cache->get( $key, $curTTL );
1012 $this->assertEquals( $value, $v, "Key was created with value" );
1013 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1014
1015 $this->cache->delete( $key );
1016
1017 $curTTL = null;
1018 $v = $this->cache->get( $key, $curTTL );
1019 $this->assertFalse( $v, "Deleted key has false value" );
1020 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1021
1022 $this->cache->set( $key, $value . 'more' );
1023 $v = $this->cache->get( $key, $curTTL );
1024 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1025 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1026
1027 $this->cache->set( $key, $value );
1028 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1029
1030 $curTTL = null;
1031 $v = $this->cache->get( $key, $curTTL );
1032 $this->assertFalse( $v, "Deleted key has false value" );
1033 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1034
1035 $this->cache->set( $key, $value );
1036 $v = $this->cache->get( $key, $curTTL );
1037 $this->assertEquals( $value, $v, "Key was created with value" );
1038 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1039 }
1040
1041 /**
1042 * @dataProvider getWithSetCallback_versions_provider
1043 * @covers WANObjectCache::getWithSetCallback()
1044 * @covers WANObjectCache::doGetWithSetCallback()
1045 * @param array $extOpts
1046 * @param bool $versioned
1047 */
1048 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1049 $cache = $this->cache;
1050
1051 $key = wfRandomString();
1052 $value = wfRandomString();
1053
1054 $wasSet = 0;
1055 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
1056 ++$wasSet;
1057 return $value;
1058 };
1059
1060 // Set the main key (version N if versioned)
1061 $wasSet = 0;
1062 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1063 $this->assertEquals( $value, $v, "Value returned" );
1064 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1065 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1066 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1067 // Set the key for version N+1 (if versioned)
1068 if ( $versioned ) {
1069 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1070
1071 $wasSet = 0;
1072 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
1073 $this->assertEquals( $value, $v, "Value returned" );
1074 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1075
1076 $wasSet = 0;
1077 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
1078 $this->assertEquals( $value, $v, "Value returned" );
1079 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1080 }
1081
1082 $wasSet = 0;
1083 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1084 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1085
1086 $wasSet = 0;
1087 $cache->delete( $key );
1088 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1089 $this->assertEquals( $value, $v, "Value returned" );
1090 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1091
1092 if ( $versioned ) {
1093 $wasSet = 0;
1094 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1095 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
1096 $this->assertEquals( $value, $v, "Value returned" );
1097 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1098 }
1099 }
1100
1101 public static function getWithSetCallback_versions_provider() {
1102 return [
1103 [ [], false ],
1104 [ [ 'version' => 1 ], true ]
1105 ];
1106 }
1107
1108 /**
1109 * @covers WANObjectCache::touchCheckKey
1110 * @covers WANObjectCache::resetCheckKey
1111 * @covers WANObjectCache::getCheckKeyTime
1112 * @covers WANObjectCache::makePurgeValue
1113 * @covers WANObjectCache::parsePurgeValue
1114 */
1115 public function testTouchKeys() {
1116 $key = wfRandomString();
1117
1118 $priorTime = microtime( true );
1119 usleep( 100 );
1120 $t0 = $this->cache->getCheckKeyTime( $key );
1121 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1122
1123 $priorTime = microtime( true );
1124 usleep( 100 );
1125 $this->cache->touchCheckKey( $key );
1126 $t1 = $this->cache->getCheckKeyTime( $key );
1127 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1128
1129 $t2 = $this->cache->getCheckKeyTime( $key );
1130 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1131
1132 usleep( 100 );
1133 $this->cache->touchCheckKey( $key );
1134 $t3 = $this->cache->getCheckKeyTime( $key );
1135 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1136
1137 $t4 = $this->cache->getCheckKeyTime( $key );
1138 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1139
1140 usleep( 100 );
1141 $this->cache->resetCheckKey( $key );
1142 $t5 = $this->cache->getCheckKeyTime( $key );
1143 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1144
1145 $t6 = $this->cache->getCheckKeyTime( $key );
1146 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1147 }
1148
1149 /**
1150 * @covers WANObjectCache::getMulti()
1151 */
1152 public function testGetWithSeveralCheckKeys() {
1153 $key = wfRandomString();
1154 $tKey1 = wfRandomString();
1155 $tKey2 = wfRandomString();
1156 $value = 'meow';
1157
1158 // Two check keys are newer (given hold-off) than $key, another is older
1159 $this->internalCache->set(
1160 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1161 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
1162 );
1163 $this->internalCache->set(
1164 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1165 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
1166 );
1167 $this->internalCache->set(
1168 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1169 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
1170 );
1171 $this->cache->set( $key, $value, 30 );
1172
1173 $curTTL = null;
1174 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1175 $this->assertEquals( $value, $v, "Value matches" );
1176 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1177 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1178 }
1179
1180 /**
1181 * @covers WANObjectCache::reap()
1182 * @covers WANObjectCache::reapCheckKey()
1183 */
1184 public function testReap() {
1185 $vKey1 = wfRandomString();
1186 $vKey2 = wfRandomString();
1187 $tKey1 = wfRandomString();
1188 $tKey2 = wfRandomString();
1189 $value = 'moo';
1190
1191 $knownPurge = time() - 60;
1192 $goodTime = microtime( true ) - 5;
1193 $badTime = microtime( true ) - 300;
1194
1195 $this->internalCache->set(
1196 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
1197 [
1198 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1199 WANObjectCache::FLD_VALUE => $value,
1200 WANObjectCache::FLD_TTL => 3600,
1201 WANObjectCache::FLD_TIME => $goodTime
1202 ]
1203 );
1204 $this->internalCache->set(
1205 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
1206 [
1207 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1208 WANObjectCache::FLD_VALUE => $value,
1209 WANObjectCache::FLD_TTL => 3600,
1210 WANObjectCache::FLD_TIME => $badTime
1211 ]
1212 );
1213 $this->internalCache->set(
1214 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1215 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
1216 );
1217 $this->internalCache->set(
1218 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1219 WANObjectCache::PURGE_VAL_PREFIX . $badTime
1220 );
1221
1222 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1223 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1224 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1225 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1226
1227 $this->assertFalse( $bad1 );
1228 $this->assertTrue( $bad2 );
1229
1230 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1231 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1232 $this->assertFalse( $tBad1 );
1233 $this->assertTrue( $tBad2 );
1234 }
1235
1236 /**
1237 * @covers WANObjectCache::reap()
1238 */
1239 public function testReap_fail() {
1240 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1241 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1242 $backend->expects( $this->once() )->method( 'get' )
1243 ->willReturn( [
1244 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1245 WANObjectCache::FLD_VALUE => 'value',
1246 WANObjectCache::FLD_TTL => 3600,
1247 WANObjectCache::FLD_TIME => 300,
1248 ] );
1249 $backend->expects( $this->once() )->method( 'changeTTL' )
1250 ->willReturn( false );
1251
1252 $wanCache = new WANObjectCache( [
1253 'cache' => $backend,
1254 'pool' => 'testcache-hash',
1255 'relayer' => new EventRelayerNull( [] )
1256 ] );
1257
1258 $isStale = null;
1259 $ret = $wanCache->reap( 'key', 360, $isStale );
1260 $this->assertTrue( $isStale, 'value was stale' );
1261 $this->assertFalse( $ret, 'changeTTL failed' );
1262 }
1263
1264 /**
1265 * @covers WANObjectCache::set()
1266 */
1267 public function testSetWithLag() {
1268 $value = 1;
1269
1270 $key = wfRandomString();
1271 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1272 $this->cache->set( $key, $value, 30, $opts );
1273 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1274
1275 $key = wfRandomString();
1276 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1277 $this->cache->set( $key, $value, 30, $opts );
1278 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1279
1280 $key = wfRandomString();
1281 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1282 $this->cache->set( $key, $value, 30, $opts );
1283 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1284 }
1285
1286 /**
1287 * @covers WANObjectCache::set()
1288 */
1289 public function testWritePending() {
1290 $value = 1;
1291
1292 $key = wfRandomString();
1293 $opts = [ 'pending' => true ];
1294 $this->cache->set( $key, $value, 30, $opts );
1295 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1296 }
1297
1298 public function testMcRouterSupport() {
1299 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1300 ->setMethods( [ 'set', 'delete' ] )->getMock();
1301 $localBag->expects( $this->never() )->method( 'set' );
1302 $localBag->expects( $this->never() )->method( 'delete' );
1303 $wanCache = new WANObjectCache( [
1304 'cache' => $localBag,
1305 'pool' => 'testcache-hash',
1306 'relayer' => new EventRelayerNull( [] )
1307 ] );
1308 $valFunc = function () {
1309 return 1;
1310 };
1311
1312 // None of these should use broadcasting commands (e.g. SET, DELETE)
1313 $wanCache->get( 'x' );
1314 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1315 $wanCache->getMulti( [ 'x', 'y' ] );
1316 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1317 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1318 $wanCache->getCheckKeyTime( 'zzz' );
1319 $wanCache->reap( 'x', time() - 300 );
1320 $wanCache->reap( 'zzz', time() - 300 );
1321 }
1322
1323 /**
1324 * @dataProvider provideAdaptiveTTL
1325 * @covers WANObjectCache::adaptiveTTL()
1326 * @param float|int $ago
1327 * @param int $maxTTL
1328 * @param int $minTTL
1329 * @param float $factor
1330 * @param int $adaptiveTTL
1331 */
1332 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1333 $mtime = $ago ? time() - $ago : $ago;
1334 $margin = 5;
1335 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1336
1337 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1338 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1339
1340 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1341
1342 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1343 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1344 }
1345
1346 public static function provideAdaptiveTTL() {
1347 return [
1348 [ 3600, 900, 30, 0.2, 720 ],
1349 [ 3600, 500, 30, 0.2, 500 ],
1350 [ 3600, 86400, 800, 0.2, 800 ],
1351 [ false, 86400, 800, 0.2, 800 ],
1352 [ null, 86400, 800, 0.2, 800 ]
1353 ];
1354 }
1355
1356 /**
1357 * @covers WANObjectCache::__construct
1358 * @covers WANObjectCache::newEmpty
1359 */
1360 public function testNewEmpty() {
1361 $this->assertInstanceOf(
1362 WANObjectCache::class,
1363 WANObjectCache::newEmpty()
1364 );
1365 }
1366
1367 /**
1368 * @covers WANObjectCache::setLogger
1369 */
1370 public function testSetLogger() {
1371 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1372 }
1373
1374 /**
1375 * @covers WANObjectCache::getQoS
1376 */
1377 public function testGetQoS() {
1378 $backend = $this->getMockBuilder( HashBagOStuff::class )
1379 ->setMethods( [ 'getQoS' ] )->getMock();
1380 $backend->expects( $this->once() )->method( 'getQoS' )
1381 ->willReturn( BagOStuff::QOS_UNKNOWN );
1382 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1383
1384 $this->assertSame(
1385 $wanCache::QOS_UNKNOWN,
1386 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1387 );
1388 }
1389
1390 /**
1391 * @covers WANObjectCache::makeKey
1392 */
1393 public function testMakeKey() {
1394 $backend = $this->getMockBuilder( HashBagOStuff::class )
1395 ->setMethods( [ 'makeKey' ] )->getMock();
1396 $backend->expects( $this->once() )->method( 'makeKey' )
1397 ->willReturn( 'special' );
1398
1399 $wanCache = new WANObjectCache( [
1400 'cache' => $backend,
1401 'pool' => 'testcache-hash',
1402 'relayer' => new EventRelayerNull( [] )
1403 ] );
1404
1405 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1406 }
1407
1408 /**
1409 * @covers WANObjectCache::makeGlobalKey
1410 */
1411 public function testMakeGlobalKey() {
1412 $backend = $this->getMockBuilder( HashBagOStuff::class )
1413 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1414 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1415 ->willReturn( 'special' );
1416
1417 $wanCache = new WANObjectCache( [
1418 'cache' => $backend,
1419 'pool' => 'testcache-hash',
1420 'relayer' => new EventRelayerNull( [] )
1421 ] );
1422
1423 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1424 }
1425
1426 public static function statsKeyProvider() {
1427 return [
1428 [ 'domain:page:5', 'page' ],
1429 [ 'domain:main-key', 'main-key' ],
1430 [ 'domain:page:history', 'page' ],
1431 [ 'missingdomainkey', 'missingdomainkey' ]
1432 ];
1433 }
1434
1435 /**
1436 * @dataProvider statsKeyProvider
1437 * @covers WANObjectCache::determineKeyClass
1438 */
1439 public function testStatsKeyClass( $key, $class ) {
1440 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1441 'cache' => new HashBagOStuff,
1442 'pool' => 'testcache-hash',
1443 'relayer' => new EventRelayerNull( [] )
1444 ] ) );
1445
1446 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1447 }
1448 }
1449
1450 class TimeAdjustableHashBagOStuff extends HashBagOStuff {
1451 private $timeOverride = 0;
1452
1453 public function setTime( $time ) {
1454 $this->timeOverride = $time;
1455 }
1456
1457 protected function getCurrentTime() {
1458 return $this->timeOverride ?: parent::getCurrentTime();
1459 }
1460 }
1461
1462 class TimeAdjustableWANObjectCache extends WANObjectCache {
1463 private $timeOverride = 0;
1464
1465 public function setTime( $time ) {
1466 $this->timeOverride = $time;
1467 if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1468 $this->cache->setTime( $time );
1469 }
1470 }
1471
1472 protected function getCurrentTime() {
1473 return $this->timeOverride ?: parent::getCurrentTime();
1474 }
1475 }
1476
1477 class NearExpiringWANObjectCache extends TimeAdjustableWANObjectCache {
1478 const CLOCK_SKEW = 1;
1479
1480 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1481 return ( ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1482 }
1483 }
1484
1485 class PopularityRefreshingWANObjectCache extends TimeAdjustableWANObjectCache {
1486 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1487 return ( ( $now - $asOf ) > $timeTillRefresh );
1488 }
1489 }