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