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