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