Merge "Add support for Argon2 password hashing"
[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 $value = wfRandomString();
915
916 $calls = 0;
917 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
918 ++$calls;
919 $setOpts['since'] = microtime( true ) - 10;
920 // Immediately kill any mutex rather than waiting a second
921 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
922 return $value;
923 };
924
925 // Value should be marked as stale due to snapshot lag
926 $curTTL = null;
927 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
928 $this->assertEquals( $value, $ret );
929 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
930 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
931 $this->assertEquals( 1, $calls, 'Value was generated' );
932
933 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
934 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
935 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
936 $this->assertEquals( $value, $ret );
937 $this->assertEquals( 1, $calls, 'Callback was not used' );
938 }
939
940 /**
941 * @covers WANObjectCache::getWithSetCallback()
942 * @covers WANObjectCache::doGetWithSetCallback()
943 */
944 public function testBusyValue() {
945 $cache = $this->cache;
946 $key = wfRandomString();
947 $value = wfRandomString();
948 $busyValue = wfRandomString();
949
950 $calls = 0;
951 $func = function () use ( &$calls, $value, $cache, $key ) {
952 ++$calls;
953 // Immediately kill any mutex rather than waiting a second
954 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
955 return $value;
956 };
957
958 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
959 $this->assertEquals( $value, $ret );
960 $this->assertEquals( 1, $calls, 'Value was populated' );
961
962 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
963 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
964
965 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
966 $ret = $cache->getWithSetCallback( $key, 30, $func,
967 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
968 $this->assertEquals( $value, $ret, 'Callback used' );
969 $this->assertEquals( 2, $calls, 'Callback used' );
970
971 $ret = $cache->getWithSetCallback( $key, 30, $func,
972 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
973 $this->assertEquals( $value, $ret, 'Old value used' );
974 $this->assertEquals( 2, $calls, 'Callback was not used' );
975
976 $cache->delete( $key ); // no value at all anymore and still locked
977 $ret = $cache->getWithSetCallback( $key, 30, $func,
978 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
979 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
980 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
981
982 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
983 $ret = $cache->getWithSetCallback( $key, 30, $func,
984 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
985 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
986 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
987
988 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
989 $ret = $cache->getWithSetCallback( $key, 30, $func,
990 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
991 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
992 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
993 }
994
995 /**
996 * @covers WANObjectCache::getMulti()
997 */
998 public function testGetMulti() {
999 $cache = $this->cache;
1000
1001 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1002 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1003
1004 $key1 = wfRandomString();
1005 $key2 = wfRandomString();
1006 $key3 = wfRandomString();
1007
1008 $mockWallClock = 1549343530.2053;
1009 $priorTime = $mockWallClock; // reference time
1010 $cache->setMockTime( $mockWallClock );
1011
1012 $cache->set( $key1, $value1, 5 );
1013 $cache->set( $key2, $value2, 10 );
1014
1015 $curTTLs = [];
1016 $this->assertEquals(
1017 [ $key1 => $value1, $key2 => $value2 ],
1018 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1019 'Result array populated'
1020 );
1021
1022 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1023 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1024 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1025
1026 $cKey1 = wfRandomString();
1027 $cKey2 = wfRandomString();
1028
1029 $mockWallClock += 1;
1030
1031 $curTTLs = [];
1032 $this->assertEquals(
1033 [ $key1 => $value1, $key2 => $value2 ],
1034 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1035 "Result array populated even with new check keys"
1036 );
1037 $t1 = $cache->getCheckKeyTime( $cKey1 );
1038 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1039 $t2 = $cache->getCheckKeyTime( $cKey2 );
1040 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1041 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1042 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1043 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1044
1045 $mockWallClock += 1;
1046
1047 $curTTLs = [];
1048 $this->assertEquals(
1049 [ $key1 => $value1, $key2 => $value2 ],
1050 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1051 "Result array still populated even with new check keys"
1052 );
1053 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1054 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1055 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1056 }
1057
1058 /**
1059 * @covers WANObjectCache::getMulti()
1060 * @covers WANObjectCache::processCheckKeys()
1061 */
1062 public function testGetMultiCheckKeys() {
1063 $cache = $this->cache;
1064
1065 $checkAll = wfRandomString();
1066 $check1 = wfRandomString();
1067 $check2 = wfRandomString();
1068 $check3 = wfRandomString();
1069 $value1 = wfRandomString();
1070 $value2 = wfRandomString();
1071
1072 $mockWallClock = 1549343530.2053;
1073 $cache->setMockTime( $mockWallClock );
1074
1075 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1076 // several seconds during the test to assert the behaviour.
1077 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1078 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
1079 }
1080
1081 $mockWallClock += 0.100;
1082
1083 $cache->set( 'key1', $value1, 10 );
1084 $cache->set( 'key2', $value2, 10 );
1085
1086 $curTTLs = [];
1087 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1088 'key1' => $check1,
1089 $checkAll,
1090 'key2' => $check2,
1091 'key3' => $check3,
1092 ] );
1093 $this->assertEquals(
1094 [ 'key1' => $value1, 'key2' => $value2 ],
1095 $result,
1096 'Initial values'
1097 );
1098 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1099 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1100 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1101 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1102
1103 $mockWallClock += 0.100;
1104 $cache->touchCheckKey( $check1 );
1105
1106 $curTTLs = [];
1107 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1108 'key1' => $check1,
1109 $checkAll,
1110 'key2' => $check2,
1111 'key3' => $check3,
1112 ] );
1113 $this->assertEquals(
1114 [ 'key1' => $value1, 'key2' => $value2 ],
1115 $result,
1116 'key1 expired by check1, but value still provided'
1117 );
1118 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1119 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1120
1121 $cache->touchCheckKey( $checkAll );
1122
1123 $curTTLs = [];
1124 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1125 'key1' => $check1,
1126 $checkAll,
1127 'key2' => $check2,
1128 'key3' => $check3,
1129 ] );
1130 $this->assertEquals(
1131 [ 'key1' => $value1, 'key2' => $value2 ],
1132 $result,
1133 'All keys expired by checkAll, but value still provided'
1134 );
1135 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1136 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1137 }
1138
1139 /**
1140 * @covers WANObjectCache::get()
1141 * @covers WANObjectCache::processCheckKeys()
1142 */
1143 public function testCheckKeyInitHoldoff() {
1144 $cache = $this->cache;
1145
1146 for ( $i = 0; $i < 500; ++$i ) {
1147 $key = wfRandomString();
1148 $checkKey = wfRandomString();
1149 // miss, set, hit
1150 $cache->get( $key, $curTTL, [ $checkKey ] );
1151 $cache->set( $key, 'val', 10 );
1152 $curTTL = null;
1153 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1154
1155 $this->assertEquals( 'val', $v );
1156 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1157 }
1158
1159 for ( $i = 0; $i < 500; ++$i ) {
1160 $key = wfRandomString();
1161 $checkKey = wfRandomString();
1162 // set, hit
1163 $cache->set( $key, 'val', 10 );
1164 $curTTL = null;
1165 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1166
1167 $this->assertEquals( 'val', $v );
1168 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1169 }
1170 }
1171
1172 /**
1173 * @covers WANObjectCache::delete
1174 * @covers WANObjectCache::relayDelete
1175 * @covers WANObjectCache::relayPurge
1176 */
1177 public function testDelete() {
1178 $key = wfRandomString();
1179 $value = wfRandomString();
1180 $this->cache->set( $key, $value );
1181
1182 $curTTL = null;
1183 $v = $this->cache->get( $key, $curTTL );
1184 $this->assertEquals( $value, $v, "Key was created with value" );
1185 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1186
1187 $this->cache->delete( $key );
1188
1189 $curTTL = null;
1190 $v = $this->cache->get( $key, $curTTL );
1191 $this->assertFalse( $v, "Deleted key has false value" );
1192 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1193
1194 $this->cache->set( $key, $value . 'more' );
1195 $v = $this->cache->get( $key, $curTTL );
1196 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1197 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1198
1199 $this->cache->set( $key, $value );
1200 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1201
1202 $curTTL = null;
1203 $v = $this->cache->get( $key, $curTTL );
1204 $this->assertFalse( $v, "Deleted key has false value" );
1205 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1206
1207 $this->cache->set( $key, $value );
1208 $v = $this->cache->get( $key, $curTTL );
1209 $this->assertEquals( $value, $v, "Key was created with value" );
1210 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1211 }
1212
1213 /**
1214 * @dataProvider getWithSetCallback_versions_provider
1215 * @covers WANObjectCache::getWithSetCallback()
1216 * @covers WANObjectCache::doGetWithSetCallback()
1217 * @param array $extOpts
1218 * @param bool $versioned
1219 */
1220 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1221 $cache = $this->cache;
1222
1223 $key = wfRandomString();
1224 $valueV1 = wfRandomString();
1225 $valueV2 = [ wfRandomString() ];
1226
1227 $wasSet = 0;
1228 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1229 ++$wasSet;
1230
1231 return $valueV1;
1232 };
1233
1234 $priorValue = false;
1235 $priorAsOf = null;
1236 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1237 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1238 $priorValue = $oldValue;
1239 $priorAsOf = $oldAsOf;
1240 ++$wasSet;
1241
1242 return $valueV2; // new array format
1243 };
1244
1245 // Set the main key (version N if versioned)
1246 $wasSet = 0;
1247 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1248 $this->assertEquals( $valueV1, $v, "Value returned" );
1249 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1250 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1251 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1252 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1253
1254 if ( $versioned ) {
1255 // Set the key for version N+1 format
1256 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1257 } else {
1258 // Start versioning now with the unversioned key still there
1259 $verOpts = [ 'version' => 1 ];
1260 }
1261
1262 // Value goes to secondary key since V1 already used $key
1263 $wasSet = 0;
1264 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1265 $this->assertEquals( $valueV2, $v, "Value returned" );
1266 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1267 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1268 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1269
1270 $wasSet = 0;
1271 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1272 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1273 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1274
1275 // Clear out the older or unversioned key
1276 $cache->delete( $key, 0 );
1277
1278 // Set the key for next/first versioned format
1279 $wasSet = 0;
1280 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1281 $this->assertEquals( $valueV2, $v, "Value returned" );
1282 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1283
1284 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1285 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1286 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1287 }
1288
1289 public static function getWithSetCallback_versions_provider() {
1290 return [
1291 [ [], false ],
1292 [ [ 'version' => 1 ], true ]
1293 ];
1294 }
1295
1296 /**
1297 * @covers WANObjectCache::useInterimHoldOffCaching
1298 * @covers WANObjectCache::getInterimValue
1299 */
1300 public function testInterimHoldOffCaching() {
1301 $cache = $this->cache;
1302
1303 $value = 'CRL-40-940';
1304 $wasCalled = 0;
1305 $func = function () use ( &$wasCalled, $value ) {
1306 $wasCalled++;
1307
1308 return $value;
1309 };
1310
1311 $cache->useInterimHoldOffCaching( true );
1312
1313 $key = wfRandomString( 32 );
1314 $v = $cache->getWithSetCallback( $key, 60, $func );
1315 $v = $cache->getWithSetCallback( $key, 60, $func );
1316 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1317 $cache->delete( $key );
1318 $v = $cache->getWithSetCallback( $key, 60, $func );
1319 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1320 $v = $cache->getWithSetCallback( $key, 60, $func );
1321 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1322 // Lock up the mutex so interim cache is used
1323 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1324 $v = $cache->getWithSetCallback( $key, 60, $func );
1325 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1326 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
1327
1328 $cache->useInterimHoldOffCaching( false );
1329
1330 $wasCalled = 0;
1331 $key = wfRandomString( 32 );
1332 $v = $cache->getWithSetCallback( $key, 60, $func );
1333 $v = $cache->getWithSetCallback( $key, 60, $func );
1334 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1335 $cache->delete( $key );
1336 $v = $cache->getWithSetCallback( $key, 60, $func );
1337 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1338 $v = $cache->getWithSetCallback( $key, 60, $func );
1339 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1340 $v = $cache->getWithSetCallback( $key, 60, $func );
1341 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1342 // Lock up the mutex so interim cache is used
1343 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1344 $v = $cache->getWithSetCallback( $key, 60, $func );
1345 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1346 }
1347
1348 /**
1349 * @covers WANObjectCache::touchCheckKey
1350 * @covers WANObjectCache::resetCheckKey
1351 * @covers WANObjectCache::getCheckKeyTime
1352 * @covers WANObjectCache::getMultiCheckKeyTime
1353 * @covers WANObjectCache::makePurgeValue
1354 * @covers WANObjectCache::parsePurgeValue
1355 */
1356 public function testTouchKeys() {
1357 $cache = $this->cache;
1358 $key = wfRandomString();
1359
1360 $mockWallClock = 1549343530.2053;
1361 $priorTime = $mockWallClock; // reference time
1362 $cache->setMockTime( $mockWallClock );
1363
1364 $mockWallClock += 0.100;
1365 $t0 = $cache->getCheckKeyTime( $key );
1366 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1367
1368 $priorTime = $mockWallClock;
1369 $mockWallClock += 0.100;
1370 $cache->touchCheckKey( $key );
1371 $t1 = $cache->getCheckKeyTime( $key );
1372 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1373
1374 $t2 = $cache->getCheckKeyTime( $key );
1375 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1376
1377 $mockWallClock += 0.100;
1378 $cache->touchCheckKey( $key );
1379 $t3 = $cache->getCheckKeyTime( $key );
1380 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1381
1382 $t4 = $cache->getCheckKeyTime( $key );
1383 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1384
1385 $mockWallClock += 0.100;
1386 $cache->resetCheckKey( $key );
1387 $t5 = $cache->getCheckKeyTime( $key );
1388 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1389
1390 $t6 = $cache->getCheckKeyTime( $key );
1391 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1392 }
1393
1394 /**
1395 * @covers WANObjectCache::getMulti()
1396 */
1397 public function testGetWithSeveralCheckKeys() {
1398 $key = wfRandomString();
1399 $tKey1 = wfRandomString();
1400 $tKey2 = wfRandomString();
1401 $value = 'meow';
1402
1403 $mockWallClock = 1549343530.2053;
1404 $priorTime = $mockWallClock; // reference time
1405 $this->cache->setMockTime( $mockWallClock );
1406
1407 // Two check keys are newer (given hold-off) than $key, another is older
1408 $this->internalCache->set(
1409 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1410 WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 3 )
1411 );
1412 $this->internalCache->set(
1413 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1414 WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 5 )
1415 );
1416 $this->internalCache->set(
1417 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1418 WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 30 )
1419 );
1420 $this->cache->set( $key, $value, 30 );
1421
1422 $curTTL = null;
1423 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1424 $this->assertEquals( $value, $v, "Value matches" );
1425 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1426 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1427 }
1428
1429 /**
1430 * @covers WANObjectCache::reap()
1431 * @covers WANObjectCache::reapCheckKey()
1432 */
1433 public function testReap() {
1434 $vKey1 = wfRandomString();
1435 $vKey2 = wfRandomString();
1436 $tKey1 = wfRandomString();
1437 $tKey2 = wfRandomString();
1438 $value = 'moo';
1439
1440 $knownPurge = time() - 60;
1441 $goodTime = microtime( true ) - 5;
1442 $badTime = microtime( true ) - 300;
1443
1444 $this->internalCache->set(
1445 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
1446 [
1447 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1448 WANObjectCache::FLD_VALUE => $value,
1449 WANObjectCache::FLD_TTL => 3600,
1450 WANObjectCache::FLD_TIME => $goodTime
1451 ]
1452 );
1453 $this->internalCache->set(
1454 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
1455 [
1456 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1457 WANObjectCache::FLD_VALUE => $value,
1458 WANObjectCache::FLD_TTL => 3600,
1459 WANObjectCache::FLD_TIME => $badTime
1460 ]
1461 );
1462 $this->internalCache->set(
1463 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1464 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
1465 );
1466 $this->internalCache->set(
1467 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1468 WANObjectCache::PURGE_VAL_PREFIX . $badTime
1469 );
1470
1471 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1472 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1473 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1474 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1475
1476 $this->assertFalse( $bad1 );
1477 $this->assertTrue( $bad2 );
1478
1479 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1480 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1481 $this->assertFalse( $tBad1 );
1482 $this->assertTrue( $tBad2 );
1483 }
1484
1485 /**
1486 * @covers WANObjectCache::reap()
1487 */
1488 public function testReap_fail() {
1489 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1490 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1491 $backend->expects( $this->once() )->method( 'get' )
1492 ->willReturn( [
1493 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1494 WANObjectCache::FLD_VALUE => 'value',
1495 WANObjectCache::FLD_TTL => 3600,
1496 WANObjectCache::FLD_TIME => 300,
1497 ] );
1498 $backend->expects( $this->once() )->method( 'changeTTL' )
1499 ->willReturn( false );
1500
1501 $wanCache = new WANObjectCache( [
1502 'cache' => $backend
1503 ] );
1504
1505 $isStale = null;
1506 $ret = $wanCache->reap( 'key', 360, $isStale );
1507 $this->assertTrue( $isStale, 'value was stale' );
1508 $this->assertFalse( $ret, 'changeTTL failed' );
1509 }
1510
1511 /**
1512 * @covers WANObjectCache::set()
1513 */
1514 public function testSetWithLag() {
1515 $value = 1;
1516
1517 $key = wfRandomString();
1518 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1519 $this->cache->set( $key, $value, 30, $opts );
1520 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1521
1522 $key = wfRandomString();
1523 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1524 $this->cache->set( $key, $value, 30, $opts );
1525 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1526
1527 $key = wfRandomString();
1528 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1529 $this->cache->set( $key, $value, 30, $opts );
1530 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1531 }
1532
1533 /**
1534 * @covers WANObjectCache::set()
1535 */
1536 public function testWritePending() {
1537 $value = 1;
1538
1539 $key = wfRandomString();
1540 $opts = [ 'pending' => true ];
1541 $this->cache->set( $key, $value, 30, $opts );
1542 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1543 }
1544
1545 public function testMcRouterSupport() {
1546 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1547 ->setMethods( [ 'set', 'delete' ] )->getMock();
1548 $localBag->expects( $this->never() )->method( 'set' );
1549 $localBag->expects( $this->never() )->method( 'delete' );
1550 $wanCache = new WANObjectCache( [
1551 'cache' => $localBag,
1552 'mcrouterAware' => true,
1553 'region' => 'pmtpa',
1554 'cluster' => 'mw-wan'
1555 ] );
1556 $valFunc = function () {
1557 return 1;
1558 };
1559
1560 // None of these should use broadcasting commands (e.g. SET, DELETE)
1561 $wanCache->get( 'x' );
1562 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1563 $wanCache->getMulti( [ 'x', 'y' ] );
1564 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1565 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1566 $wanCache->getCheckKeyTime( 'zzz' );
1567 $wanCache->reap( 'x', time() - 300 );
1568 $wanCache->reap( 'zzz', time() - 300 );
1569 }
1570
1571 public function testMcRouterSupportBroadcastDelete() {
1572 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1573 ->setMethods( [ 'set' ] )->getMock();
1574 $wanCache = new WANObjectCache( [
1575 'cache' => $localBag,
1576 'mcrouterAware' => true,
1577 'region' => 'pmtpa',
1578 'cluster' => 'mw-wan'
1579 ] );
1580
1581 $localBag->expects( $this->once() )->method( 'set' )
1582 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX . "test" );
1583
1584 $wanCache->delete( 'test' );
1585 }
1586
1587 public function testMcRouterSupportBroadcastTouchCK() {
1588 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1589 ->setMethods( [ 'set' ] )->getMock();
1590 $wanCache = new WANObjectCache( [
1591 'cache' => $localBag,
1592 'mcrouterAware' => true,
1593 'region' => 'pmtpa',
1594 'cluster' => 'mw-wan'
1595 ] );
1596
1597 $localBag->expects( $this->once() )->method( 'set' )
1598 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1599
1600 $wanCache->touchCheckKey( 'test' );
1601 }
1602
1603 public function testMcRouterSupportBroadcastResetCK() {
1604 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1605 ->setMethods( [ 'delete' ] )->getMock();
1606 $wanCache = new WANObjectCache( [
1607 'cache' => $localBag,
1608 'mcrouterAware' => true,
1609 'region' => 'pmtpa',
1610 'cluster' => 'mw-wan'
1611 ] );
1612
1613 $localBag->expects( $this->once() )->method( 'delete' )
1614 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1615
1616 $wanCache->resetCheckKey( 'test' );
1617 }
1618
1619 public function testEpoch() {
1620 $bag = new HashBagOStuff();
1621 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1622 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1623
1624 $now = microtime( true );
1625 $cache->setMockTime( $now );
1626
1627 $cache->set( $key, 'Do what thou Wilt' );
1628 $cache->touchCheckKey( $key );
1629
1630 $then = $now;
1631 $now += 30;
1632 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1633 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1634
1635 $cache = new WANObjectCache( [
1636 'cache' => $bag,
1637 'epoch' => $now - 3600
1638 ] );
1639 $cache->setMockTime( $now );
1640
1641 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1642 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1643
1644 $now += 30;
1645 $cache = new WANObjectCache( [
1646 'cache' => $bag,
1647 'epoch' => $now + 3600
1648 ] );
1649 $cache->setMockTime( $now );
1650
1651 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1652 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1653 }
1654
1655 /**
1656 * @dataProvider provideAdaptiveTTL
1657 * @covers WANObjectCache::adaptiveTTL()
1658 * @param float|int $ago
1659 * @param int $maxTTL
1660 * @param int $minTTL
1661 * @param float $factor
1662 * @param int $adaptiveTTL
1663 */
1664 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1665 $mtime = $ago ? time() - $ago : $ago;
1666 $margin = 5;
1667 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1668
1669 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1670 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1671
1672 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1673
1674 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1675 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1676 }
1677
1678 public static function provideAdaptiveTTL() {
1679 return [
1680 [ 3600, 900, 30, 0.2, 720 ],
1681 [ 3600, 500, 30, 0.2, 500 ],
1682 [ 3600, 86400, 800, 0.2, 800 ],
1683 [ false, 86400, 800, 0.2, 800 ],
1684 [ null, 86400, 800, 0.2, 800 ]
1685 ];
1686 }
1687
1688 /**
1689 * @covers WANObjectCache::__construct
1690 * @covers WANObjectCache::newEmpty
1691 */
1692 public function testNewEmpty() {
1693 $this->assertInstanceOf(
1694 WANObjectCache::class,
1695 WANObjectCache::newEmpty()
1696 );
1697 }
1698
1699 /**
1700 * @covers WANObjectCache::setLogger
1701 */
1702 public function testSetLogger() {
1703 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1704 }
1705
1706 /**
1707 * @covers WANObjectCache::getQoS
1708 */
1709 public function testGetQoS() {
1710 $backend = $this->getMockBuilder( HashBagOStuff::class )
1711 ->setMethods( [ 'getQoS' ] )->getMock();
1712 $backend->expects( $this->once() )->method( 'getQoS' )
1713 ->willReturn( BagOStuff::QOS_UNKNOWN );
1714 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1715
1716 $this->assertSame(
1717 $wanCache::QOS_UNKNOWN,
1718 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1719 );
1720 }
1721
1722 /**
1723 * @covers WANObjectCache::makeKey
1724 */
1725 public function testMakeKey() {
1726 $backend = $this->getMockBuilder( HashBagOStuff::class )
1727 ->setMethods( [ 'makeKey' ] )->getMock();
1728 $backend->expects( $this->once() )->method( 'makeKey' )
1729 ->willReturn( 'special' );
1730
1731 $wanCache = new WANObjectCache( [
1732 'cache' => $backend
1733 ] );
1734
1735 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1736 }
1737
1738 /**
1739 * @covers WANObjectCache::makeGlobalKey
1740 */
1741 public function testMakeGlobalKey() {
1742 $backend = $this->getMockBuilder( HashBagOStuff::class )
1743 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1744 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1745 ->willReturn( 'special' );
1746
1747 $wanCache = new WANObjectCache( [
1748 'cache' => $backend
1749 ] );
1750
1751 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1752 }
1753
1754 public static function statsKeyProvider() {
1755 return [
1756 [ 'domain:page:5', 'page' ],
1757 [ 'domain:main-key', 'main-key' ],
1758 [ 'domain:page:history', 'page' ],
1759 [ 'missingdomainkey', 'missingdomainkey' ]
1760 ];
1761 }
1762
1763 /**
1764 * @dataProvider statsKeyProvider
1765 * @covers WANObjectCache::determineKeyClass
1766 */
1767 public function testStatsKeyClass( $key, $class ) {
1768 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1769 'cache' => new HashBagOStuff
1770 ] ) );
1771
1772 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1773 }
1774 }
1775
1776 class NearExpiringWANObjectCache extends WANObjectCache {
1777 const CLOCK_SKEW = 1;
1778
1779 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1780 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1781 }
1782 }
1783
1784 class PopularityRefreshingWANObjectCache extends WANObjectCache {
1785 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1786 return ( ( $now - $asOf ) > $timeTillRefresh );
1787 }
1788 }