Merge "Add talk namespace option to Special:NewPages"
[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 testBusyValueBasic() {
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 ) {
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 public function getBusyValues_Provider() {
1129 $hash = new HashBagOStuff( [] );
1130
1131 return [
1132 [
1133 function () {
1134 return "Saint Oliver Plunckett";
1135 },
1136 'Saint Oliver Plunckett'
1137 ],
1138 [ 'strlen', 'strlen' ],
1139 [ 'WANObjectCache::newEmpty', 'WANObjectCache::newEmpty' ],
1140 [ [ 'WANObjectCache', 'newEmpty' ], [ 'WANObjectCache', 'newEmpty' ] ],
1141 [ [ $hash, 'getLastError' ], [ $hash, 'getLastError' ] ],
1142 [ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
1143 ];
1144 }
1145
1146 /**
1147 * @covers WANObjectCache::getWithSetCallback()
1148 * @covers WANObjectCache::fetchOrRegenerate()
1149 * @dataProvider getBusyValues_Provider
1150 * @param mixed $busyValue
1151 * @param mixed $expected
1152 */
1153 public function testBusyValueTypes( $busyValue, $expected ) {
1154 $cache = $this->cache;
1155 $key = wfRandomString();
1156
1157 $mockWallClock = 1549343530.2053;
1158 $cache->setMockTime( $mockWallClock );
1159
1160 $calls = 0;
1161 $func = function () use ( &$calls ) {
1162 ++$calls;
1163 return 418;
1164 };
1165
1166 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
1167 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1168
1169 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
1170 $this->assertSame( $expected, $ret, 'busyValue used as expected' );
1171 $this->assertSame( 0, $calls, 'busyValue was used' );
1172 }
1173
1174 /**
1175 * @covers WANObjectCache::getMulti()
1176 */
1177 public function testGetMulti() {
1178 $cache = $this->cache;
1179
1180 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1181 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1182
1183 $key1 = wfRandomString();
1184 $key2 = wfRandomString();
1185 $key3 = wfRandomString();
1186
1187 $mockWallClock = 1549343530.2053;
1188 $priorTime = $mockWallClock; // reference time
1189 $cache->setMockTime( $mockWallClock );
1190
1191 $cache->set( $key1, $value1, 5 );
1192 $cache->set( $key2, $value2, 10 );
1193
1194 $curTTLs = [];
1195 $this->assertSame(
1196 [ $key1 => $value1, $key2 => $value2 ],
1197 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1198 'Result array populated'
1199 );
1200
1201 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1202 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1203 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1204
1205 $cKey1 = wfRandomString();
1206 $cKey2 = wfRandomString();
1207
1208 $mockWallClock += 1;
1209
1210 $curTTLs = [];
1211 $this->assertSame(
1212 [ $key1 => $value1, $key2 => $value2 ],
1213 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1214 "Result array populated even with new check keys"
1215 );
1216 $t1 = $cache->getCheckKeyTime( $cKey1 );
1217 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1218 $t2 = $cache->getCheckKeyTime( $cKey2 );
1219 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1220 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1221 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1222 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1223
1224 $mockWallClock += 1;
1225
1226 $curTTLs = [];
1227 $this->assertEquals(
1228 [ $key1 => $value1, $key2 => $value2 ],
1229 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1230 "Result array still populated even with new check keys"
1231 );
1232 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1233 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1234 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1235 }
1236
1237 /**
1238 * @covers WANObjectCache::getMulti()
1239 * @covers WANObjectCache::processCheckKeys()
1240 */
1241 public function testGetMultiCheckKeys() {
1242 $cache = $this->cache;
1243
1244 $checkAll = wfRandomString();
1245 $check1 = wfRandomString();
1246 $check2 = wfRandomString();
1247 $check3 = wfRandomString();
1248 $value1 = wfRandomString();
1249 $value2 = wfRandomString();
1250
1251 $mockWallClock = 1549343530.2053;
1252 $cache->setMockTime( $mockWallClock );
1253
1254 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1255 // several seconds during the test to assert the behaviour.
1256 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1257 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_TTL_NONE );
1258 }
1259
1260 $mockWallClock += 0.100;
1261
1262 $cache->set( 'key1', $value1, 10 );
1263 $cache->set( 'key2', $value2, 10 );
1264
1265 $curTTLs = [];
1266 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1267 'key1' => $check1,
1268 $checkAll,
1269 'key2' => $check2,
1270 'key3' => $check3,
1271 ] );
1272 $this->assertSame(
1273 [ 'key1' => $value1, 'key2' => $value2 ],
1274 $result,
1275 'Initial values'
1276 );
1277 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1278 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1279 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1280 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1281
1282 $mockWallClock += 0.100;
1283 $cache->touchCheckKey( $check1 );
1284
1285 $curTTLs = [];
1286 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1287 'key1' => $check1,
1288 $checkAll,
1289 'key2' => $check2,
1290 'key3' => $check3,
1291 ] );
1292 $this->assertSame(
1293 [ 'key1' => $value1, 'key2' => $value2 ],
1294 $result,
1295 'key1 expired by check1, but value still provided'
1296 );
1297 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1298 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1299
1300 $cache->touchCheckKey( $checkAll );
1301
1302 $curTTLs = [];
1303 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1304 'key1' => $check1,
1305 $checkAll,
1306 'key2' => $check2,
1307 'key3' => $check3,
1308 ] );
1309 $this->assertEquals(
1310 [ 'key1' => $value1, 'key2' => $value2 ],
1311 $result,
1312 'All keys expired by checkAll, but value still provided'
1313 );
1314 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1315 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1316 }
1317
1318 /**
1319 * @covers WANObjectCache::get()
1320 * @covers WANObjectCache::processCheckKeys()
1321 */
1322 public function testCheckKeyInitHoldoff() {
1323 $cache = $this->cache;
1324
1325 for ( $i = 0; $i < 500; ++$i ) {
1326 $key = wfRandomString();
1327 $checkKey = wfRandomString();
1328 // miss, set, hit
1329 $cache->get( $key, $curTTL, [ $checkKey ] );
1330 $cache->set( $key, 'val', 10 );
1331 $curTTL = null;
1332 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1333
1334 $this->assertEquals( 'val', $v );
1335 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1336 }
1337
1338 for ( $i = 0; $i < 500; ++$i ) {
1339 $key = wfRandomString();
1340 $checkKey = wfRandomString();
1341 // set, hit
1342 $cache->set( $key, 'val', 10 );
1343 $curTTL = null;
1344 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1345
1346 $this->assertEquals( 'val', $v );
1347 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1348 }
1349 }
1350
1351 /**
1352 * @covers WANObjectCache::delete
1353 * @covers WANObjectCache::relayDelete
1354 * @covers WANObjectCache::relayPurge
1355 */
1356 public function testDelete() {
1357 $key = wfRandomString();
1358 $value = wfRandomString();
1359 $this->cache->set( $key, $value );
1360
1361 $curTTL = null;
1362 $v = $this->cache->get( $key, $curTTL );
1363 $this->assertEquals( $value, $v, "Key was created with value" );
1364 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1365
1366 $this->cache->delete( $key );
1367
1368 $curTTL = null;
1369 $v = $this->cache->get( $key, $curTTL );
1370 $this->assertFalse( $v, "Deleted key has false value" );
1371 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1372
1373 $this->cache->set( $key, $value . 'more' );
1374 $v = $this->cache->get( $key, $curTTL );
1375 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1376 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1377
1378 $this->cache->set( $key, $value );
1379 $this->cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE );
1380
1381 $curTTL = null;
1382 $v = $this->cache->get( $key, $curTTL );
1383 $this->assertFalse( $v, "Deleted key has false value" );
1384 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1385
1386 $this->cache->set( $key, $value );
1387 $v = $this->cache->get( $key, $curTTL );
1388 $this->assertEquals( $value, $v, "Key was created with value" );
1389 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1390 }
1391
1392 /**
1393 * @dataProvider getWithSetCallback_versions_provider
1394 * @covers WANObjectCache::getWithSetCallback()
1395 * @covers WANObjectCache::fetchOrRegenerate()
1396 * @param array $extOpts
1397 * @param bool $versioned
1398 */
1399 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1400 $cache = $this->cache;
1401
1402 $key = wfRandomString();
1403 $valueV1 = wfRandomString();
1404 $valueV2 = [ wfRandomString() ];
1405
1406 $wasSet = 0;
1407 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1408 ++$wasSet;
1409
1410 return $valueV1;
1411 };
1412
1413 $priorValue = false;
1414 $priorAsOf = null;
1415 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1416 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1417 $priorValue = $oldValue;
1418 $priorAsOf = $oldAsOf;
1419 ++$wasSet;
1420
1421 return $valueV2; // new array format
1422 };
1423
1424 // Set the main key (version N if versioned)
1425 $wasSet = 0;
1426 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1427 $this->assertEquals( $valueV1, $v, "Value returned" );
1428 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1429 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1430 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1431 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1432
1433 if ( $versioned ) {
1434 // Set the key for version N+1 format
1435 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1436 } else {
1437 // Start versioning now with the unversioned key still there
1438 $verOpts = [ 'version' => 1 ];
1439 }
1440
1441 // Value goes to secondary key since V1 already used $key
1442 $wasSet = 0;
1443 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1444 $this->assertEquals( $valueV2, $v, "Value returned" );
1445 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1446 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1447 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1448
1449 $wasSet = 0;
1450 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1451 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1452 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1453
1454 // Clear out the older or unversioned key
1455 $cache->delete( $key, 0 );
1456
1457 // Set the key for next/first versioned format
1458 $wasSet = 0;
1459 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1460 $this->assertEquals( $valueV2, $v, "Value returned" );
1461 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1462
1463 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1464 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1465 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1466 }
1467
1468 public static function getWithSetCallback_versions_provider() {
1469 return [
1470 [ [], false ],
1471 [ [ 'version' => 1 ], true ]
1472 ];
1473 }
1474
1475 /**
1476 * @covers WANObjectCache::useInterimHoldOffCaching
1477 * @covers WANObjectCache::getInterimValue
1478 */
1479 public function testInterimHoldOffCaching() {
1480 $cache = $this->cache;
1481
1482 $mockWallClock = 1549343530.2053;
1483 $cache->setMockTime( $mockWallClock );
1484
1485 $value = 'CRL-40-940';
1486 $wasCalled = 0;
1487 $func = function () use ( &$wasCalled, $value ) {
1488 $wasCalled++;
1489
1490 return $value;
1491 };
1492
1493 $cache->useInterimHoldOffCaching( true );
1494
1495 $key = wfRandomString( 32 );
1496 $v = $cache->getWithSetCallback( $key, 60, $func );
1497 $v = $cache->getWithSetCallback( $key, 60, $func );
1498 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1499
1500 $cache->delete( $key );
1501 $mockWallClock += 0.001; // cached values will be newer than tombstone
1502 $v = $cache->getWithSetCallback( $key, 60, $func );
1503 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1504 $v = $cache->getWithSetCallback( $key, 60, $func );
1505 $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
1506
1507 $mockWallClock += 0.2; // interim key not brand new
1508 $v = $cache->getWithSetCallback( $key, 60, $func );
1509 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1510 // Lock up the mutex so interim cache is used
1511 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1512 $v = $cache->getWithSetCallback( $key, 60, $func );
1513 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1514 $this->internalCache->delete( 'WANCache:m:' . $key );
1515
1516 $cache->useInterimHoldOffCaching( false );
1517
1518 $wasCalled = 0;
1519 $key = wfRandomString( 32 );
1520 $v = $cache->getWithSetCallback( $key, 60, $func );
1521 $v = $cache->getWithSetCallback( $key, 60, $func );
1522 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1523 $cache->delete( $key );
1524 $v = $cache->getWithSetCallback( $key, 60, $func );
1525 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1526 $v = $cache->getWithSetCallback( $key, 60, $func );
1527 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1528 $v = $cache->getWithSetCallback( $key, 60, $func );
1529 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1530 // Lock up the mutex so interim cache is used
1531 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1532 $v = $cache->getWithSetCallback( $key, 60, $func );
1533 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1534 }
1535
1536 /**
1537 * @covers WANObjectCache::touchCheckKey
1538 * @covers WANObjectCache::resetCheckKey
1539 * @covers WANObjectCache::getCheckKeyTime
1540 * @covers WANObjectCache::getMultiCheckKeyTime
1541 * @covers WANObjectCache::makePurgeValue
1542 * @covers WANObjectCache::parsePurgeValue
1543 */
1544 public function testTouchKeys() {
1545 $cache = $this->cache;
1546 $key = wfRandomString();
1547
1548 $mockWallClock = 1549343530.2053;
1549 $priorTime = $mockWallClock; // reference time
1550 $cache->setMockTime( $mockWallClock );
1551
1552 $mockWallClock += 0.100;
1553 $t0 = $cache->getCheckKeyTime( $key );
1554 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1555
1556 $priorTime = $mockWallClock;
1557 $mockWallClock += 0.100;
1558 $cache->touchCheckKey( $key );
1559 $t1 = $cache->getCheckKeyTime( $key );
1560 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1561
1562 $t2 = $cache->getCheckKeyTime( $key );
1563 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1564
1565 $mockWallClock += 0.100;
1566 $cache->touchCheckKey( $key );
1567 $t3 = $cache->getCheckKeyTime( $key );
1568 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1569
1570 $t4 = $cache->getCheckKeyTime( $key );
1571 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1572
1573 $mockWallClock += 0.100;
1574 $cache->resetCheckKey( $key );
1575 $t5 = $cache->getCheckKeyTime( $key );
1576 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1577
1578 $t6 = $cache->getCheckKeyTime( $key );
1579 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1580 }
1581
1582 /**
1583 * @covers WANObjectCache::getMulti()
1584 */
1585 public function testGetWithSeveralCheckKeys() {
1586 $key = wfRandomString();
1587 $tKey1 = wfRandomString();
1588 $tKey2 = wfRandomString();
1589 $value = 'meow';
1590
1591 $mockWallClock = 1549343530.2053;
1592 $priorTime = $mockWallClock; // reference time
1593 $this->cache->setMockTime( $mockWallClock );
1594
1595 // Two check keys are newer (given hold-off) than $key, another is older
1596 $this->internalCache->set(
1597 'WANCache:t:' . $tKey2,
1598 'PURGED:' . ( $priorTime - 3 )
1599 );
1600 $this->internalCache->set(
1601 'WANCache:t:' . $tKey2,
1602 'PURGED:' . ( $priorTime - 5 )
1603 );
1604 $this->internalCache->set(
1605 'WANCache:t:' . $tKey1,
1606 'PURGED:' . ( $priorTime - 30 )
1607 );
1608 $this->cache->set( $key, $value, 30 );
1609
1610 $curTTL = null;
1611 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1612 $this->assertEquals( $value, $v, "Value matches" );
1613 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1614 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1615 }
1616
1617 /**
1618 * @covers WANObjectCache::reap()
1619 * @covers WANObjectCache::reapCheckKey()
1620 */
1621 public function testReap() {
1622 $vKey1 = wfRandomString();
1623 $vKey2 = wfRandomString();
1624 $tKey1 = wfRandomString();
1625 $tKey2 = wfRandomString();
1626 $value = 'moo';
1627
1628 $knownPurge = time() - 60;
1629 $goodTime = microtime( true ) - 5;
1630 $badTime = microtime( true ) - 300;
1631
1632 $this->internalCache->set(
1633 'WANCache:v:' . $vKey1,
1634 [
1635 0 => 1,
1636 1 => $value,
1637 2 => 3600,
1638 3 => $goodTime
1639 ]
1640 );
1641 $this->internalCache->set(
1642 'WANCache:v:' . $vKey2,
1643 [
1644 0 => 1,
1645 1 => $value,
1646 2 => 3600,
1647 3 => $badTime
1648 ]
1649 );
1650 $this->internalCache->set(
1651 'WANCache:t:' . $tKey1,
1652 'PURGED:' . $goodTime
1653 );
1654 $this->internalCache->set(
1655 'WANCache:t:' . $tKey2,
1656 'PURGED:' . $badTime
1657 );
1658
1659 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1660 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1661 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1662 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1663
1664 $this->assertFalse( $bad1 );
1665 $this->assertTrue( $bad2 );
1666
1667 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1668 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1669 $this->assertFalse( $tBad1 );
1670 $this->assertTrue( $tBad2 );
1671 }
1672
1673 /**
1674 * @covers WANObjectCache::reap()
1675 */
1676 public function testReap_fail() {
1677 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1678 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1679 $backend->expects( $this->once() )->method( 'get' )
1680 ->willReturn( [
1681 0 => 1,
1682 1 => 'value',
1683 2 => 3600,
1684 3 => 300,
1685 ] );
1686 $backend->expects( $this->once() )->method( 'changeTTL' )
1687 ->willReturn( false );
1688
1689 $wanCache = new WANObjectCache( [
1690 'cache' => $backend
1691 ] );
1692
1693 $isStale = null;
1694 $ret = $wanCache->reap( 'key', 360, $isStale );
1695 $this->assertTrue( $isStale, 'value was stale' );
1696 $this->assertFalse( $ret, 'changeTTL failed' );
1697 }
1698
1699 /**
1700 * @covers WANObjectCache::set()
1701 */
1702 public function testSetWithLag() {
1703 $value = 1;
1704
1705 $key = wfRandomString();
1706 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1707 $this->cache->set( $key, $value, 30, $opts );
1708 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1709
1710 $key = wfRandomString();
1711 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1712 $this->cache->set( $key, $value, 30, $opts );
1713 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1714
1715 $key = wfRandomString();
1716 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1717 $this->cache->set( $key, $value, 30, $opts );
1718 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1719 }
1720
1721 /**
1722 * @covers WANObjectCache::set()
1723 */
1724 public function testWritePending() {
1725 $value = 1;
1726
1727 $key = wfRandomString();
1728 $opts = [ 'pending' => true ];
1729 $this->cache->set( $key, $value, 30, $opts );
1730 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1731 }
1732
1733 public function testMcRouterSupport() {
1734 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1735 ->setMethods( [ 'set', 'delete' ] )->getMock();
1736 $localBag->expects( $this->never() )->method( 'set' );
1737 $localBag->expects( $this->never() )->method( 'delete' );
1738 $wanCache = new WANObjectCache( [
1739 'cache' => $localBag,
1740 'mcrouterAware' => true,
1741 'region' => 'pmtpa',
1742 'cluster' => 'mw-wan'
1743 ] );
1744 $valFunc = function () {
1745 return 1;
1746 };
1747
1748 // None of these should use broadcasting commands (e.g. SET, DELETE)
1749 $wanCache->get( 'x' );
1750 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1751 $wanCache->getMulti( [ 'x', 'y' ] );
1752 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1753 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1754 $wanCache->getCheckKeyTime( 'zzz' );
1755 $wanCache->reap( 'x', time() - 300 );
1756 $wanCache->reap( 'zzz', time() - 300 );
1757 }
1758
1759 public function testMcRouterSupportBroadcastDelete() {
1760 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1761 ->setMethods( [ 'set' ] )->getMock();
1762 $wanCache = new WANObjectCache( [
1763 'cache' => $localBag,
1764 'mcrouterAware' => true,
1765 'region' => 'pmtpa',
1766 'cluster' => 'mw-wan'
1767 ] );
1768
1769 $localBag->expects( $this->once() )->method( 'set' )
1770 ->with( "/*/mw-wan/" . 'WANCache:v:' . "test" );
1771
1772 $wanCache->delete( 'test' );
1773 }
1774
1775 public function testMcRouterSupportBroadcastTouchCK() {
1776 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1777 ->setMethods( [ 'set' ] )->getMock();
1778 $wanCache = new WANObjectCache( [
1779 'cache' => $localBag,
1780 'mcrouterAware' => true,
1781 'region' => 'pmtpa',
1782 'cluster' => 'mw-wan'
1783 ] );
1784
1785 $localBag->expects( $this->once() )->method( 'set' )
1786 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1787
1788 $wanCache->touchCheckKey( 'test' );
1789 }
1790
1791 public function testMcRouterSupportBroadcastResetCK() {
1792 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1793 ->setMethods( [ 'delete' ] )->getMock();
1794 $wanCache = new WANObjectCache( [
1795 'cache' => $localBag,
1796 'mcrouterAware' => true,
1797 'region' => 'pmtpa',
1798 'cluster' => 'mw-wan'
1799 ] );
1800
1801 $localBag->expects( $this->once() )->method( 'delete' )
1802 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1803
1804 $wanCache->resetCheckKey( 'test' );
1805 }
1806
1807 public function testEpoch() {
1808 $bag = new HashBagOStuff();
1809 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1810 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1811
1812 $now = microtime( true );
1813 $cache->setMockTime( $now );
1814
1815 $cache->set( $key, 'Do what thou Wilt' );
1816 $cache->touchCheckKey( $key );
1817
1818 $then = $now;
1819 $now += 30;
1820 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1821 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1822
1823 $cache = new WANObjectCache( [
1824 'cache' => $bag,
1825 'epoch' => $now - 3600
1826 ] );
1827 $cache->setMockTime( $now );
1828
1829 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1830 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1831
1832 $now += 30;
1833 $cache = new WANObjectCache( [
1834 'cache' => $bag,
1835 'epoch' => $now + 3600
1836 ] );
1837 $cache->setMockTime( $now );
1838
1839 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1840 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1841 }
1842
1843 /**
1844 * @dataProvider provideAdaptiveTTL
1845 * @covers WANObjectCache::adaptiveTTL()
1846 * @param float|int $ago
1847 * @param int $maxTTL
1848 * @param int $minTTL
1849 * @param float $factor
1850 * @param int $adaptiveTTL
1851 */
1852 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1853 $mtime = $ago ? time() - $ago : $ago;
1854 $margin = 5;
1855 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1856
1857 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1858 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1859
1860 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1861
1862 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1863 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1864 }
1865
1866 public static function provideAdaptiveTTL() {
1867 return [
1868 [ 3600, 900, 30, 0.2, 720 ],
1869 [ 3600, 500, 30, 0.2, 500 ],
1870 [ 3600, 86400, 800, 0.2, 800 ],
1871 [ false, 86400, 800, 0.2, 800 ],
1872 [ null, 86400, 800, 0.2, 800 ]
1873 ];
1874 }
1875
1876 /**
1877 * @covers WANObjectCache::__construct
1878 * @covers WANObjectCache::newEmpty
1879 */
1880 public function testNewEmpty() {
1881 $this->assertInstanceOf(
1882 WANObjectCache::class,
1883 WANObjectCache::newEmpty()
1884 );
1885 }
1886
1887 /**
1888 * @covers WANObjectCache::setLogger
1889 */
1890 public function testSetLogger() {
1891 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1892 }
1893
1894 /**
1895 * @covers WANObjectCache::getQoS
1896 */
1897 public function testGetQoS() {
1898 $backend = $this->getMockBuilder( HashBagOStuff::class )
1899 ->setMethods( [ 'getQoS' ] )->getMock();
1900 $backend->expects( $this->once() )->method( 'getQoS' )
1901 ->willReturn( BagOStuff::QOS_UNKNOWN );
1902 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1903
1904 $this->assertSame(
1905 $wanCache::QOS_UNKNOWN,
1906 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1907 );
1908 }
1909
1910 /**
1911 * @covers WANObjectCache::makeKey
1912 */
1913 public function testMakeKey() {
1914 if ( defined( 'HHVM_VERSION' ) ) {
1915 $this->markTestSkipped( 'HHVM Reflection buggy' );
1916 }
1917
1918 $backend = $this->getMockBuilder( HashBagOStuff::class )
1919 ->setMethods( [ 'makeKey' ] )->getMock();
1920 $backend->expects( $this->once() )->method( 'makeKey' )
1921 ->willReturn( 'special' );
1922
1923 $wanCache = new WANObjectCache( [
1924 'cache' => $backend
1925 ] );
1926
1927 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1928 }
1929
1930 /**
1931 * @covers WANObjectCache::makeGlobalKey
1932 */
1933 public function testMakeGlobalKey() {
1934 if ( defined( 'HHVM_VERSION' ) ) {
1935 $this->markTestSkipped( 'HHVM Reflection buggy' );
1936 }
1937
1938 $backend = $this->getMockBuilder( HashBagOStuff::class )
1939 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1940 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1941 ->willReturn( 'special' );
1942
1943 $wanCache = new WANObjectCache( [
1944 'cache' => $backend
1945 ] );
1946
1947 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1948 }
1949
1950 public static function statsKeyProvider() {
1951 return [
1952 [ 'domain:page:5', 'page' ],
1953 [ 'domain:main-key', 'main-key' ],
1954 [ 'domain:page:history', 'page' ],
1955 [ 'missingdomainkey', 'missingdomainkey' ]
1956 ];
1957 }
1958
1959 /**
1960 * @dataProvider statsKeyProvider
1961 * @covers WANObjectCache::determineKeyClassForStats
1962 */
1963 public function testStatsKeyClass( $key, $class ) {
1964 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1965 'cache' => new HashBagOStuff
1966 ] ) );
1967
1968 $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
1969 }
1970
1971 /**
1972 * @covers WANObjectCache::makeMultiKeys
1973 */
1974 public function testMakeMultiKeys() {
1975 $cache = $this->cache;
1976
1977 $ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
1978 $keyCallback = function ( $id, WANObjectCache $cache ) {
1979 return $cache->makeKey( 'key', $id );
1980 };
1981 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1982
1983 $expected = [
1984 "local:key:1" => 1,
1985 "local:key:2" => 2,
1986 "local:key:3" => 3,
1987 "local:key:4" => 4,
1988 "local:key:5" => 5,
1989 "local:key:6" => 6,
1990 "local:key:7" => 7
1991 ];
1992 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1993
1994 $ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
1995 $keyCallback = function ( $id, WANObjectCache $cache ) {
1996 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
1997 };
1998 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1999
2000 $expected = [
2001 "global:key:1:a:1:b" => '1',
2002 "global:key:2:a:2:b" => '2',
2003 "global:key:3:a:3:b" => '3',
2004 "global:key:4:a:4:b" => '4',
2005 "global:key:5:a:5:b" => '5',
2006 "global:key:6:a:6:b" => '6',
2007 "global:key:7:a:7:b" => '7'
2008 ];
2009 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2010 }
2011
2012 /**
2013 * @covers WANObjectCache::makeMultiKeys
2014 */
2015 public function testMakeMultiKeysIntString() {
2016 $cache = $this->cache;
2017 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
2018 $keyCallback = function ( $id, WANObjectCache $cache ) {
2019 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
2020 };
2021
2022 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2023
2024 $expected = [
2025 "global:key:1:a:1:b" => 1,
2026 "global:key:2:a:2:b" => 2,
2027 "global:key:3:a:3:b" => 3,
2028 "global:key:4:a:4:b" => 4,
2029 "global:key:5:a:5:b" => 5,
2030 "global:key:6:a:6:b" => 6,
2031 "global:key:7:a:7:b" => 7
2032 ];
2033 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2034 }
2035
2036 /**
2037 * @covers WANObjectCache::makeMultiKeys
2038 * @expectedException UnexpectedValueException
2039 */
2040 public function testMakeMultiKeysCollision() {
2041 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
2042
2043 $this->cache->makeMultiKeys(
2044 $ids,
2045 function ( $id ) {
2046 return "keymod:" . $id % 3;
2047 }
2048 );
2049 }
2050
2051 /**
2052 * @covers WANObjectCache::multiRemap
2053 */
2054 public function testMultiRemap() {
2055 $a = [ 'a', 'b', 'c' ];
2056 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
2057
2058 $this->assertEquals(
2059 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
2060 $this->cache->multiRemap( $a, $res )
2061 );
2062
2063 $a = [ 'a', 'b', 'c', 'c', 'd' ];
2064 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3, 'keyD' => 4 ];
2065
2066 $this->assertEquals(
2067 [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ],
2068 $this->cache->multiRemap( $a, $res )
2069 );
2070 }
2071
2072 /**
2073 * @covers WANObjectCache::hash256
2074 */
2075 public function testHash256() {
2076 $bag = new HashBagOStuff();
2077 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 5 ] );
2078 $this->assertEquals(
2079 'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
2080 $cache->hash256( 'x' )
2081 );
2082
2083 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 50 ] );
2084 $this->assertEquals(
2085 'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
2086 $cache->hash256( 'x' )
2087 );
2088
2089 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden' ] );
2090 $this->assertEquals(
2091 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2092 $cache->hash256( 'x' )
2093 );
2094
2095 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden', 'epoch' => 3 ] );
2096 $this->assertEquals(
2097 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2098 $cache->hash256( 'x' )
2099 );
2100 }
2101 }
2102
2103 class NearExpiringWANObjectCache extends WANObjectCache {
2104 const CLOCK_SKEW = 1;
2105
2106 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
2107 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
2108 }
2109 }
2110
2111 class PopularityRefreshingWANObjectCache extends WANObjectCache {
2112 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
2113 return ( ( $now - $asOf ) > $timeTillRefresh );
2114 }
2115 }