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