.pipeline/config.yaml: rename dev stage to publish
[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->assertSame( 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->assertSame( 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->assertSame( 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->assertSame( 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->assertSame( 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->assertSame( 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::get()
1353 * @covers WANObjectCache::processCheckKeys()
1354 */
1355 public function testCheckKeyHoldoff() {
1356 $cache = $this->cache;
1357 $key = wfRandomString();
1358 $checkKey = wfRandomString();
1359
1360 $mockWallClock = 1549343530.2053;
1361 $cache->setMockTime( $mockWallClock );
1362 $cache->touchCheckKey( $checkKey, 8 );
1363
1364 $mockWallClock += 1;
1365 $cache->set( $key, 1, 60 );
1366 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1367 $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
1368
1369 $mockWallClock += 3;
1370 $cache->set( $key, 1, 60 );
1371 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1372 $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
1373
1374 $mockWallClock += 10;
1375 $cache->set( $key, 1, 60 );
1376 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1377 $this->assertGreaterThan( 0, $curTTL, "Key not in hold-off due to check key" );
1378 }
1379
1380 /**
1381 * @covers WANObjectCache::delete
1382 * @covers WANObjectCache::relayDelete
1383 * @covers WANObjectCache::relayPurge
1384 */
1385 public function testDelete() {
1386 $key = wfRandomString();
1387 $value = wfRandomString();
1388 $this->cache->set( $key, $value );
1389
1390 $curTTL = null;
1391 $v = $this->cache->get( $key, $curTTL );
1392 $this->assertEquals( $value, $v, "Key was created with value" );
1393 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1394
1395 $this->cache->delete( $key );
1396
1397 $curTTL = null;
1398 $v = $this->cache->get( $key, $curTTL );
1399 $this->assertFalse( $v, "Deleted key has false value" );
1400 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1401
1402 $this->cache->set( $key, $value . 'more' );
1403 $v = $this->cache->get( $key, $curTTL );
1404 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1405 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1406
1407 $this->cache->set( $key, $value );
1408 $this->cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE );
1409
1410 $curTTL = null;
1411 $v = $this->cache->get( $key, $curTTL );
1412 $this->assertFalse( $v, "Deleted key has false value" );
1413 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1414
1415 $this->cache->set( $key, $value );
1416 $v = $this->cache->get( $key, $curTTL );
1417 $this->assertEquals( $value, $v, "Key was created with value" );
1418 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1419 }
1420
1421 /**
1422 * @dataProvider getWithSetCallback_versions_provider
1423 * @covers WANObjectCache::getWithSetCallback()
1424 * @covers WANObjectCache::fetchOrRegenerate()
1425 * @param array $extOpts
1426 * @param bool $versioned
1427 */
1428 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1429 $cache = $this->cache;
1430
1431 $key = wfRandomString();
1432 $valueV1 = wfRandomString();
1433 $valueV2 = [ wfRandomString() ];
1434
1435 $wasSet = 0;
1436 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1437 ++$wasSet;
1438
1439 return $valueV1;
1440 };
1441
1442 $priorValue = false;
1443 $priorAsOf = null;
1444 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1445 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1446 $priorValue = $oldValue;
1447 $priorAsOf = $oldAsOf;
1448 ++$wasSet;
1449
1450 return $valueV2; // new array format
1451 };
1452
1453 // Set the main key (version N if versioned)
1454 $wasSet = 0;
1455 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1456 $this->assertEquals( $valueV1, $v, "Value returned" );
1457 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1458 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1459 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1460 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1461
1462 if ( $versioned ) {
1463 // Set the key for version N+1 format
1464 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1465 } else {
1466 // Start versioning now with the unversioned key still there
1467 $verOpts = [ 'version' => 1 ];
1468 }
1469
1470 // Value goes to secondary key since V1 already used $key
1471 $wasSet = 0;
1472 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1473 $this->assertEquals( $valueV2, $v, "Value returned" );
1474 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1475 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1476 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1477
1478 $wasSet = 0;
1479 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1480 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1481 $this->assertSame( 0, $wasSet, "Value not regenerated (secondary key)" );
1482
1483 // Clear out the older or unversioned key
1484 $cache->delete( $key, 0 );
1485
1486 // Set the key for next/first versioned format
1487 $wasSet = 0;
1488 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1489 $this->assertEquals( $valueV2, $v, "Value returned" );
1490 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1491
1492 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1493 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1494 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1495 }
1496
1497 public static function getWithSetCallback_versions_provider() {
1498 return [
1499 [ [], false ],
1500 [ [ 'version' => 1 ], true ]
1501 ];
1502 }
1503
1504 /**
1505 * @covers WANObjectCache::useInterimHoldOffCaching
1506 * @covers WANObjectCache::getInterimValue
1507 */
1508 public function testInterimHoldOffCaching() {
1509 $cache = $this->cache;
1510
1511 $mockWallClock = 1549343530.2053;
1512 $cache->setMockTime( $mockWallClock );
1513
1514 $value = 'CRL-40-940';
1515 $wasCalled = 0;
1516 $func = function () use ( &$wasCalled, $value ) {
1517 $wasCalled++;
1518
1519 return $value;
1520 };
1521
1522 $cache->useInterimHoldOffCaching( true );
1523
1524 $key = wfRandomString( 32 );
1525 $v = $cache->getWithSetCallback( $key, 60, $func );
1526 $v = $cache->getWithSetCallback( $key, 60, $func );
1527 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1528
1529 $cache->delete( $key );
1530 $mockWallClock += 0.001; // cached values will be newer than tombstone
1531 $v = $cache->getWithSetCallback( $key, 60, $func );
1532 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1533 $v = $cache->getWithSetCallback( $key, 60, $func );
1534 $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
1535
1536 $mockWallClock += 0.2; // interim key not brand new
1537 $v = $cache->getWithSetCallback( $key, 60, $func );
1538 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1539 // Lock up the mutex so interim cache is used
1540 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1541 $v = $cache->getWithSetCallback( $key, 60, $func );
1542 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1543 $this->internalCache->delete( 'WANCache:m:' . $key );
1544
1545 $cache->useInterimHoldOffCaching( false );
1546
1547 $wasCalled = 0;
1548 $key = wfRandomString( 32 );
1549 $v = $cache->getWithSetCallback( $key, 60, $func );
1550 $v = $cache->getWithSetCallback( $key, 60, $func );
1551 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1552 $cache->delete( $key );
1553 $v = $cache->getWithSetCallback( $key, 60, $func );
1554 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1555 $v = $cache->getWithSetCallback( $key, 60, $func );
1556 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1557 $v = $cache->getWithSetCallback( $key, 60, $func );
1558 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1559 // Lock up the mutex so interim cache is used
1560 $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 );
1561 $v = $cache->getWithSetCallback( $key, 60, $func );
1562 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1563 }
1564
1565 /**
1566 * @covers WANObjectCache::touchCheckKey
1567 * @covers WANObjectCache::resetCheckKey
1568 * @covers WANObjectCache::getCheckKeyTime
1569 * @covers WANObjectCache::getMultiCheckKeyTime
1570 * @covers WANObjectCache::makePurgeValue
1571 * @covers WANObjectCache::parsePurgeValue
1572 */
1573 public function testTouchKeys() {
1574 $cache = $this->cache;
1575 $key = wfRandomString();
1576
1577 $mockWallClock = 1549343530.2053;
1578 $priorTime = $mockWallClock; // reference time
1579 $cache->setMockTime( $mockWallClock );
1580
1581 $mockWallClock += 0.100;
1582 $t0 = $cache->getCheckKeyTime( $key );
1583 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1584
1585 $priorTime = $mockWallClock;
1586 $mockWallClock += 0.100;
1587 $cache->touchCheckKey( $key );
1588 $t1 = $cache->getCheckKeyTime( $key );
1589 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1590
1591 $t2 = $cache->getCheckKeyTime( $key );
1592 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1593
1594 $mockWallClock += 0.100;
1595 $cache->touchCheckKey( $key );
1596 $t3 = $cache->getCheckKeyTime( $key );
1597 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1598
1599 $t4 = $cache->getCheckKeyTime( $key );
1600 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1601
1602 $mockWallClock += 0.100;
1603 $cache->resetCheckKey( $key );
1604 $t5 = $cache->getCheckKeyTime( $key );
1605 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1606
1607 $t6 = $cache->getCheckKeyTime( $key );
1608 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1609 }
1610
1611 /**
1612 * @covers WANObjectCache::getMulti()
1613 */
1614 public function testGetWithSeveralCheckKeys() {
1615 $key = wfRandomString();
1616 $tKey1 = wfRandomString();
1617 $tKey2 = wfRandomString();
1618 $value = 'meow';
1619
1620 $mockWallClock = 1549343530.2053;
1621 $priorTime = $mockWallClock; // reference time
1622 $this->cache->setMockTime( $mockWallClock );
1623
1624 // Two check keys are newer (given hold-off) than $key, another is older
1625 $this->internalCache->set(
1626 'WANCache:t:' . $tKey2,
1627 'PURGED:' . ( $priorTime - 3 )
1628 );
1629 $this->internalCache->set(
1630 'WANCache:t:' . $tKey2,
1631 'PURGED:' . ( $priorTime - 5 )
1632 );
1633 $this->internalCache->set(
1634 'WANCache:t:' . $tKey1,
1635 'PURGED:' . ( $priorTime - 30 )
1636 );
1637 $this->cache->set( $key, $value, 30 );
1638
1639 $curTTL = null;
1640 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1641 $this->assertEquals( $value, $v, "Value matches" );
1642 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1643 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1644 }
1645
1646 /**
1647 * @covers WANObjectCache::reap()
1648 * @covers WANObjectCache::reapCheckKey()
1649 */
1650 public function testReap() {
1651 $vKey1 = wfRandomString();
1652 $vKey2 = wfRandomString();
1653 $tKey1 = wfRandomString();
1654 $tKey2 = wfRandomString();
1655 $value = 'moo';
1656
1657 $knownPurge = time() - 60;
1658 $goodTime = microtime( true ) - 5;
1659 $badTime = microtime( true ) - 300;
1660
1661 $this->internalCache->set(
1662 'WANCache:v:' . $vKey1,
1663 [
1664 0 => 1,
1665 1 => $value,
1666 2 => 3600,
1667 3 => $goodTime
1668 ]
1669 );
1670 $this->internalCache->set(
1671 'WANCache:v:' . $vKey2,
1672 [
1673 0 => 1,
1674 1 => $value,
1675 2 => 3600,
1676 3 => $badTime
1677 ]
1678 );
1679 $this->internalCache->set(
1680 'WANCache:t:' . $tKey1,
1681 'PURGED:' . $goodTime
1682 );
1683 $this->internalCache->set(
1684 'WANCache:t:' . $tKey2,
1685 'PURGED:' . $badTime
1686 );
1687
1688 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1689 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1690 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1691 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1692
1693 $this->assertFalse( $bad1 );
1694 $this->assertTrue( $bad2 );
1695
1696 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1697 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1698 $this->assertFalse( $tBad1 );
1699 $this->assertTrue( $tBad2 );
1700 }
1701
1702 /**
1703 * @covers WANObjectCache::reap()
1704 */
1705 public function testReap_fail() {
1706 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1707 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1708 $backend->expects( $this->once() )->method( 'get' )
1709 ->willReturn( [
1710 0 => 1,
1711 1 => 'value',
1712 2 => 3600,
1713 3 => 300,
1714 ] );
1715 $backend->expects( $this->once() )->method( 'changeTTL' )
1716 ->willReturn( false );
1717
1718 $wanCache = new WANObjectCache( [
1719 'cache' => $backend
1720 ] );
1721
1722 $isStale = null;
1723 $ret = $wanCache->reap( 'key', 360, $isStale );
1724 $this->assertTrue( $isStale, 'value was stale' );
1725 $this->assertFalse( $ret, 'changeTTL failed' );
1726 }
1727
1728 /**
1729 * @covers WANObjectCache::set()
1730 */
1731 public function testSetWithLag() {
1732 $value = 1;
1733
1734 $key = wfRandomString();
1735 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1736 $this->cache->set( $key, $value, 30, $opts );
1737 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1738
1739 $key = wfRandomString();
1740 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1741 $this->cache->set( $key, $value, 30, $opts );
1742 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1743
1744 $key = wfRandomString();
1745 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1746 $this->cache->set( $key, $value, 30, $opts );
1747 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1748 }
1749
1750 /**
1751 * @covers WANObjectCache::set()
1752 */
1753 public function testWritePending() {
1754 $value = 1;
1755
1756 $key = wfRandomString();
1757 $opts = [ 'pending' => true ];
1758 $this->cache->set( $key, $value, 30, $opts );
1759 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1760 }
1761
1762 public function testMcRouterSupport() {
1763 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1764 ->setMethods( [ 'set', 'delete' ] )->getMock();
1765 $localBag->expects( $this->never() )->method( 'set' );
1766 $localBag->expects( $this->never() )->method( 'delete' );
1767 $wanCache = new WANObjectCache( [
1768 'cache' => $localBag,
1769 'mcrouterAware' => true,
1770 'region' => 'pmtpa',
1771 'cluster' => 'mw-wan'
1772 ] );
1773 $valFunc = function () {
1774 return 1;
1775 };
1776
1777 // None of these should use broadcasting commands (e.g. SET, DELETE)
1778 $wanCache->get( 'x' );
1779 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1780 $wanCache->getMulti( [ 'x', 'y' ] );
1781 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1782 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1783 $wanCache->getCheckKeyTime( 'zzz' );
1784 $wanCache->reap( 'x', time() - 300 );
1785 $wanCache->reap( 'zzz', time() - 300 );
1786 }
1787
1788 public function testMcRouterSupportBroadcastDelete() {
1789 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1790 ->setMethods( [ 'set' ] )->getMock();
1791 $wanCache = new WANObjectCache( [
1792 'cache' => $localBag,
1793 'mcrouterAware' => true,
1794 'region' => 'pmtpa',
1795 'cluster' => 'mw-wan'
1796 ] );
1797
1798 $localBag->expects( $this->once() )->method( 'set' )
1799 ->with( "/*/mw-wan/" . 'WANCache:v:' . "test" );
1800
1801 $wanCache->delete( 'test' );
1802 }
1803
1804 public function testMcRouterSupportBroadcastTouchCK() {
1805 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1806 ->setMethods( [ 'set' ] )->getMock();
1807 $wanCache = new WANObjectCache( [
1808 'cache' => $localBag,
1809 'mcrouterAware' => true,
1810 'region' => 'pmtpa',
1811 'cluster' => 'mw-wan'
1812 ] );
1813
1814 $localBag->expects( $this->once() )->method( 'set' )
1815 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1816
1817 $wanCache->touchCheckKey( 'test' );
1818 }
1819
1820 public function testMcRouterSupportBroadcastResetCK() {
1821 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1822 ->setMethods( [ 'delete' ] )->getMock();
1823 $wanCache = new WANObjectCache( [
1824 'cache' => $localBag,
1825 'mcrouterAware' => true,
1826 'region' => 'pmtpa',
1827 'cluster' => 'mw-wan'
1828 ] );
1829
1830 $localBag->expects( $this->once() )->method( 'delete' )
1831 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1832
1833 $wanCache->resetCheckKey( 'test' );
1834 }
1835
1836 public function testEpoch() {
1837 $bag = new HashBagOStuff();
1838 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1839 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1840
1841 $now = microtime( true );
1842 $cache->setMockTime( $now );
1843
1844 $cache->set( $key, 'Do what thou Wilt' );
1845 $cache->touchCheckKey( $key );
1846
1847 $then = $now;
1848 $now += 30;
1849 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1850 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1851
1852 $cache = new WANObjectCache( [
1853 'cache' => $bag,
1854 'epoch' => $now - 3600
1855 ] );
1856 $cache->setMockTime( $now );
1857
1858 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1859 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1860
1861 $now += 30;
1862 $cache = new WANObjectCache( [
1863 'cache' => $bag,
1864 'epoch' => $now + 3600
1865 ] );
1866 $cache->setMockTime( $now );
1867
1868 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1869 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1870 }
1871
1872 /**
1873 * @dataProvider provideAdaptiveTTL
1874 * @covers WANObjectCache::adaptiveTTL()
1875 * @param float|int $ago
1876 * @param int $maxTTL
1877 * @param int $minTTL
1878 * @param float $factor
1879 * @param int $adaptiveTTL
1880 */
1881 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1882 $mtime = $ago ? time() - $ago : $ago;
1883 $margin = 5;
1884 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1885
1886 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1887 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1888
1889 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1890
1891 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1892 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1893 }
1894
1895 public static function provideAdaptiveTTL() {
1896 return [
1897 [ 3600, 900, 30, 0.2, 720 ],
1898 [ 3600, 500, 30, 0.2, 500 ],
1899 [ 3600, 86400, 800, 0.2, 800 ],
1900 [ false, 86400, 800, 0.2, 800 ],
1901 [ null, 86400, 800, 0.2, 800 ]
1902 ];
1903 }
1904
1905 /**
1906 * @covers WANObjectCache::__construct
1907 * @covers WANObjectCache::newEmpty
1908 */
1909 public function testNewEmpty() {
1910 $this->assertInstanceOf(
1911 WANObjectCache::class,
1912 WANObjectCache::newEmpty()
1913 );
1914 }
1915
1916 /**
1917 * @covers WANObjectCache::setLogger
1918 */
1919 public function testSetLogger() {
1920 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1921 }
1922
1923 /**
1924 * @covers WANObjectCache::getQoS
1925 */
1926 public function testGetQoS() {
1927 $backend = $this->getMockBuilder( HashBagOStuff::class )
1928 ->setMethods( [ 'getQoS' ] )->getMock();
1929 $backend->expects( $this->once() )->method( 'getQoS' )
1930 ->willReturn( BagOStuff::QOS_UNKNOWN );
1931 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1932
1933 $this->assertSame(
1934 $wanCache::QOS_UNKNOWN,
1935 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1936 );
1937 }
1938
1939 /**
1940 * @covers WANObjectCache::makeKey
1941 */
1942 public function testMakeKey() {
1943 if ( defined( 'HHVM_VERSION' ) ) {
1944 $this->markTestSkipped( 'HHVM Reflection buggy' );
1945 }
1946
1947 $backend = $this->getMockBuilder( HashBagOStuff::class )
1948 ->setMethods( [ 'makeKey' ] )->getMock();
1949 $backend->expects( $this->once() )->method( 'makeKey' )
1950 ->willReturn( 'special' );
1951
1952 $wanCache = new WANObjectCache( [
1953 'cache' => $backend
1954 ] );
1955
1956 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1957 }
1958
1959 /**
1960 * @covers WANObjectCache::makeGlobalKey
1961 */
1962 public function testMakeGlobalKey() {
1963 if ( defined( 'HHVM_VERSION' ) ) {
1964 $this->markTestSkipped( 'HHVM Reflection buggy' );
1965 }
1966
1967 $backend = $this->getMockBuilder( HashBagOStuff::class )
1968 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1969 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1970 ->willReturn( 'special' );
1971
1972 $wanCache = new WANObjectCache( [
1973 'cache' => $backend
1974 ] );
1975
1976 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1977 }
1978
1979 public static function statsKeyProvider() {
1980 return [
1981 [ 'domain:page:5', 'page' ],
1982 [ 'domain:main-key', 'main-key' ],
1983 [ 'domain:page:history', 'page' ],
1984 // Regression test for T232907
1985 [ 'domain:foo-bar-1.2:abc:v2', 'foo-bar-1_2' ],
1986 [ 'missingdomainkey', 'missingdomainkey' ]
1987 ];
1988 }
1989
1990 /**
1991 * @dataProvider statsKeyProvider
1992 * @covers WANObjectCache::determineKeyClassForStats
1993 */
1994 public function testStatsKeyClass( $key, $class ) {
1995 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1996 'cache' => new HashBagOStuff
1997 ] ) );
1998
1999 $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
2000 }
2001
2002 /**
2003 * @covers WANObjectCache::makeMultiKeys
2004 */
2005 public function testMakeMultiKeys() {
2006 $cache = $this->cache;
2007
2008 $ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
2009 $keyCallback = function ( $id, WANObjectCache $cache ) {
2010 return $cache->makeKey( 'key', $id );
2011 };
2012 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2013
2014 $expected = [
2015 "local:key:1" => 1,
2016 "local:key:2" => 2,
2017 "local:key:3" => 3,
2018 "local:key:4" => 4,
2019 "local:key:5" => 5,
2020 "local:key:6" => 6,
2021 "local:key:7" => 7
2022 ];
2023 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2024
2025 $ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
2026 $keyCallback = function ( $id, WANObjectCache $cache ) {
2027 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
2028 };
2029 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2030
2031 $expected = [
2032 "global:key:1:a:1:b" => '1',
2033 "global:key:2:a:2:b" => '2',
2034 "global:key:3:a:3:b" => '3',
2035 "global:key:4:a:4:b" => '4',
2036 "global:key:5:a:5:b" => '5',
2037 "global:key:6:a:6:b" => '6',
2038 "global:key:7:a:7:b" => '7'
2039 ];
2040 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2041 }
2042
2043 /**
2044 * @covers WANObjectCache::makeMultiKeys
2045 */
2046 public function testMakeMultiKeysIntString() {
2047 $cache = $this->cache;
2048 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
2049 $keyCallback = function ( $id, WANObjectCache $cache ) {
2050 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
2051 };
2052
2053 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2054
2055 $expected = [
2056 "global:key:1:a:1:b" => 1,
2057 "global:key:2:a:2:b" => 2,
2058 "global:key:3:a:3:b" => 3,
2059 "global:key:4:a:4:b" => 4,
2060 "global:key:5:a:5:b" => 5,
2061 "global:key:6:a:6:b" => 6,
2062 "global:key:7:a:7:b" => 7
2063 ];
2064 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2065 }
2066
2067 /**
2068 * @covers WANObjectCache::makeMultiKeys
2069 * @expectedException UnexpectedValueException
2070 */
2071 public function testMakeMultiKeysCollision() {
2072 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
2073
2074 $this->cache->makeMultiKeys(
2075 $ids,
2076 function ( $id ) {
2077 return "keymod:" . $id % 3;
2078 }
2079 );
2080 }
2081
2082 /**
2083 * @covers WANObjectCache::multiRemap
2084 */
2085 public function testMultiRemap() {
2086 $a = [ 'a', 'b', 'c' ];
2087 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
2088
2089 $this->assertEquals(
2090 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
2091 $this->cache->multiRemap( $a, $res )
2092 );
2093
2094 $a = [ 'a', 'b', 'c', 'c', 'd' ];
2095 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3, 'keyD' => 4 ];
2096
2097 $this->assertEquals(
2098 [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ],
2099 $this->cache->multiRemap( $a, $res )
2100 );
2101 }
2102
2103 /**
2104 * @covers WANObjectCache::hash256
2105 */
2106 public function testHash256() {
2107 $bag = new HashBagOStuff();
2108 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 5 ] );
2109 $this->assertEquals(
2110 'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
2111 $cache->hash256( 'x' )
2112 );
2113
2114 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 50 ] );
2115 $this->assertEquals(
2116 'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
2117 $cache->hash256( 'x' )
2118 );
2119
2120 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden' ] );
2121 $this->assertEquals(
2122 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2123 $cache->hash256( 'x' )
2124 );
2125
2126 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden', 'epoch' => 3 ] );
2127 $this->assertEquals(
2128 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2129 $cache->hash256( 'x' )
2130 );
2131 }
2132 }
2133
2134 class NearExpiringWANObjectCache extends WANObjectCache {
2135 const CLOCK_SKEW = 1;
2136
2137 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
2138 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
2139 }
2140 }
2141
2142 class PopularityRefreshingWANObjectCache extends WANObjectCache {
2143 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
2144 return ( ( $now - $asOf ) > $timeTillRefresh );
2145 }
2146 }