9e4a5d7d6271b25f465d18bf28b904a18cea91d8
[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::getNonProcessCachedMultiKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
18 */
19 class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
20
21 use MediaWikiCoversValidator;
22 use PHPUnit4And6Compat;
23
24 /** @var WANObjectCache */
25 private $cache;
26 /** @var BagOStuff */
27 private $internalCache;
28
29 protected function setUp() {
30 parent::setUp();
31
32 $this->cache = new WANObjectCache( [
33 'cache' => new HashBagOStuff()
34 ] );
35
36 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
37 /** @noinspection PhpUndefinedFieldInspection */
38 $this->internalCache = $wanCache->cache;
39 }
40
41 /**
42 * @dataProvider provideSetAndGet
43 * @covers WANObjectCache::set()
44 * @covers WANObjectCache::get()
45 * @covers WANObjectCache::makeKey()
46 * @param mixed $value
47 * @param int $ttl
48 */
49 public function testSetAndGet( $value, $ttl ) {
50 $cache = $this->cache;
51
52 $curTTL = null;
53 $asOf = null;
54 $key = $cache->makeKey( 'x', wfRandomString() );
55
56 $cache->get( $key, $curTTL, [], $asOf );
57 $this->assertNull( $curTTL, "Current TTL is null" );
58 $this->assertNull( $asOf, "Current as-of-time is infinite" );
59
60 $t = microtime( true );
61
62 $cache->set( $key, $value, $cache::TTL_UNCACHEABLE );
63 $cache->get( $key, $curTTL, [], $asOf );
64 $this->assertNull( $curTTL, "Current TTL is null (TTL_UNCACHEABLE)" );
65 $this->assertNull( $asOf, "Current as-of-time is infinite (TTL_UNCACHEABLE)" );
66
67 $cache->set( $key, $value, $ttl );
68
69 $this->assertEquals( $value, $cache->get( $key, $curTTL, [], $asOf ) );
70 if ( is_infinite( $ttl ) || $ttl == 0 ) {
71 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
72 } else {
73 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
74 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
75 }
76 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
77 $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
78 }
79
80 public static function provideSetAndGet() {
81 return [
82 [ 14141, 3 ],
83 [ 3535.666, 3 ],
84 [ [], 3 ],
85 [ null, 3 ],
86 [ '0', 3 ],
87 [ (object)[ 'meow' ], 3 ],
88 [ INF, 3 ],
89 [ '', 3 ],
90 [ 'pizzacat', INF ],
91 ];
92 }
93
94 /**
95 * @covers WANObjectCache::get()
96 * @covers WANObjectCache::makeGlobalKey()
97 */
98 public function testGetNotExists() {
99 $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
100 $curTTL = null;
101 $value = $this->cache->get( $key, $curTTL );
102
103 $this->assertFalse( $value, "Non-existing key has false value" );
104 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
105 }
106
107 /**
108 * @covers WANObjectCache::set()
109 */
110 public function testSetOver() {
111 $key = wfRandomString();
112 for ( $i = 0; $i < 3; ++$i ) {
113 $value = wfRandomString();
114 $this->cache->set( $key, $value, 3 );
115
116 $this->assertEquals( $this->cache->get( $key ), $value );
117 }
118 }
119
120 /**
121 * @covers WANObjectCache::set()
122 */
123 public function testStaleSet() {
124 $key = wfRandomString();
125 $value = wfRandomString();
126 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
127
128 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
129 }
130
131 /**
132 * @covers WANObjectCache::getWithSetCallback
133 */
134 public function testProcessCacheLruAndDelete() {
135 $cache = $this->cache;
136 $mockWallClock = 1549343530.2053;
137 $cache->setMockTime( $mockWallClock );
138
139 $hit = 0;
140 $fn = function () use ( &$hit ) {
141 ++$hit;
142 return 42;
143 };
144 $keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
145 $keysB = [ wfRandomString(), wfRandomString(), wfRandomString() ];
146 $pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
147
148 foreach ( $keysA as $i => $key ) {
149 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
150 }
151 $this->assertEquals( 3, $hit, "Values not cached yet" );
152
153 foreach ( $keysA as $i => $key ) {
154 // Should not evict from process cache
155 $cache->delete( $key );
156 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
157 }
158 $this->assertEquals( 3, $hit, "Values cached; not cleared by delete()" );
159
160 foreach ( $keysB as $i => $key ) {
161 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
162 }
163 $this->assertEquals( 6, $hit, "New values not cached yet" );
164
165 foreach ( $keysB as $i => $key ) {
166 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
167 }
168 $this->assertEquals( 6, $hit, "New values cached" );
169
170 foreach ( $keysA as $i => $key ) {
171 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
172 }
173 $this->assertEquals( 9, $hit, "Prior values evicted by new values" );
174 }
175
176 /**
177 * @covers WANObjectCache::getWithSetCallback
178 */
179 public function testProcessCacheInterimKeys() {
180 $cache = $this->cache;
181 $mockWallClock = 1549343530.2053;
182 $cache->setMockTime( $mockWallClock );
183
184 $hit = 0;
185 $fn = function () use ( &$hit ) {
186 ++$hit;
187 return 42;
188 };
189 $keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
190 $pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
191
192 foreach ( $keysA as $i => $key ) {
193 $cache->delete( $key ); // tombstone key
194 $mockWallClock += 0.001; // cached values will be newer than tombstone
195 // Get into process cache (specific group) and interim cache
196 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
197 }
198 $this->assertEquals( 3, $hit );
199
200 // Get into process cache (default group)
201 $key = reset( $keysA );
202 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
203 $this->assertEquals( 3, $hit, "Value recently interim-cached" );
204
205 $mockWallClock += 0.2; // interim key not brand new
206 $cache->clearProcessCache();
207 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
208 $this->assertEquals( 4, $hit, "Value calculated (interim key not recent and reset)" );
209 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
210 $this->assertEquals( 4, $hit, "Value process cached" );
211 }
212
213 /**
214 * @covers WANObjectCache::getWithSetCallback
215 */
216 public function testProcessCacheNesting() {
217 $cache = $this->cache;
218 $mockWallClock = 1549343530.2053;
219 $cache->setMockTime( $mockWallClock );
220
221 $keyOuter = "outer-" . wfRandomString();
222 $keyInner = "inner-" . wfRandomString();
223
224 $innerHit = 0;
225 $innerFn = function () use ( &$innerHit ) {
226 ++$innerHit;
227 return 42;
228 };
229
230 $outerHit = 0;
231 $outerFn = function () use ( $keyInner, $innerFn, $cache, &$outerHit ) {
232 ++$outerHit;
233 $v = $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
234
235 return 43 + $v;
236 };
237
238 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
239 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
240
241 $this->assertEquals( 1, $innerHit, "Inner callback value cached" );
242 $cache->delete( $keyInner, $cache::HOLDOFF_NONE );
243 $mockWallClock += 1;
244
245 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
246 $this->assertEquals( 1, $innerHit, "Inner callback process cached" );
247
248 // Outer key misses and inner key process cache value is refused
249 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
250
251 $this->assertEquals( 1, $outerHit, "Outer callback value not yet cached" );
252 $this->assertEquals( 2, $innerHit, "Inner callback value process cache skipped" );
253
254 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
255
256 $this->assertEquals( 1, $outerHit, "Outer callback value cached" );
257
258 $cache->delete( $keyInner, $cache::HOLDOFF_NONE );
259 $cache->delete( $keyOuter, $cache::HOLDOFF_NONE );
260 $mockWallClock += 1;
261 $cache->clearProcessCache();
262 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
263
264 $this->assertEquals( 2, $outerHit, "Outer callback value not yet cached" );
265 $this->assertEquals( 3, $innerHit, "Inner callback value not yet cached" );
266
267 $cache->delete( $keyInner, $cache::HOLDOFF_NONE );
268 $mockWallClock += 1;
269 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
270
271 $this->assertEquals( 3, $innerHit, "Inner callback value process cached" );
272 }
273
274 /**
275 * @dataProvider getWithSetCallback_provider
276 * @covers WANObjectCache::getWithSetCallback()
277 * @covers WANObjectCache::fetchOrRegenerate()
278 * @param array $extOpts
279 * @param bool $versioned
280 */
281 public function testGetWithSetCallback( array $extOpts, $versioned ) {
282 $cache = $this->cache;
283
284 $key = wfRandomString();
285 $value = wfRandomString();
286 $cKey1 = wfRandomString();
287 $cKey2 = wfRandomString();
288
289 $priorValue = null;
290 $priorAsOf = null;
291 $wasSet = 0;
292 $func = function ( $old, &$ttl, &$opts, $asOf )
293 use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) {
294 ++$wasSet;
295 $priorValue = $old;
296 $priorAsOf = $asOf;
297 $ttl = 20; // override with another value
298 return $value;
299 };
300
301 $mockWallClock = 1549343530.2053;
302 $priorTime = $mockWallClock; // reference time
303 $cache->setMockTime( $mockWallClock );
304
305 $wasSet = 0;
306 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
307 $this->assertEquals( $value, $v, "Value returned" );
308 $this->assertEquals( 1, $wasSet, "Value regenerated" );
309 $this->assertFalse( $priorValue, "No prior value" );
310 $this->assertNull( $priorAsOf, "No prior value" );
311
312 $curTTL = null;
313 $cache->get( $key, $curTTL );
314 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
315 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
316
317 $wasSet = 0;
318 $v = $cache->getWithSetCallback(
319 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
320 $this->assertEquals( $value, $v, "Value returned" );
321 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
322
323 $mockWallClock += 1;
324
325 $wasSet = 0;
326 $v = $cache->getWithSetCallback(
327 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
328 );
329 $this->assertEquals( $value, $v, "Value returned" );
330 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
331 $this->assertEquals( $value, $priorValue, "Has prior value" );
332 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
333 $t1 = $cache->getCheckKeyTime( $cKey1 );
334 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
335 $t2 = $cache->getCheckKeyTime( $cKey2 );
336 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
337
338 $mockWallClock += 0.2; // interim key is not brand new and check keys have past values
339 $priorTime = $mockWallClock; // reference time
340 $wasSet = 0;
341 $v = $cache->getWithSetCallback(
342 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
343 );
344 $this->assertEquals( $value, $v, "Value returned" );
345 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
346 $t1 = $cache->getCheckKeyTime( $cKey1 );
347 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
348 $t2 = $cache->getCheckKeyTime( $cKey2 );
349 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
350
351 $curTTL = null;
352 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
353 $this->assertEquals( $value, $v, "Value returned" );
354 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
355
356 $wasSet = 0;
357 $key = wfRandomString();
358 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
359 $this->assertEquals( $value, $v, "Value returned" );
360 $cache->delete( $key );
361 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
362 $this->assertEquals( $value, $v, "Value still returned after deleted" );
363 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
364
365 $oldValReceived = -1;
366 $oldAsOfReceived = -1;
367 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
368 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
369 ++$wasSet;
370 $oldValReceived = $oldVal;
371 $oldAsOfReceived = $oldAsOf;
372
373 return 'xxx' . $wasSet;
374 };
375
376 $mockWallClock = 1549343530.2053;
377 $priorTime = $mockWallClock; // reference time
378
379 $wasSet = 0;
380 $key = wfRandomString();
381 $v = $cache->getWithSetCallback(
382 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
383 $this->assertEquals( 'xxx1', $v, "Value returned" );
384 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
385 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
386
387 $mockWallClock += 40;
388 $v = $cache->getWithSetCallback(
389 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
390 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
391 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
392 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
393 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
394
395 $mockWallClock += 260;
396 $v = $cache->getWithSetCallback(
397 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
398 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
399 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
400 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
401 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
402
403 $mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL - 1 );
404 $wasSet = 0;
405 $key = wfRandomString();
406 $checkKey = $cache->makeKey( 'template', 'X' );
407 $cache->touchCheckKey( $checkKey ); // init check key
408 $mockWallClock = $priorTime;
409 $v = $cache->getWithSetCallback(
410 $key,
411 $cache::TTL_INDEFINITE,
412 $checkFunc,
413 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
414 );
415 $this->assertEquals( 'xxx1', $v, "Value returned" );
416 $this->assertEquals( 1, $wasSet, "Value computed" );
417 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
418 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
419
420 $mockWallClock += $cache::TTL_HOUR; // some time passes
421 $v = $cache->getWithSetCallback(
422 $key,
423 $cache::TTL_INDEFINITE,
424 $checkFunc,
425 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
426 );
427 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
428 $this->assertEquals( 1, $wasSet, "Cached value returned" );
429
430 $cache->touchCheckKey( $checkKey ); // make key stale
431 $mockWallClock += 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
432
433 $v = $cache->getWithSetCallback(
434 $key,
435 $cache::TTL_INDEFINITE,
436 $checkFunc,
437 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
438 );
439 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
440 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
441
442 // Chance of refresh increase to unity as staleness approaches graceTTL
443 $mockWallClock += $cache::TTL_WEEK; // 8 days of being stale
444 $v = $cache->getWithSetCallback(
445 $key,
446 $cache::TTL_INDEFINITE,
447 $checkFunc,
448 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
449 );
450 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
451 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
452 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
453 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
454 }
455
456 /**
457 * @dataProvider getWithSetCallback_provider
458 * @covers WANObjectCache::getWithSetCallback()
459 * @covers WANObjectCache::fetchOrRegenerate()
460 * @param array $extOpts
461 * @param bool $versioned
462 */
463 function testGetWithSetcallback_touched( array $extOpts, $versioned ) {
464 $cache = $this->cache;
465
466 $mockWallClock = 1549343530.2053;
467 $cache->setMockTime( $mockWallClock );
468
469 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
470 use ( &$wasSet ) {
471 ++$wasSet;
472
473 return 'xxx' . $wasSet;
474 };
475
476 $key = wfRandomString();
477 $wasSet = 0;
478 $touched = null;
479 $touchedCallback = function () use ( &$touched ) {
480 return $touched;
481 };
482 $v = $cache->getWithSetCallback(
483 $key,
484 $cache::TTL_INDEFINITE,
485 $checkFunc,
486 [ 'touchedCallback' => $touchedCallback ] + $extOpts
487 );
488 $mockWallClock += 60;
489 $v = $cache->getWithSetCallback(
490 $key,
491 $cache::TTL_INDEFINITE,
492 $checkFunc,
493 [ 'touchedCallback' => $touchedCallback ] + $extOpts
494 );
495 $this->assertEquals( 'xxx1', $v, "Value was computed once" );
496 $this->assertEquals( 1, $wasSet, "Value was computed once" );
497
498 $touched = $mockWallClock - 10;
499 $v = $cache->getWithSetCallback(
500 $key,
501 $cache::TTL_INDEFINITE,
502 $checkFunc,
503 [ 'touchedCallback' => $touchedCallback ] + $extOpts
504 );
505 $v = $cache->getWithSetCallback(
506 $key,
507 $cache::TTL_INDEFINITE,
508 $checkFunc,
509 [ 'touchedCallback' => $touchedCallback ] + $extOpts
510 );
511 $this->assertEquals( 'xxx2', $v, "Value was recomputed once" );
512 $this->assertEquals( 2, $wasSet, "Value was recomputed once" );
513 }
514
515 public static function getWithSetCallback_provider() {
516 return [
517 [ [], false ],
518 [ [ 'version' => 1 ], true ]
519 ];
520 }
521
522 public function testPreemtiveRefresh() {
523 $value = 'KatCafe';
524 $wasSet = 0;
525 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
526 {
527 ++$wasSet;
528 return $value;
529 };
530
531 $cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
532 $mockWallClock = 1549343530.2053;
533 $cache->setMockTime( $mockWallClock );
534
535 $wasSet = 0;
536 $key = wfRandomString();
537 $opts = [ 'lowTTL' => 30 ];
538 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
539 $this->assertEquals( $value, $v, "Value returned" );
540 $this->assertEquals( 1, $wasSet, "Value calculated" );
541
542 $mockWallClock += 0.2; // interim key is not brand new
543 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
544 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
545
546 $wasSet = 0;
547 $key = wfRandomString();
548 $opts = [ 'lowTTL' => 1 ];
549 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
550 $this->assertEquals( $value, $v, "Value returned" );
551 $this->assertEquals( 1, $wasSet, "Value calculated" );
552 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
553 $this->assertEquals( 1, $wasSet, "Value cached" );
554
555 $asycList = [];
556 $asyncHandler = function ( $callback ) use ( &$asycList ) {
557 $asycList[] = $callback;
558 };
559 $cache = new NearExpiringWANObjectCache( [
560 'cache' => new HashBagOStuff(),
561 'asyncHandler' => $asyncHandler
562 ] );
563
564 $mockWallClock = 1549343530.2053;
565 $priorTime = $mockWallClock; // reference time
566 $cache->setMockTime( $mockWallClock );
567
568 $wasSet = 0;
569 $key = wfRandomString();
570 $opts = [ 'lowTTL' => 100 ];
571 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
572 $this->assertEquals( $value, $v, "Value returned" );
573 $this->assertEquals( 1, $wasSet, "Value calculated" );
574 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
575 $this->assertEquals( 1, $wasSet, "Cached value used" );
576 $this->assertEquals( $v, $value, "Value cached" );
577
578 $mockWallClock += 250;
579 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
580 $this->assertEquals( $value, $v, "Value returned" );
581 $this->assertEquals( 1, $wasSet, "Stale value used" );
582 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
583 $value = 'NewCatsInTown'; // change callback return value
584 $asycList[0](); // run the refresh callback
585 $asycList = [];
586 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
587 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
588 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
589 $this->assertEquals( $value, $v, "New value stored" );
590
591 $cache = new PopularityRefreshingWANObjectCache( [
592 'cache' => new HashBagOStuff()
593 ] );
594
595 $mockWallClock = $priorTime;
596 $cache->setMockTime( $mockWallClock );
597
598 $wasSet = 0;
599 $key = wfRandomString();
600 $opts = [ 'hotTTR' => 900 ];
601 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
602 $this->assertEquals( $value, $v, "Value returned" );
603 $this->assertEquals( 1, $wasSet, "Value calculated" );
604
605 $mockWallClock += 30;
606
607 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
608 $this->assertEquals( 1, $wasSet, "Value cached" );
609
610 $mockWallClock = $priorTime;
611 $wasSet = 0;
612 $key = wfRandomString();
613 $opts = [ 'hotTTR' => 10 ];
614 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
615 $this->assertEquals( $value, $v, "Value returned" );
616 $this->assertEquals( 1, $wasSet, "Value calculated" );
617
618 $mockWallClock += 30;
619
620 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
621 $this->assertEquals( $value, $v, "Value returned" );
622 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
623 }
624
625 /**
626 * @dataProvider getMultiWithSetCallback_provider
627 * @covers WANObjectCache::getMultiWithSetCallback
628 * @covers WANObjectCache::makeMultiKeys
629 * @covers WANObjectCache::getMulti
630 * @param array $extOpts
631 * @param bool $versioned
632 */
633 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
634 $cache = $this->cache;
635
636 $keyA = wfRandomString();
637 $keyB = wfRandomString();
638 $keyC = wfRandomString();
639 $cKey1 = wfRandomString();
640 $cKey2 = wfRandomString();
641
642 $priorValue = null;
643 $priorAsOf = null;
644 $wasSet = 0;
645 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
646 &$wasSet, &$priorValue, &$priorAsOf
647 ) {
648 ++$wasSet;
649 $priorValue = $old;
650 $priorAsOf = $asOf;
651 $ttl = 20; // override with another value
652 return "@$id$";
653 };
654
655 $mockWallClock = 1549343530.2053;
656 $priorTime = $mockWallClock; // reference time
657 $cache->setMockTime( $mockWallClock );
658
659 $wasSet = 0;
660 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
661 $value = "@3353$";
662 $v = $cache->getMultiWithSetCallback(
663 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
664 $this->assertEquals( $value, $v[$keyA], "Value returned" );
665 $this->assertEquals( 1, $wasSet, "Value regenerated" );
666 $this->assertFalse( $priorValue, "No prior value" );
667 $this->assertNull( $priorAsOf, "No prior value" );
668
669 $curTTL = null;
670 $cache->get( $keyA, $curTTL );
671 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
672 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
673
674 $wasSet = 0;
675 $value = "@efef$";
676 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
677 $v = $cache->getMultiWithSetCallback(
678 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
679 $this->assertEquals( $value, $v[$keyB], "Value returned" );
680 $this->assertEquals( 1, $wasSet, "Value regenerated" );
681 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
682
683 $v = $cache->getMultiWithSetCallback(
684 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
685 $this->assertEquals( $value, $v[$keyB], "Value returned" );
686 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
687 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
688
689 $mockWallClock += 1;
690
691 $wasSet = 0;
692 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
693 $v = $cache->getMultiWithSetCallback(
694 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
695 );
696 $this->assertEquals( $value, $v[$keyB], "Value returned" );
697 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
698 $this->assertEquals( $value, $priorValue, "Has prior value" );
699 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
700 $t1 = $cache->getCheckKeyTime( $cKey1 );
701 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
702 $t2 = $cache->getCheckKeyTime( $cKey2 );
703 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
704
705 $mockWallClock += 0.01;
706 $priorTime = $mockWallClock;
707 $value = "@43636$";
708 $wasSet = 0;
709 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
710 $v = $cache->getMultiWithSetCallback(
711 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
712 );
713 $this->assertEquals( $value, $v[$keyC], "Value returned" );
714 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
715 $t1 = $cache->getCheckKeyTime( $cKey1 );
716 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
717 $t2 = $cache->getCheckKeyTime( $cKey2 );
718 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
719
720 $curTTL = null;
721 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
722 $this->assertEquals( $value, $v, "Value returned" );
723 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
724
725 $wasSet = 0;
726 $key = wfRandomString();
727 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
728 $v = $cache->getMultiWithSetCallback(
729 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
730 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
731 $cache->delete( $key );
732 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
733 $v = $cache->getMultiWithSetCallback(
734 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
735 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
736 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
737
738 $calls = 0;
739 $ids = [ 1, 2, 3, 4, 5, 6 ];
740 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
741 return $wanCache->makeKey( 'test', $id );
742 };
743 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
744 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
745 ++$calls;
746
747 return "val-{$id}";
748 };
749 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
750
751 $this->assertEquals(
752 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
753 array_values( $values ),
754 "Correct values in correct order"
755 );
756 $this->assertEquals(
757 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
758 array_keys( $values ),
759 "Correct keys in correct order"
760 );
761 $this->assertEquals( count( $ids ), $calls );
762
763 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
764 $this->assertEquals( count( $ids ), $calls, "Values cached" );
765
766 // Mock the BagOStuff to assure only one getMulti() call given process caching
767 $localBag = $this->getMockBuilder( HashBagOStuff::class )
768 ->setMethods( [ 'getMulti' ] )->getMock();
769 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
770 'WANCache:v:' . 'k1' => 'val-id1',
771 'WANCache:v:' . 'k2' => 'val-id2'
772 ] );
773 $wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
774
775 // Warm the process cache
776 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
777 $this->assertEquals(
778 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
779 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
780 );
781 // Use the process cache
782 $this->assertEquals(
783 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
784 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
785 );
786 }
787
788 public static function getMultiWithSetCallback_provider() {
789 return [
790 [ [], false ],
791 [ [ 'version' => 1 ], true ]
792 ];
793 }
794
795 /**
796 * @dataProvider getMultiWithUnionSetCallback_provider
797 * @covers WANObjectCache::getMultiWithUnionSetCallback()
798 * @covers WANObjectCache::makeMultiKeys()
799 * @param array $extOpts
800 * @param bool $versioned
801 */
802 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
803 $cache = $this->cache;
804
805 $keyA = wfRandomString();
806 $keyB = wfRandomString();
807 $keyC = wfRandomString();
808 $cKey1 = wfRandomString();
809 $cKey2 = wfRandomString();
810
811 $wasSet = 0;
812 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
813 &$wasSet, &$priorValue, &$priorAsOf
814 ) {
815 $newValues = [];
816 foreach ( $ids as $id ) {
817 ++$wasSet;
818 $newValues[$id] = "@$id$";
819 $ttls[$id] = 20; // override with another value
820 }
821
822 return $newValues;
823 };
824
825 $mockWallClock = 1549343530.2053;
826 $priorTime = $mockWallClock; // reference time
827 $cache->setMockTime( $mockWallClock );
828
829 $wasSet = 0;
830 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
831 $value = "@3353$";
832 $v = $cache->getMultiWithUnionSetCallback(
833 $keyedIds, 30, $genFunc, $extOpts );
834 $this->assertEquals( $value, $v[$keyA], "Value returned" );
835 $this->assertEquals( 1, $wasSet, "Value regenerated" );
836
837 $curTTL = null;
838 $cache->get( $keyA, $curTTL );
839 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
840 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
841
842 $wasSet = 0;
843 $value = "@efef$";
844 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
845 $v = $cache->getMultiWithUnionSetCallback(
846 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
847 $this->assertEquals( $value, $v[$keyB], "Value returned" );
848 $this->assertEquals( 1, $wasSet, "Value regenerated" );
849 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
850
851 $v = $cache->getMultiWithUnionSetCallback(
852 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
853 $this->assertEquals( $value, $v[$keyB], "Value returned" );
854 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
855 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
856
857 $mockWallClock += 1;
858
859 $wasSet = 0;
860 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
861 $v = $cache->getMultiWithUnionSetCallback(
862 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
863 );
864 $this->assertEquals( $value, $v[$keyB], "Value returned" );
865 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
866 $t1 = $cache->getCheckKeyTime( $cKey1 );
867 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
868 $t2 = $cache->getCheckKeyTime( $cKey2 );
869 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
870
871 $mockWallClock += 0.01;
872 $priorTime = $mockWallClock;
873 $value = "@43636$";
874 $wasSet = 0;
875 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
876 $v = $cache->getMultiWithUnionSetCallback(
877 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
878 );
879 $this->assertEquals( $value, $v[$keyC], "Value returned" );
880 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
881 $t1 = $cache->getCheckKeyTime( $cKey1 );
882 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
883 $t2 = $cache->getCheckKeyTime( $cKey2 );
884 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
885
886 $curTTL = null;
887 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
888 $this->assertEquals( $value, $v, "Value returned" );
889 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
890
891 $wasSet = 0;
892 $key = wfRandomString();
893 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
894 $v = $cache->getMultiWithUnionSetCallback(
895 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
896 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
897 $cache->delete( $key );
898 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
899 $v = $cache->getMultiWithUnionSetCallback(
900 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
901 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
902 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
903
904 $calls = 0;
905 $ids = [ 1, 2, 3, 4, 5, 6 ];
906 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
907 return $wanCache->makeKey( 'test', $id );
908 };
909 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
910 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
911 $newValues = [];
912 foreach ( $ids as $id ) {
913 ++$calls;
914 $newValues[$id] = "val-{$id}";
915 }
916
917 return $newValues;
918 };
919 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
920
921 $this->assertEquals(
922 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
923 array_values( $values ),
924 "Correct values in correct order"
925 );
926 $this->assertEquals(
927 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
928 array_keys( $values ),
929 "Correct keys in correct order"
930 );
931 $this->assertEquals( count( $ids ), $calls );
932
933 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
934 $this->assertEquals( count( $ids ), $calls, "Values cached" );
935 }
936
937 public static function getMultiWithUnionSetCallback_provider() {
938 return [
939 [ [], false ],
940 [ [ 'version' => 1 ], true ]
941 ];
942 }
943
944 /**
945 * @covers WANObjectCache::getWithSetCallback()
946 * @covers WANObjectCache::fetchOrRegenerate()
947 */
948 public function testLockTSE() {
949 $cache = $this->cache;
950 $key = wfRandomString();
951 $value = wfRandomString();
952
953 $mockWallClock = 1549343530.2053;
954 $cache->setMockTime( $mockWallClock );
955
956 $calls = 0;
957 $func = function () use ( &$calls, $value, $cache, $key ) {
958 ++$calls;
959 return $value;
960 };
961
962 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
963 $this->assertEquals( $value, $ret );
964 $this->assertEquals( 1, $calls, 'Value was populated' );
965
966 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
967 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
968
969 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
970 $ret = $cache->getWithSetCallback( $key, 30, $func,
971 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
972 $this->assertEquals( $value, $ret, 'Old value used' );
973 $this->assertEquals( 1, $calls, 'Callback was not used' );
974
975 $cache->delete( $key );
976 $mockWallClock += 0.001; // cached values will be newer than tombstone
977 $ret = $cache->getWithSetCallback( $key, 30, $func,
978 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
979 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
980 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
981
982 $ret = $cache->getWithSetCallback( $key, 30, $func,
983 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
984 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
985 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
986 }
987
988 /**
989 * @covers WANObjectCache::getWithSetCallback()
990 * @covers WANObjectCache::fetchOrRegenerate()
991 * @covers WANObjectCache::set()
992 */
993 public function testLockTSESlow() {
994 $cache = $this->cache;
995 $key = wfRandomString();
996 $key2 = wfRandomString();
997 $value = wfRandomString();
998
999 $mockWallClock = 1549343530.2053;
1000 $cache->setMockTime( $mockWallClock );
1001
1002 $calls = 0;
1003 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, &$mockWallClock ) {
1004 ++$calls;
1005 $setOpts['since'] = $mockWallClock - 10;
1006 return $value;
1007 };
1008
1009 // Value should be given a low logical TTL due to snapshot lag
1010 $curTTL = null;
1011 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1012 $this->assertEquals( $value, $ret );
1013 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
1014 $this->assertEquals( 1, $curTTL, 'Value has reduced logical TTL', 0.01 );
1015 $this->assertEquals( 1, $calls, 'Value was generated' );
1016
1017 $mockWallClock += 2; // low logical TTL expired
1018
1019 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1020 $this->assertEquals( $value, $ret );
1021 $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
1022
1023 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1024 $this->assertEquals( $value, $ret );
1025 $this->assertEquals( 2, $calls, 'Callback was not used (interim value used)' );
1026
1027 $mockWallClock += 2; // low logical TTL expired
1028 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
1029 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1030
1031 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1032 $this->assertEquals( $value, $ret );
1033 $this->assertEquals( 2, $calls, 'Callback was not used (mutex not acquired)' );
1034
1035 $mockWallClock += 301; // physical TTL expired
1036 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
1037 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1038
1039 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1040 $this->assertEquals( $value, $ret );
1041 $this->assertEquals( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' );
1042
1043 $calls = 0;
1044 $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
1045 ++$calls;
1046 $setOpts['lag'] = 15;
1047 return $value;
1048 };
1049
1050 // Value should be given a low logical TTL due to replication lag
1051 $curTTL = null;
1052 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1053 $this->assertEquals( $value, $ret );
1054 $this->assertEquals( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
1055 $this->assertEquals( 30, $curTTL, 'Value has reduced logical TTL', 0.01 );
1056 $this->assertEquals( 1, $calls, 'Value was generated' );
1057
1058 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1059 $this->assertEquals( $value, $ret );
1060 $this->assertEquals( 1, $calls, 'Callback was used (not expired)' );
1061
1062 $mockWallClock += 31;
1063
1064 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1065 $this->assertEquals( $value, $ret );
1066 $this->assertEquals( 2, $calls, 'Callback was used (mutex acquired)' );
1067 }
1068
1069 /**
1070 * @covers WANObjectCache::getWithSetCallback()
1071 * @covers WANObjectCache::fetchOrRegenerate()
1072 */
1073 public function testBusyValue() {
1074 $cache = $this->cache;
1075 $key = wfRandomString();
1076 $value = wfRandomString();
1077 $busyValue = wfRandomString();
1078
1079 $mockWallClock = 1549343530.2053;
1080 $cache->setMockTime( $mockWallClock );
1081
1082 $calls = 0;
1083 $func = function () use ( &$calls, $value, $cache, $key ) {
1084 ++$calls;
1085 return $value;
1086 };
1087
1088 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
1089 $this->assertEquals( $value, $ret );
1090 $this->assertEquals( 1, $calls, 'Value was populated' );
1091
1092 $mockWallClock += 0.2; // interim keys not brand new
1093
1094 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
1095 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1096
1097 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
1098 $ret = $cache->getWithSetCallback( $key, 30, $func,
1099 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1100 $this->assertEquals( $value, $ret, 'Callback used' );
1101 $this->assertEquals( 2, $calls, 'Callback used' );
1102
1103 $ret = $cache->getWithSetCallback( $key, 30, $func,
1104 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1105 $this->assertEquals( $value, $ret, 'Old value used' );
1106 $this->assertEquals( 2, $calls, 'Callback was not used' );
1107
1108 $cache->delete( $key ); // no value at all anymore and still locked
1109 $ret = $cache->getWithSetCallback( $key, 30, $func,
1110 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1111 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
1112 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
1113
1114 $this->internalCache->delete( 'WANCache:m:' . $key );
1115 $mockWallClock += 0.001; // cached values will be newer than tombstone
1116 $ret = $cache->getWithSetCallback( $key, 30, $func,
1117 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1118 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
1119 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
1120
1121 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1122 $ret = $cache->getWithSetCallback( $key, 30, $func,
1123 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1124 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
1125 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
1126 }
1127
1128 /**
1129 * @covers WANObjectCache::getMulti()
1130 */
1131 public function testGetMulti() {
1132 $cache = $this->cache;
1133
1134 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1135 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1136
1137 $key1 = wfRandomString();
1138 $key2 = wfRandomString();
1139 $key3 = wfRandomString();
1140
1141 $mockWallClock = 1549343530.2053;
1142 $priorTime = $mockWallClock; // reference time
1143 $cache->setMockTime( $mockWallClock );
1144
1145 $cache->set( $key1, $value1, 5 );
1146 $cache->set( $key2, $value2, 10 );
1147
1148 $curTTLs = [];
1149 $this->assertSame(
1150 [ $key1 => $value1, $key2 => $value2 ],
1151 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1152 'Result array populated'
1153 );
1154
1155 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1156 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1157 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1158
1159 $cKey1 = wfRandomString();
1160 $cKey2 = wfRandomString();
1161
1162 $mockWallClock += 1;
1163
1164 $curTTLs = [];
1165 $this->assertSame(
1166 [ $key1 => $value1, $key2 => $value2 ],
1167 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1168 "Result array populated even with new check keys"
1169 );
1170 $t1 = $cache->getCheckKeyTime( $cKey1 );
1171 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1172 $t2 = $cache->getCheckKeyTime( $cKey2 );
1173 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1174 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1175 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1176 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1177
1178 $mockWallClock += 1;
1179
1180 $curTTLs = [];
1181 $this->assertEquals(
1182 [ $key1 => $value1, $key2 => $value2 ],
1183 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1184 "Result array still populated even with new check keys"
1185 );
1186 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1187 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1188 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1189 }
1190
1191 /**
1192 * @covers WANObjectCache::getMulti()
1193 * @covers WANObjectCache::processCheckKeys()
1194 */
1195 public function testGetMultiCheckKeys() {
1196 $cache = $this->cache;
1197
1198 $checkAll = wfRandomString();
1199 $check1 = wfRandomString();
1200 $check2 = wfRandomString();
1201 $check3 = wfRandomString();
1202 $value1 = wfRandomString();
1203 $value2 = wfRandomString();
1204
1205 $mockWallClock = 1549343530.2053;
1206 $cache->setMockTime( $mockWallClock );
1207
1208 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1209 // several seconds during the test to assert the behaviour.
1210 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1211 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_TTL_NONE );
1212 }
1213
1214 $mockWallClock += 0.100;
1215
1216 $cache->set( 'key1', $value1, 10 );
1217 $cache->set( 'key2', $value2, 10 );
1218
1219 $curTTLs = [];
1220 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1221 'key1' => $check1,
1222 $checkAll,
1223 'key2' => $check2,
1224 'key3' => $check3,
1225 ] );
1226 $this->assertSame(
1227 [ 'key1' => $value1, 'key2' => $value2 ],
1228 $result,
1229 'Initial values'
1230 );
1231 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1232 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1233 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1234 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1235
1236 $mockWallClock += 0.100;
1237 $cache->touchCheckKey( $check1 );
1238
1239 $curTTLs = [];
1240 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1241 'key1' => $check1,
1242 $checkAll,
1243 'key2' => $check2,
1244 'key3' => $check3,
1245 ] );
1246 $this->assertSame(
1247 [ 'key1' => $value1, 'key2' => $value2 ],
1248 $result,
1249 'key1 expired by check1, but value still provided'
1250 );
1251 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1252 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1253
1254 $cache->touchCheckKey( $checkAll );
1255
1256 $curTTLs = [];
1257 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1258 'key1' => $check1,
1259 $checkAll,
1260 'key2' => $check2,
1261 'key3' => $check3,
1262 ] );
1263 $this->assertEquals(
1264 [ 'key1' => $value1, 'key2' => $value2 ],
1265 $result,
1266 'All keys expired by checkAll, but value still provided'
1267 );
1268 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1269 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1270 }
1271
1272 /**
1273 * @covers WANObjectCache::get()
1274 * @covers WANObjectCache::processCheckKeys()
1275 */
1276 public function testCheckKeyInitHoldoff() {
1277 $cache = $this->cache;
1278
1279 for ( $i = 0; $i < 500; ++$i ) {
1280 $key = wfRandomString();
1281 $checkKey = wfRandomString();
1282 // miss, set, hit
1283 $cache->get( $key, $curTTL, [ $checkKey ] );
1284 $cache->set( $key, 'val', 10 );
1285 $curTTL = null;
1286 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1287
1288 $this->assertEquals( 'val', $v );
1289 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1290 }
1291
1292 for ( $i = 0; $i < 500; ++$i ) {
1293 $key = wfRandomString();
1294 $checkKey = wfRandomString();
1295 // set, hit
1296 $cache->set( $key, 'val', 10 );
1297 $curTTL = null;
1298 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1299
1300 $this->assertEquals( 'val', $v );
1301 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1302 }
1303 }
1304
1305 /**
1306 * @covers WANObjectCache::delete
1307 * @covers WANObjectCache::relayDelete
1308 * @covers WANObjectCache::relayPurge
1309 */
1310 public function testDelete() {
1311 $key = wfRandomString();
1312 $value = wfRandomString();
1313 $this->cache->set( $key, $value );
1314
1315 $curTTL = null;
1316 $v = $this->cache->get( $key, $curTTL );
1317 $this->assertEquals( $value, $v, "Key was created with value" );
1318 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1319
1320 $this->cache->delete( $key );
1321
1322 $curTTL = null;
1323 $v = $this->cache->get( $key, $curTTL );
1324 $this->assertFalse( $v, "Deleted key has false value" );
1325 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1326
1327 $this->cache->set( $key, $value . 'more' );
1328 $v = $this->cache->get( $key, $curTTL );
1329 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1330 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1331
1332 $this->cache->set( $key, $value );
1333 $this->cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE );
1334
1335 $curTTL = null;
1336 $v = $this->cache->get( $key, $curTTL );
1337 $this->assertFalse( $v, "Deleted key has false value" );
1338 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1339
1340 $this->cache->set( $key, $value );
1341 $v = $this->cache->get( $key, $curTTL );
1342 $this->assertEquals( $value, $v, "Key was created with value" );
1343 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1344 }
1345
1346 /**
1347 * @dataProvider getWithSetCallback_versions_provider
1348 * @covers WANObjectCache::getWithSetCallback()
1349 * @covers WANObjectCache::fetchOrRegenerate()
1350 * @param array $extOpts
1351 * @param bool $versioned
1352 */
1353 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1354 $cache = $this->cache;
1355
1356 $key = wfRandomString();
1357 $valueV1 = wfRandomString();
1358 $valueV2 = [ wfRandomString() ];
1359
1360 $wasSet = 0;
1361 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1362 ++$wasSet;
1363
1364 return $valueV1;
1365 };
1366
1367 $priorValue = false;
1368 $priorAsOf = null;
1369 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1370 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1371 $priorValue = $oldValue;
1372 $priorAsOf = $oldAsOf;
1373 ++$wasSet;
1374
1375 return $valueV2; // new array format
1376 };
1377
1378 // Set the main key (version N if versioned)
1379 $wasSet = 0;
1380 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1381 $this->assertEquals( $valueV1, $v, "Value returned" );
1382 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1383 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1384 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1385 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1386
1387 if ( $versioned ) {
1388 // Set the key for version N+1 format
1389 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1390 } else {
1391 // Start versioning now with the unversioned key still there
1392 $verOpts = [ 'version' => 1 ];
1393 }
1394
1395 // Value goes to secondary key since V1 already used $key
1396 $wasSet = 0;
1397 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1398 $this->assertEquals( $valueV2, $v, "Value returned" );
1399 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1400 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1401 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1402
1403 $wasSet = 0;
1404 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1405 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1406 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1407
1408 // Clear out the older or unversioned key
1409 $cache->delete( $key, 0 );
1410
1411 // Set the key for next/first versioned format
1412 $wasSet = 0;
1413 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1414 $this->assertEquals( $valueV2, $v, "Value returned" );
1415 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1416
1417 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1418 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1419 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1420 }
1421
1422 public static function getWithSetCallback_versions_provider() {
1423 return [
1424 [ [], false ],
1425 [ [ 'version' => 1 ], true ]
1426 ];
1427 }
1428
1429 /**
1430 * @covers WANObjectCache::useInterimHoldOffCaching
1431 * @covers WANObjectCache::getInterimValue
1432 */
1433 public function testInterimHoldOffCaching() {
1434 $cache = $this->cache;
1435
1436 $mockWallClock = 1549343530.2053;
1437 $cache->setMockTime( $mockWallClock );
1438
1439 $value = 'CRL-40-940';
1440 $wasCalled = 0;
1441 $func = function () use ( &$wasCalled, $value ) {
1442 $wasCalled++;
1443
1444 return $value;
1445 };
1446
1447 $cache->useInterimHoldOffCaching( true );
1448
1449 $key = wfRandomString( 32 );
1450 $v = $cache->getWithSetCallback( $key, 60, $func );
1451 $v = $cache->getWithSetCallback( $key, 60, $func );
1452 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1453
1454 $cache->delete( $key );
1455 $mockWallClock += 0.001; // cached values will be newer than tombstone
1456 $v = $cache->getWithSetCallback( $key, 60, $func );
1457 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1458 $v = $cache->getWithSetCallback( $key, 60, $func );
1459 $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
1460
1461 $mockWallClock += 0.2; // interim key not brand new
1462 $v = $cache->getWithSetCallback( $key, 60, $func );
1463 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1464 // Lock up the mutex so interim cache is used
1465 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1466 $v = $cache->getWithSetCallback( $key, 60, $func );
1467 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1468 $this->internalCache->delete( 'WANCache:m:' . $key );
1469
1470 $cache->useInterimHoldOffCaching( false );
1471
1472 $wasCalled = 0;
1473 $key = wfRandomString( 32 );
1474 $v = $cache->getWithSetCallback( $key, 60, $func );
1475 $v = $cache->getWithSetCallback( $key, 60, $func );
1476 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1477 $cache->delete( $key );
1478 $v = $cache->getWithSetCallback( $key, 60, $func );
1479 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1480 $v = $cache->getWithSetCallback( $key, 60, $func );
1481 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1482 $v = $cache->getWithSetCallback( $key, 60, $func );
1483 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1484 // Lock up the mutex so interim cache is used
1485 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1486 $v = $cache->getWithSetCallback( $key, 60, $func );
1487 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1488 }
1489
1490 /**
1491 * @covers WANObjectCache::touchCheckKey
1492 * @covers WANObjectCache::resetCheckKey
1493 * @covers WANObjectCache::getCheckKeyTime
1494 * @covers WANObjectCache::getMultiCheckKeyTime
1495 * @covers WANObjectCache::makePurgeValue
1496 * @covers WANObjectCache::parsePurgeValue
1497 */
1498 public function testTouchKeys() {
1499 $cache = $this->cache;
1500 $key = wfRandomString();
1501
1502 $mockWallClock = 1549343530.2053;
1503 $priorTime = $mockWallClock; // reference time
1504 $cache->setMockTime( $mockWallClock );
1505
1506 $mockWallClock += 0.100;
1507 $t0 = $cache->getCheckKeyTime( $key );
1508 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1509
1510 $priorTime = $mockWallClock;
1511 $mockWallClock += 0.100;
1512 $cache->touchCheckKey( $key );
1513 $t1 = $cache->getCheckKeyTime( $key );
1514 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1515
1516 $t2 = $cache->getCheckKeyTime( $key );
1517 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1518
1519 $mockWallClock += 0.100;
1520 $cache->touchCheckKey( $key );
1521 $t3 = $cache->getCheckKeyTime( $key );
1522 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1523
1524 $t4 = $cache->getCheckKeyTime( $key );
1525 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1526
1527 $mockWallClock += 0.100;
1528 $cache->resetCheckKey( $key );
1529 $t5 = $cache->getCheckKeyTime( $key );
1530 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1531
1532 $t6 = $cache->getCheckKeyTime( $key );
1533 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1534 }
1535
1536 /**
1537 * @covers WANObjectCache::getMulti()
1538 */
1539 public function testGetWithSeveralCheckKeys() {
1540 $key = wfRandomString();
1541 $tKey1 = wfRandomString();
1542 $tKey2 = wfRandomString();
1543 $value = 'meow';
1544
1545 $mockWallClock = 1549343530.2053;
1546 $priorTime = $mockWallClock; // reference time
1547 $this->cache->setMockTime( $mockWallClock );
1548
1549 // Two check keys are newer (given hold-off) than $key, another is older
1550 $this->internalCache->set(
1551 'WANCache:t:' . $tKey2,
1552 'PURGED:' . ( $priorTime - 3 )
1553 );
1554 $this->internalCache->set(
1555 'WANCache:t:' . $tKey2,
1556 'PURGED:' . ( $priorTime - 5 )
1557 );
1558 $this->internalCache->set(
1559 'WANCache:t:' . $tKey1,
1560 'PURGED:' . ( $priorTime - 30 )
1561 );
1562 $this->cache->set( $key, $value, 30 );
1563
1564 $curTTL = null;
1565 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1566 $this->assertEquals( $value, $v, "Value matches" );
1567 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1568 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1569 }
1570
1571 /**
1572 * @covers WANObjectCache::reap()
1573 * @covers WANObjectCache::reapCheckKey()
1574 */
1575 public function testReap() {
1576 $vKey1 = wfRandomString();
1577 $vKey2 = wfRandomString();
1578 $tKey1 = wfRandomString();
1579 $tKey2 = wfRandomString();
1580 $value = 'moo';
1581
1582 $knownPurge = time() - 60;
1583 $goodTime = microtime( true ) - 5;
1584 $badTime = microtime( true ) - 300;
1585
1586 $this->internalCache->set(
1587 'WANCache:v:' . $vKey1,
1588 [
1589 0 => 1,
1590 1 => $value,
1591 2 => 3600,
1592 3 => $goodTime
1593 ]
1594 );
1595 $this->internalCache->set(
1596 'WANCache:v:' . $vKey2,
1597 [
1598 0 => 1,
1599 1 => $value,
1600 2 => 3600,
1601 3 => $badTime
1602 ]
1603 );
1604 $this->internalCache->set(
1605 'WANCache:t:' . $tKey1,
1606 'PURGED:' . $goodTime
1607 );
1608 $this->internalCache->set(
1609 'WANCache:t:' . $tKey2,
1610 'PURGED:' . $badTime
1611 );
1612
1613 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1614 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1615 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1616 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1617
1618 $this->assertFalse( $bad1 );
1619 $this->assertTrue( $bad2 );
1620
1621 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1622 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1623 $this->assertFalse( $tBad1 );
1624 $this->assertTrue( $tBad2 );
1625 }
1626
1627 /**
1628 * @covers WANObjectCache::reap()
1629 */
1630 public function testReap_fail() {
1631 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1632 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1633 $backend->expects( $this->once() )->method( 'get' )
1634 ->willReturn( [
1635 0 => 1,
1636 1 => 'value',
1637 2 => 3600,
1638 3 => 300,
1639 ] );
1640 $backend->expects( $this->once() )->method( 'changeTTL' )
1641 ->willReturn( false );
1642
1643 $wanCache = new WANObjectCache( [
1644 'cache' => $backend
1645 ] );
1646
1647 $isStale = null;
1648 $ret = $wanCache->reap( 'key', 360, $isStale );
1649 $this->assertTrue( $isStale, 'value was stale' );
1650 $this->assertFalse( $ret, 'changeTTL failed' );
1651 }
1652
1653 /**
1654 * @covers WANObjectCache::set()
1655 */
1656 public function testSetWithLag() {
1657 $value = 1;
1658
1659 $key = wfRandomString();
1660 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1661 $this->cache->set( $key, $value, 30, $opts );
1662 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1663
1664 $key = wfRandomString();
1665 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1666 $this->cache->set( $key, $value, 30, $opts );
1667 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1668
1669 $key = wfRandomString();
1670 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1671 $this->cache->set( $key, $value, 30, $opts );
1672 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1673 }
1674
1675 /**
1676 * @covers WANObjectCache::set()
1677 */
1678 public function testWritePending() {
1679 $value = 1;
1680
1681 $key = wfRandomString();
1682 $opts = [ 'pending' => true ];
1683 $this->cache->set( $key, $value, 30, $opts );
1684 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1685 }
1686
1687 public function testMcRouterSupport() {
1688 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1689 ->setMethods( [ 'set', 'delete' ] )->getMock();
1690 $localBag->expects( $this->never() )->method( 'set' );
1691 $localBag->expects( $this->never() )->method( 'delete' );
1692 $wanCache = new WANObjectCache( [
1693 'cache' => $localBag,
1694 'mcrouterAware' => true,
1695 'region' => 'pmtpa',
1696 'cluster' => 'mw-wan'
1697 ] );
1698 $valFunc = function () {
1699 return 1;
1700 };
1701
1702 // None of these should use broadcasting commands (e.g. SET, DELETE)
1703 $wanCache->get( 'x' );
1704 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1705 $wanCache->getMulti( [ 'x', 'y' ] );
1706 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1707 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1708 $wanCache->getCheckKeyTime( 'zzz' );
1709 $wanCache->reap( 'x', time() - 300 );
1710 $wanCache->reap( 'zzz', time() - 300 );
1711 }
1712
1713 public function testMcRouterSupportBroadcastDelete() {
1714 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1715 ->setMethods( [ 'set' ] )->getMock();
1716 $wanCache = new WANObjectCache( [
1717 'cache' => $localBag,
1718 'mcrouterAware' => true,
1719 'region' => 'pmtpa',
1720 'cluster' => 'mw-wan'
1721 ] );
1722
1723 $localBag->expects( $this->once() )->method( 'set' )
1724 ->with( "/*/mw-wan/" . 'WANCache:v:' . "test" );
1725
1726 $wanCache->delete( 'test' );
1727 }
1728
1729 public function testMcRouterSupportBroadcastTouchCK() {
1730 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1731 ->setMethods( [ 'set' ] )->getMock();
1732 $wanCache = new WANObjectCache( [
1733 'cache' => $localBag,
1734 'mcrouterAware' => true,
1735 'region' => 'pmtpa',
1736 'cluster' => 'mw-wan'
1737 ] );
1738
1739 $localBag->expects( $this->once() )->method( 'set' )
1740 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1741
1742 $wanCache->touchCheckKey( 'test' );
1743 }
1744
1745 public function testMcRouterSupportBroadcastResetCK() {
1746 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1747 ->setMethods( [ 'delete' ] )->getMock();
1748 $wanCache = new WANObjectCache( [
1749 'cache' => $localBag,
1750 'mcrouterAware' => true,
1751 'region' => 'pmtpa',
1752 'cluster' => 'mw-wan'
1753 ] );
1754
1755 $localBag->expects( $this->once() )->method( 'delete' )
1756 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1757
1758 $wanCache->resetCheckKey( 'test' );
1759 }
1760
1761 public function testEpoch() {
1762 $bag = new HashBagOStuff();
1763 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1764 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1765
1766 $now = microtime( true );
1767 $cache->setMockTime( $now );
1768
1769 $cache->set( $key, 'Do what thou Wilt' );
1770 $cache->touchCheckKey( $key );
1771
1772 $then = $now;
1773 $now += 30;
1774 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1775 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1776
1777 $cache = new WANObjectCache( [
1778 'cache' => $bag,
1779 'epoch' => $now - 3600
1780 ] );
1781 $cache->setMockTime( $now );
1782
1783 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1784 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1785
1786 $now += 30;
1787 $cache = new WANObjectCache( [
1788 'cache' => $bag,
1789 'epoch' => $now + 3600
1790 ] );
1791 $cache->setMockTime( $now );
1792
1793 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1794 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1795 }
1796
1797 /**
1798 * @dataProvider provideAdaptiveTTL
1799 * @covers WANObjectCache::adaptiveTTL()
1800 * @param float|int $ago
1801 * @param int $maxTTL
1802 * @param int $minTTL
1803 * @param float $factor
1804 * @param int $adaptiveTTL
1805 */
1806 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1807 $mtime = $ago ? time() - $ago : $ago;
1808 $margin = 5;
1809 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1810
1811 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1812 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1813
1814 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1815
1816 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1817 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1818 }
1819
1820 public static function provideAdaptiveTTL() {
1821 return [
1822 [ 3600, 900, 30, 0.2, 720 ],
1823 [ 3600, 500, 30, 0.2, 500 ],
1824 [ 3600, 86400, 800, 0.2, 800 ],
1825 [ false, 86400, 800, 0.2, 800 ],
1826 [ null, 86400, 800, 0.2, 800 ]
1827 ];
1828 }
1829
1830 /**
1831 * @covers WANObjectCache::__construct
1832 * @covers WANObjectCache::newEmpty
1833 */
1834 public function testNewEmpty() {
1835 $this->assertInstanceOf(
1836 WANObjectCache::class,
1837 WANObjectCache::newEmpty()
1838 );
1839 }
1840
1841 /**
1842 * @covers WANObjectCache::setLogger
1843 */
1844 public function testSetLogger() {
1845 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1846 }
1847
1848 /**
1849 * @covers WANObjectCache::getQoS
1850 */
1851 public function testGetQoS() {
1852 $backend = $this->getMockBuilder( HashBagOStuff::class )
1853 ->setMethods( [ 'getQoS' ] )->getMock();
1854 $backend->expects( $this->once() )->method( 'getQoS' )
1855 ->willReturn( BagOStuff::QOS_UNKNOWN );
1856 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1857
1858 $this->assertSame(
1859 $wanCache::QOS_UNKNOWN,
1860 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1861 );
1862 }
1863
1864 /**
1865 * @covers WANObjectCache::makeKey
1866 */
1867 public function testMakeKey() {
1868 if ( defined( 'HHVM_VERSION' ) ) {
1869 $this->markTestSkipped( 'HHVM Reflection buggy' );
1870 }
1871
1872 $backend = $this->getMockBuilder( HashBagOStuff::class )
1873 ->setMethods( [ 'makeKey' ] )->getMock();
1874 $backend->expects( $this->once() )->method( 'makeKey' )
1875 ->willReturn( 'special' );
1876
1877 $wanCache = new WANObjectCache( [
1878 'cache' => $backend
1879 ] );
1880
1881 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1882 }
1883
1884 /**
1885 * @covers WANObjectCache::makeGlobalKey
1886 */
1887 public function testMakeGlobalKey() {
1888 if ( defined( 'HHVM_VERSION' ) ) {
1889 $this->markTestSkipped( 'HHVM Reflection buggy' );
1890 }
1891
1892 $backend = $this->getMockBuilder( HashBagOStuff::class )
1893 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1894 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1895 ->willReturn( 'special' );
1896
1897 $wanCache = new WANObjectCache( [
1898 'cache' => $backend
1899 ] );
1900
1901 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1902 }
1903
1904 public static function statsKeyProvider() {
1905 return [
1906 [ 'domain:page:5', 'page' ],
1907 [ 'domain:main-key', 'main-key' ],
1908 [ 'domain:page:history', 'page' ],
1909 [ 'missingdomainkey', 'missingdomainkey' ]
1910 ];
1911 }
1912
1913 /**
1914 * @dataProvider statsKeyProvider
1915 * @covers WANObjectCache::determineKeyClassForStats
1916 */
1917 public function testStatsKeyClass( $key, $class ) {
1918 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1919 'cache' => new HashBagOStuff
1920 ] ) );
1921
1922 $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
1923 }
1924
1925 /**
1926 * @covers WANObjectCache::makeMultiKeys
1927 */
1928 public function testMakeMultiKeys() {
1929 $cache = $this->cache;
1930
1931 $ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
1932 $keyCallback = function ( $id, WANObjectCache $cache ) {
1933 return $cache->makeKey( 'key', $id );
1934 };
1935 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1936
1937 $expected = [
1938 "local:key:1" => 1,
1939 "local:key:2" => 2,
1940 "local:key:3" => 3,
1941 "local:key:4" => 4,
1942 "local:key:5" => 5,
1943 "local:key:6" => 6,
1944 "local:key:7" => 7
1945 ];
1946 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1947
1948 $ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
1949 $keyCallback = function ( $id, WANObjectCache $cache ) {
1950 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
1951 };
1952 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1953
1954 $expected = [
1955 "global:key:1:a:1:b" => '1',
1956 "global:key:2:a:2:b" => '2',
1957 "global:key:3:a:3:b" => '3',
1958 "global:key:4:a:4:b" => '4',
1959 "global:key:5:a:5:b" => '5',
1960 "global:key:6:a:6:b" => '6',
1961 "global:key:7:a:7:b" => '7'
1962 ];
1963 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1964 }
1965
1966 /**
1967 * @covers WANObjectCache::makeMultiKeys
1968 */
1969 public function testMakeMultiKeysIntString() {
1970 $cache = $this->cache;
1971 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
1972 $keyCallback = function ( $id, WANObjectCache $cache ) {
1973 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
1974 };
1975
1976 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1977
1978 $expected = [
1979 "global:key:1:a:1:b" => 1,
1980 "global:key:2:a:2:b" => 2,
1981 "global:key:3:a:3:b" => 3,
1982 "global:key:4:a:4:b" => 4,
1983 "global:key:5:a:5:b" => 5,
1984 "global:key:6:a:6:b" => 6,
1985 "global:key:7:a:7:b" => 7
1986 ];
1987 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1988 }
1989
1990 /**
1991 * @covers WANObjectCache::makeMultiKeys
1992 * @expectedException UnexpectedValueException
1993 */
1994 public function testMakeMultiKeysCollision() {
1995 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
1996
1997 $this->cache->makeMultiKeys(
1998 $ids,
1999 function ( $id ) {
2000 return "keymod:" . $id % 3;
2001 }
2002 );
2003 }
2004
2005 /**
2006 * @covers WANObjectCache::multiRemap
2007 */
2008 public function testMultiRemap() {
2009 $a = [ 'a', 'b', 'c' ];
2010 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
2011
2012 $this->assertEquals(
2013 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
2014 $this->cache->multiRemap( $a, $res )
2015 );
2016
2017 $a = [ 'a', 'b', 'c', 'c', 'd' ];
2018 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3, 'keyD' => 4 ];
2019
2020 $this->assertEquals(
2021 [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ],
2022 $this->cache->multiRemap( $a, $res )
2023 );
2024 }
2025
2026 /**
2027 * @covers WANObjectCache::hash256
2028 */
2029 public function testHash256() {
2030 $bag = new HashBagOStuff();
2031 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 5 ] );
2032 $this->assertEquals(
2033 'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
2034 $cache->hash256( 'x' )
2035 );
2036
2037 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 50 ] );
2038 $this->assertEquals(
2039 'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
2040 $cache->hash256( 'x' )
2041 );
2042
2043 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden' ] );
2044 $this->assertEquals(
2045 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2046 $cache->hash256( 'x' )
2047 );
2048
2049 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden', 'epoch' => 3 ] );
2050 $this->assertEquals(
2051 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2052 $cache->hash256( 'x' )
2053 );
2054 }
2055 }
2056
2057 class NearExpiringWANObjectCache extends WANObjectCache {
2058 const CLOCK_SKEW = 1;
2059
2060 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
2061 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
2062 }
2063 }
2064
2065 class PopularityRefreshingWANObjectCache extends WANObjectCache {
2066 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
2067 return ( ( $now - $asOf ) > $timeTillRefresh );
2068 }
2069 }