Merge "Increase Opera minimum for Grades A and C to 15"
[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 $cache = new PopularityRefreshingWANObjectCache( [
403 'cache' => new HashBagOStuff(),
404 'pool' => 'empty'
405 ] );
406
407 $now = microtime( true ); // reference time
408 $wasSet = 0;
409 $key = wfRandomString();
410 $opts = [ 'hotTTR' => 900 ];
411 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
412 $this->assertEquals( $value, $v, "Value returned" );
413 $this->assertEquals( 1, $wasSet, "Value calculated" );
414 $cache->setTime( $now + 30 );
415 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
416 $this->assertEquals( 1, $wasSet, "Value cached" );
417
418 $wasSet = 0;
419 $key = wfRandomString();
420 $opts = [ 'hotTTR' => 10 ];
421 $cache->setTime( $now );
422 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
423 $this->assertEquals( $value, $v, "Value returned" );
424 $this->assertEquals( 1, $wasSet, "Value calculated" );
425 $cache->setTime( $now + 30 );
426 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
427 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
428 }
429
430 /**
431 * @covers WANObjectCache::getWithSetCallback()
432 * @covers WANObjectCache::doGetWithSetCallback()
433 */
434 public function testGetWithSetCallback_invalidCallback() {
435 $this->setExpectedException( InvalidArgumentException::class );
436 $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
437 }
438
439 /**
440 * @dataProvider getMultiWithSetCallback_provider
441 * @covers WANObjectCache::getMultiWithSetCallback
442 * @covers WANObjectCache::makeMultiKeys
443 * @covers WANObjectCache::getMulti
444 * @param array $extOpts
445 * @param bool $versioned
446 */
447 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
448 $cache = $this->cache;
449
450 $keyA = wfRandomString();
451 $keyB = wfRandomString();
452 $keyC = wfRandomString();
453 $cKey1 = wfRandomString();
454 $cKey2 = wfRandomString();
455
456 $priorValue = null;
457 $priorAsOf = null;
458 $wasSet = 0;
459 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
460 &$wasSet, &$priorValue, &$priorAsOf
461 ) {
462 ++$wasSet;
463 $priorValue = $old;
464 $priorAsOf = $asOf;
465 $ttl = 20; // override with another value
466 return "@$id$";
467 };
468
469 $wasSet = 0;
470 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
471 $value = "@3353$";
472 $v = $cache->getMultiWithSetCallback(
473 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
474 $this->assertEquals( $value, $v[$keyA], "Value returned" );
475 $this->assertEquals( 1, $wasSet, "Value regenerated" );
476 $this->assertFalse( $priorValue, "No prior value" );
477 $this->assertNull( $priorAsOf, "No prior value" );
478
479 $curTTL = null;
480 $cache->get( $keyA, $curTTL );
481 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
482 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
483
484 $wasSet = 0;
485 $value = "@efef$";
486 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
487 $v = $cache->getMultiWithSetCallback(
488 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
489 $this->assertEquals( $value, $v[$keyB], "Value returned" );
490 $this->assertEquals( 1, $wasSet, "Value regenerated" );
491 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
492 $v = $cache->getMultiWithSetCallback(
493 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
494 $this->assertEquals( $value, $v[$keyB], "Value returned" );
495 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
496 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
497
498 $priorTime = microtime( true ); // reference time
499 $cache->setTime( $priorTime );
500
501 $cache->addTime( 1 );
502
503 $wasSet = 0;
504 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
505 $v = $cache->getMultiWithSetCallback(
506 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
507 );
508 $this->assertEquals( $value, $v[$keyB], "Value returned" );
509 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
510 $this->assertEquals( $value, $priorValue, "Has prior value" );
511 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
512 $t1 = $cache->getCheckKeyTime( $cKey1 );
513 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
514 $t2 = $cache->getCheckKeyTime( $cKey2 );
515 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
516
517 $priorTime = $cache->addTime( 0.01 );
518 $value = "@43636$";
519 $wasSet = 0;
520 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
521 $v = $cache->getMultiWithSetCallback(
522 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
523 );
524 $this->assertEquals( $value, $v[$keyC], "Value returned" );
525 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
526 $t1 = $cache->getCheckKeyTime( $cKey1 );
527 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
528 $t2 = $cache->getCheckKeyTime( $cKey2 );
529 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
530
531 $curTTL = null;
532 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
533 if ( $versioned ) {
534 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
535 } else {
536 $this->assertEquals( $value, $v, "Value returned" );
537 }
538 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
539
540 $wasSet = 0;
541 $key = wfRandomString();
542 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
543 $v = $cache->getMultiWithSetCallback(
544 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
545 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
546 $cache->delete( $key );
547 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
548 $v = $cache->getMultiWithSetCallback(
549 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
550 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
551 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
552
553 $calls = 0;
554 $ids = [ 1, 2, 3, 4, 5, 6 ];
555 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
556 return $wanCache->makeKey( 'test', $id );
557 };
558 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
559 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
560 ++$calls;
561
562 return "val-{$id}";
563 };
564 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
565
566 $this->assertEquals(
567 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
568 array_values( $values ),
569 "Correct values in correct order"
570 );
571 $this->assertEquals(
572 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
573 array_keys( $values ),
574 "Correct keys in correct order"
575 );
576 $this->assertEquals( count( $ids ), $calls );
577
578 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
579 $this->assertEquals( count( $ids ), $calls, "Values cached" );
580
581 // Mock the BagOStuff to assure only one getMulti() call given process caching
582 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
583 ->setMethods( [ 'getMulti' ] )->getMock();
584 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
585 WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
586 WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
587 ] );
588 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
589
590 // Warm the process cache
591 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
592 $this->assertEquals(
593 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
594 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
595 );
596 // Use the process cache
597 $this->assertEquals(
598 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
599 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
600 );
601 }
602
603 public static function getMultiWithSetCallback_provider() {
604 return [
605 [ [], false ],
606 [ [ 'version' => 1 ], true ]
607 ];
608 }
609
610 /**
611 * @dataProvider getMultiWithUnionSetCallback_provider
612 * @covers WANObjectCache::getMultiWithUnionSetCallback()
613 * @covers WANObjectCache::makeMultiKeys()
614 * @param array $extOpts
615 * @param bool $versioned
616 */
617 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
618 $cache = $this->cache;
619
620 $keyA = wfRandomString();
621 $keyB = wfRandomString();
622 $keyC = wfRandomString();
623 $cKey1 = wfRandomString();
624 $cKey2 = wfRandomString();
625
626 $wasSet = 0;
627 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
628 &$wasSet, &$priorValue, &$priorAsOf
629 ) {
630 $newValues = [];
631 foreach ( $ids as $id ) {
632 ++$wasSet;
633 $newValues[$id] = "@$id$";
634 $ttls[$id] = 20; // override with another value
635 }
636
637 return $newValues;
638 };
639
640 $wasSet = 0;
641 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
642 $value = "@3353$";
643 $v = $cache->getMultiWithUnionSetCallback(
644 $keyedIds, 30, $genFunc, $extOpts );
645 $this->assertEquals( $value, $v[$keyA], "Value returned" );
646 $this->assertEquals( 1, $wasSet, "Value regenerated" );
647
648 $curTTL = null;
649 $cache->get( $keyA, $curTTL );
650 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
651 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
652
653 $wasSet = 0;
654 $value = "@efef$";
655 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
656 $v = $cache->getMultiWithUnionSetCallback(
657 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
658 $this->assertEquals( $value, $v[$keyB], "Value returned" );
659 $this->assertEquals( 1, $wasSet, "Value regenerated" );
660 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
661 $v = $cache->getMultiWithUnionSetCallback(
662 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
663 $this->assertEquals( $value, $v[$keyB], "Value returned" );
664 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
665 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
666
667 $priorTime = microtime( true ); // reference time
668 $cache->setTime( $priorTime );
669
670 $cache->addTime( 1 );
671
672 $wasSet = 0;
673 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
674 $v = $cache->getMultiWithUnionSetCallback(
675 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
676 );
677 $this->assertEquals( $value, $v[$keyB], "Value returned" );
678 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
679 $t1 = $cache->getCheckKeyTime( $cKey1 );
680 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
681 $t2 = $cache->getCheckKeyTime( $cKey2 );
682 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
683
684 $priorTime = $cache->addTime( 0.01 );
685 $value = "@43636$";
686 $wasSet = 0;
687 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
688 $v = $cache->getMultiWithUnionSetCallback(
689 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
690 );
691 $this->assertEquals( $value, $v[$keyC], "Value returned" );
692 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
693 $t1 = $cache->getCheckKeyTime( $cKey1 );
694 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
695 $t2 = $cache->getCheckKeyTime( $cKey2 );
696 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
697
698 $curTTL = null;
699 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
700 if ( $versioned ) {
701 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
702 } else {
703 $this->assertEquals( $value, $v, "Value returned" );
704 }
705 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
706
707 $wasSet = 0;
708 $key = wfRandomString();
709 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
710 $v = $cache->getMultiWithUnionSetCallback(
711 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
712 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
713 $cache->delete( $key );
714 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
715 $v = $cache->getMultiWithUnionSetCallback(
716 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
717 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
718 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
719
720 $calls = 0;
721 $ids = [ 1, 2, 3, 4, 5, 6 ];
722 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
723 return $wanCache->makeKey( 'test', $id );
724 };
725 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
726 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
727 $newValues = [];
728 foreach ( $ids as $id ) {
729 ++$calls;
730 $newValues[$id] = "val-{$id}";
731 }
732
733 return $newValues;
734 };
735 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
736
737 $this->assertEquals(
738 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
739 array_values( $values ),
740 "Correct values in correct order"
741 );
742 $this->assertEquals(
743 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
744 array_keys( $values ),
745 "Correct keys in correct order"
746 );
747 $this->assertEquals( count( $ids ), $calls );
748
749 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
750 $this->assertEquals( count( $ids ), $calls, "Values cached" );
751 }
752
753 public static function getMultiWithUnionSetCallback_provider() {
754 return [
755 [ [], false ],
756 [ [ 'version' => 1 ], true ]
757 ];
758 }
759
760 /**
761 * @covers WANObjectCache::getWithSetCallback()
762 * @covers WANObjectCache::doGetWithSetCallback()
763 */
764 public function testLockTSE() {
765 $cache = $this->cache;
766 $key = wfRandomString();
767 $value = wfRandomString();
768
769 $calls = 0;
770 $func = function () use ( &$calls, $value, $cache, $key ) {
771 ++$calls;
772 // Immediately kill any mutex rather than waiting a second
773 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
774 return $value;
775 };
776
777 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
778 $this->assertEquals( $value, $ret );
779 $this->assertEquals( 1, $calls, 'Value was populated' );
780
781 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
782 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
783
784 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
785 $ret = $cache->getWithSetCallback( $key, 30, $func,
786 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
787 $this->assertEquals( $value, $ret, 'Old value used' );
788 $this->assertEquals( 1, $calls, 'Callback was not used' );
789
790 $cache->delete( $key );
791 $ret = $cache->getWithSetCallback( $key, 30, $func,
792 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
793 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
794 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
795
796 $ret = $cache->getWithSetCallback( $key, 30, $func,
797 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
798 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
799 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
800 }
801
802 /**
803 * @covers WANObjectCache::getWithSetCallback()
804 * @covers WANObjectCache::doGetWithSetCallback()
805 * @covers WANObjectCache::set()
806 */
807 public function testLockTSESlow() {
808 $cache = $this->cache;
809 $key = wfRandomString();
810 $value = wfRandomString();
811
812 $calls = 0;
813 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
814 ++$calls;
815 $setOpts['since'] = microtime( true ) - 10;
816 // Immediately kill any mutex rather than waiting a second
817 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
818 return $value;
819 };
820
821 // Value should be marked as stale due to snapshot lag
822 $curTTL = null;
823 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
824 $this->assertEquals( $value, $ret );
825 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
826 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
827 $this->assertEquals( 1, $calls, 'Value was generated' );
828
829 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
830 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
831 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
832 $this->assertEquals( $value, $ret );
833 $this->assertEquals( 1, $calls, 'Callback was not used' );
834 }
835
836 /**
837 * @covers WANObjectCache::getWithSetCallback()
838 * @covers WANObjectCache::doGetWithSetCallback()
839 */
840 public function testBusyValue() {
841 $cache = $this->cache;
842 $key = wfRandomString();
843 $value = wfRandomString();
844 $busyValue = wfRandomString();
845
846 $calls = 0;
847 $func = function () use ( &$calls, $value, $cache, $key ) {
848 ++$calls;
849 // Immediately kill any mutex rather than waiting a second
850 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
851 return $value;
852 };
853
854 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
855 $this->assertEquals( $value, $ret );
856 $this->assertEquals( 1, $calls, 'Value was populated' );
857
858 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
859 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
860
861 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
862 $ret = $cache->getWithSetCallback( $key, 30, $func,
863 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
864 $this->assertEquals( $value, $ret, 'Callback used' );
865 $this->assertEquals( 2, $calls, 'Callback used' );
866
867 $ret = $cache->getWithSetCallback( $key, 30, $func,
868 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
869 $this->assertEquals( $value, $ret, 'Old value used' );
870 $this->assertEquals( 2, $calls, 'Callback was not used' );
871
872 $cache->delete( $key ); // no value at all anymore and still locked
873 $ret = $cache->getWithSetCallback( $key, 30, $func,
874 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
875 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
876 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
877
878 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
879 $ret = $cache->getWithSetCallback( $key, 30, $func,
880 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
881 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
882 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
883
884 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
885 $ret = $cache->getWithSetCallback( $key, 30, $func,
886 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
887 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
888 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
889 }
890
891 /**
892 * @covers WANObjectCache::getMulti()
893 */
894 public function testGetMulti() {
895 $cache = $this->cache;
896
897 $value1 = [ 'this' => 'is', 'a' => 'test' ];
898 $value2 = [ 'this' => 'is', 'another' => 'test' ];
899
900 $key1 = wfRandomString();
901 $key2 = wfRandomString();
902 $key3 = wfRandomString();
903
904 $cache->set( $key1, $value1, 5 );
905 $cache->set( $key2, $value2, 10 );
906
907 $curTTLs = [];
908 $this->assertEquals(
909 [ $key1 => $value1, $key2 => $value2 ],
910 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
911 'Result array populated'
912 );
913
914 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
915 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
916 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
917
918 $cKey1 = wfRandomString();
919 $cKey2 = wfRandomString();
920
921 $priorTime = microtime( true ); // reference time
922 $cache->setTime( $priorTime );
923
924 $cache->addTime( 1 );
925
926 $curTTLs = [];
927 $this->assertEquals(
928 [ $key1 => $value1, $key2 => $value2 ],
929 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
930 "Result array populated even with new check keys"
931 );
932 $t1 = $cache->getCheckKeyTime( $cKey1 );
933 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
934 $t2 = $cache->getCheckKeyTime( $cKey2 );
935 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
936 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
937 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
938 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
939
940 $cache->addTime( 1 );
941
942 $curTTLs = [];
943 $this->assertEquals(
944 [ $key1 => $value1, $key2 => $value2 ],
945 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
946 "Result array still populated even with new check keys"
947 );
948 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
949 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
950 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
951 }
952
953 /**
954 * @covers WANObjectCache::getMulti()
955 * @covers WANObjectCache::processCheckKeys()
956 */
957 public function testGetMultiCheckKeys() {
958 $cache = $this->cache;
959
960 $checkAll = wfRandomString();
961 $check1 = wfRandomString();
962 $check2 = wfRandomString();
963 $check3 = wfRandomString();
964 $value1 = wfRandomString();
965 $value2 = wfRandomString();
966
967 $cache->setTime( microtime( true ) );
968
969 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
970 // several seconds during the test to assert the behaviour.
971 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
972 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
973 }
974
975 $cache->addTime( 0.100 );
976
977 $cache->set( 'key1', $value1, 10 );
978 $cache->set( 'key2', $value2, 10 );
979
980 $curTTLs = [];
981 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
982 'key1' => $check1,
983 $checkAll,
984 'key2' => $check2,
985 'key3' => $check3,
986 ] );
987 $this->assertEquals(
988 [ 'key1' => $value1, 'key2' => $value2 ],
989 $result,
990 'Initial values'
991 );
992 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
993 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
994 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
995 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
996
997 $cache->addTime( 0.100 );
998 $cache->touchCheckKey( $check1 );
999
1000 $curTTLs = [];
1001 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1002 'key1' => $check1,
1003 $checkAll,
1004 'key2' => $check2,
1005 'key3' => $check3,
1006 ] );
1007 $this->assertEquals(
1008 [ 'key1' => $value1, 'key2' => $value2 ],
1009 $result,
1010 'key1 expired by check1, but value still provided'
1011 );
1012 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1013 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1014
1015 $cache->touchCheckKey( $checkAll );
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 'All keys expired by checkAll, but value still provided'
1028 );
1029 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1030 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1031 }
1032
1033 /**
1034 * @covers WANObjectCache::get()
1035 * @covers WANObjectCache::processCheckKeys()
1036 */
1037 public function testCheckKeyInitHoldoff() {
1038 $cache = $this->cache;
1039
1040 for ( $i = 0; $i < 500; ++$i ) {
1041 $key = wfRandomString();
1042 $checkKey = wfRandomString();
1043 // miss, set, hit
1044 $cache->get( $key, $curTTL, [ $checkKey ] );
1045 $cache->set( $key, 'val', 10 );
1046 $curTTL = null;
1047 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1048
1049 $this->assertEquals( 'val', $v );
1050 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1051 }
1052
1053 for ( $i = 0; $i < 500; ++$i ) {
1054 $key = wfRandomString();
1055 $checkKey = wfRandomString();
1056 // set, hit
1057 $cache->set( $key, 'val', 10 );
1058 $curTTL = null;
1059 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1060
1061 $this->assertEquals( 'val', $v );
1062 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1063 }
1064 }
1065
1066 /**
1067 * @covers WANObjectCache::delete
1068 * @covers WANObjectCache::relayDelete
1069 * @covers WANObjectCache::relayPurge
1070 */
1071 public function testDelete() {
1072 $key = wfRandomString();
1073 $value = wfRandomString();
1074 $this->cache->set( $key, $value );
1075
1076 $curTTL = null;
1077 $v = $this->cache->get( $key, $curTTL );
1078 $this->assertEquals( $value, $v, "Key was created with value" );
1079 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1080
1081 $this->cache->delete( $key );
1082
1083 $curTTL = null;
1084 $v = $this->cache->get( $key, $curTTL );
1085 $this->assertFalse( $v, "Deleted key has false value" );
1086 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1087
1088 $this->cache->set( $key, $value . 'more' );
1089 $v = $this->cache->get( $key, $curTTL );
1090 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1091 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1092
1093 $this->cache->set( $key, $value );
1094 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1095
1096 $curTTL = null;
1097 $v = $this->cache->get( $key, $curTTL );
1098 $this->assertFalse( $v, "Deleted key has false value" );
1099 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1100
1101 $this->cache->set( $key, $value );
1102 $v = $this->cache->get( $key, $curTTL );
1103 $this->assertEquals( $value, $v, "Key was created with value" );
1104 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1105 }
1106
1107 /**
1108 * @dataProvider getWithSetCallback_versions_provider
1109 * @covers WANObjectCache::getWithSetCallback()
1110 * @covers WANObjectCache::doGetWithSetCallback()
1111 * @param array $extOpts
1112 * @param bool $versioned
1113 */
1114 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1115 $cache = $this->cache;
1116
1117 $key = wfRandomString();
1118 $valueV1 = wfRandomString();
1119 $valueV2 = [ wfRandomString() ];
1120
1121 $wasSet = 0;
1122 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1123 ++$wasSet;
1124
1125 return $valueV1;
1126 };
1127
1128 $priorValue = false;
1129 $priorAsOf = null;
1130 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1131 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1132 $priorValue = $oldValue;
1133 $priorAsOf = $oldAsOf;
1134 ++$wasSet;
1135
1136 return $valueV2; // new array format
1137 };
1138
1139 // Set the main key (version N if versioned)
1140 $wasSet = 0;
1141 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1142 $this->assertEquals( $valueV1, $v, "Value returned" );
1143 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1144 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1145 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1146 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1147
1148 if ( $versioned ) {
1149 // Set the key for version N+1 format
1150 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1151 } else {
1152 // Start versioning now with the unversioned key still there
1153 $verOpts = [ 'version' => 1 ];
1154 }
1155
1156 // Value goes to secondary key since V1 already used $key
1157 $wasSet = 0;
1158 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1159 $this->assertEquals( $valueV2, $v, "Value returned" );
1160 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1161 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1162 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1163
1164 $wasSet = 0;
1165 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1166 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1167 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1168
1169 // Clear out the older or unversioned key
1170 $cache->delete( $key, 0 );
1171
1172 // Set the key for next/first versioned format
1173 $wasSet = 0;
1174 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1175 $this->assertEquals( $valueV2, $v, "Value returned" );
1176 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1177
1178 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1179 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1180 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1181 }
1182
1183 public static function getWithSetCallback_versions_provider() {
1184 return [
1185 [ [], false ],
1186 [ [ 'version' => 1 ], true ]
1187 ];
1188 }
1189
1190 /**
1191 * @covers WANObjectCache::touchCheckKey
1192 * @covers WANObjectCache::resetCheckKey
1193 * @covers WANObjectCache::getCheckKeyTime
1194 * @covers WANObjectCache::makePurgeValue
1195 * @covers WANObjectCache::parsePurgeValue
1196 */
1197 public function testTouchKeys() {
1198 $cache = $this->cache;
1199 $key = wfRandomString();
1200
1201 $priorTime = microtime( true ); // reference time
1202 $cache->setTime( $priorTime );
1203
1204 $newTime = $cache->addTime( 0.100 );
1205 $t0 = $cache->getCheckKeyTime( $key );
1206 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1207
1208 $priorTime = $newTime;
1209 $newTime = $cache->addTime( 0.100 );
1210 $cache->touchCheckKey( $key );
1211 $t1 = $cache->getCheckKeyTime( $key );
1212 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1213
1214 $t2 = $cache->getCheckKeyTime( $key );
1215 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1216
1217 $cache->addTime( 0.100 );
1218 $cache->touchCheckKey( $key );
1219 $t3 = $cache->getCheckKeyTime( $key );
1220 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1221
1222 $t4 = $cache->getCheckKeyTime( $key );
1223 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1224
1225 $cache->addTime( 0.100 );
1226 $cache->resetCheckKey( $key );
1227 $t5 = $cache->getCheckKeyTime( $key );
1228 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1229
1230 $t6 = $cache->getCheckKeyTime( $key );
1231 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1232 }
1233
1234 /**
1235 * @covers WANObjectCache::getMulti()
1236 */
1237 public function testGetWithSeveralCheckKeys() {
1238 $key = wfRandomString();
1239 $tKey1 = wfRandomString();
1240 $tKey2 = wfRandomString();
1241 $value = 'meow';
1242
1243 // Two check keys are newer (given hold-off) than $key, another is older
1244 $this->internalCache->set(
1245 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1246 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
1247 );
1248 $this->internalCache->set(
1249 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1250 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
1251 );
1252 $this->internalCache->set(
1253 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1254 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
1255 );
1256 $this->cache->set( $key, $value, 30 );
1257
1258 $curTTL = null;
1259 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1260 $this->assertEquals( $value, $v, "Value matches" );
1261 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1262 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1263 }
1264
1265 /**
1266 * @covers WANObjectCache::reap()
1267 * @covers WANObjectCache::reapCheckKey()
1268 */
1269 public function testReap() {
1270 $vKey1 = wfRandomString();
1271 $vKey2 = wfRandomString();
1272 $tKey1 = wfRandomString();
1273 $tKey2 = wfRandomString();
1274 $value = 'moo';
1275
1276 $knownPurge = time() - 60;
1277 $goodTime = microtime( true ) - 5;
1278 $badTime = microtime( true ) - 300;
1279
1280 $this->internalCache->set(
1281 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
1282 [
1283 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1284 WANObjectCache::FLD_VALUE => $value,
1285 WANObjectCache::FLD_TTL => 3600,
1286 WANObjectCache::FLD_TIME => $goodTime
1287 ]
1288 );
1289 $this->internalCache->set(
1290 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
1291 [
1292 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1293 WANObjectCache::FLD_VALUE => $value,
1294 WANObjectCache::FLD_TTL => 3600,
1295 WANObjectCache::FLD_TIME => $badTime
1296 ]
1297 );
1298 $this->internalCache->set(
1299 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1300 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
1301 );
1302 $this->internalCache->set(
1303 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1304 WANObjectCache::PURGE_VAL_PREFIX . $badTime
1305 );
1306
1307 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1308 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1309 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1310 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1311
1312 $this->assertFalse( $bad1 );
1313 $this->assertTrue( $bad2 );
1314
1315 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1316 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1317 $this->assertFalse( $tBad1 );
1318 $this->assertTrue( $tBad2 );
1319 }
1320
1321 /**
1322 * @covers WANObjectCache::reap()
1323 */
1324 public function testReap_fail() {
1325 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1326 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1327 $backend->expects( $this->once() )->method( 'get' )
1328 ->willReturn( [
1329 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1330 WANObjectCache::FLD_VALUE => 'value',
1331 WANObjectCache::FLD_TTL => 3600,
1332 WANObjectCache::FLD_TIME => 300,
1333 ] );
1334 $backend->expects( $this->once() )->method( 'changeTTL' )
1335 ->willReturn( false );
1336
1337 $wanCache = new WANObjectCache( [
1338 'cache' => $backend,
1339 'pool' => 'testcache-hash',
1340 'relayer' => new EventRelayerNull( [] )
1341 ] );
1342
1343 $isStale = null;
1344 $ret = $wanCache->reap( 'key', 360, $isStale );
1345 $this->assertTrue( $isStale, 'value was stale' );
1346 $this->assertFalse( $ret, 'changeTTL failed' );
1347 }
1348
1349 /**
1350 * @covers WANObjectCache::set()
1351 */
1352 public function testSetWithLag() {
1353 $value = 1;
1354
1355 $key = wfRandomString();
1356 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1357 $this->cache->set( $key, $value, 30, $opts );
1358 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1359
1360 $key = wfRandomString();
1361 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1362 $this->cache->set( $key, $value, 30, $opts );
1363 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1364
1365 $key = wfRandomString();
1366 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1367 $this->cache->set( $key, $value, 30, $opts );
1368 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1369 }
1370
1371 /**
1372 * @covers WANObjectCache::set()
1373 */
1374 public function testWritePending() {
1375 $value = 1;
1376
1377 $key = wfRandomString();
1378 $opts = [ 'pending' => true ];
1379 $this->cache->set( $key, $value, 30, $opts );
1380 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1381 }
1382
1383 public function testMcRouterSupport() {
1384 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1385 ->setMethods( [ 'set', 'delete' ] )->getMock();
1386 $localBag->expects( $this->never() )->method( 'set' );
1387 $localBag->expects( $this->never() )->method( 'delete' );
1388 $wanCache = new WANObjectCache( [
1389 'cache' => $localBag,
1390 'pool' => 'testcache-hash',
1391 'relayer' => new EventRelayerNull( [] )
1392 ] );
1393 $valFunc = function () {
1394 return 1;
1395 };
1396
1397 // None of these should use broadcasting commands (e.g. SET, DELETE)
1398 $wanCache->get( 'x' );
1399 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1400 $wanCache->getMulti( [ 'x', 'y' ] );
1401 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1402 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1403 $wanCache->getCheckKeyTime( 'zzz' );
1404 $wanCache->reap( 'x', time() - 300 );
1405 $wanCache->reap( 'zzz', time() - 300 );
1406 }
1407
1408 /**
1409 * @dataProvider provideAdaptiveTTL
1410 * @covers WANObjectCache::adaptiveTTL()
1411 * @param float|int $ago
1412 * @param int $maxTTL
1413 * @param int $minTTL
1414 * @param float $factor
1415 * @param int $adaptiveTTL
1416 */
1417 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1418 $mtime = $ago ? time() - $ago : $ago;
1419 $margin = 5;
1420 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1421
1422 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1423 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1424
1425 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1426
1427 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1428 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1429 }
1430
1431 public static function provideAdaptiveTTL() {
1432 return [
1433 [ 3600, 900, 30, 0.2, 720 ],
1434 [ 3600, 500, 30, 0.2, 500 ],
1435 [ 3600, 86400, 800, 0.2, 800 ],
1436 [ false, 86400, 800, 0.2, 800 ],
1437 [ null, 86400, 800, 0.2, 800 ]
1438 ];
1439 }
1440
1441 /**
1442 * @covers WANObjectCache::__construct
1443 * @covers WANObjectCache::newEmpty
1444 */
1445 public function testNewEmpty() {
1446 $this->assertInstanceOf(
1447 WANObjectCache::class,
1448 WANObjectCache::newEmpty()
1449 );
1450 }
1451
1452 /**
1453 * @covers WANObjectCache::setLogger
1454 */
1455 public function testSetLogger() {
1456 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1457 }
1458
1459 /**
1460 * @covers WANObjectCache::getQoS
1461 */
1462 public function testGetQoS() {
1463 $backend = $this->getMockBuilder( HashBagOStuff::class )
1464 ->setMethods( [ 'getQoS' ] )->getMock();
1465 $backend->expects( $this->once() )->method( 'getQoS' )
1466 ->willReturn( BagOStuff::QOS_UNKNOWN );
1467 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1468
1469 $this->assertSame(
1470 $wanCache::QOS_UNKNOWN,
1471 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1472 );
1473 }
1474
1475 /**
1476 * @covers WANObjectCache::makeKey
1477 */
1478 public function testMakeKey() {
1479 $backend = $this->getMockBuilder( HashBagOStuff::class )
1480 ->setMethods( [ 'makeKey' ] )->getMock();
1481 $backend->expects( $this->once() )->method( 'makeKey' )
1482 ->willReturn( 'special' );
1483
1484 $wanCache = new WANObjectCache( [
1485 'cache' => $backend,
1486 'pool' => 'testcache-hash',
1487 'relayer' => new EventRelayerNull( [] )
1488 ] );
1489
1490 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1491 }
1492
1493 /**
1494 * @covers WANObjectCache::makeGlobalKey
1495 */
1496 public function testMakeGlobalKey() {
1497 $backend = $this->getMockBuilder( HashBagOStuff::class )
1498 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1499 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1500 ->willReturn( 'special' );
1501
1502 $wanCache = new WANObjectCache( [
1503 'cache' => $backend,
1504 'pool' => 'testcache-hash',
1505 'relayer' => new EventRelayerNull( [] )
1506 ] );
1507
1508 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1509 }
1510
1511 public static function statsKeyProvider() {
1512 return [
1513 [ 'domain:page:5', 'page' ],
1514 [ 'domain:main-key', 'main-key' ],
1515 [ 'domain:page:history', 'page' ],
1516 [ 'missingdomainkey', 'missingdomainkey' ]
1517 ];
1518 }
1519
1520 /**
1521 * @dataProvider statsKeyProvider
1522 * @covers WANObjectCache::determineKeyClass
1523 */
1524 public function testStatsKeyClass( $key, $class ) {
1525 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1526 'cache' => new HashBagOStuff,
1527 'pool' => 'testcache-hash',
1528 'relayer' => new EventRelayerNull( [] )
1529 ] ) );
1530
1531 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1532 }
1533 }
1534
1535 class TimeAdjustableHashBagOStuff extends HashBagOStuff {
1536 private $timeOverride = 0;
1537
1538 public function setTime( $time ) {
1539 $this->timeOverride = $time;
1540 }
1541
1542 protected function getCurrentTime() {
1543 return $this->timeOverride ?: parent::getCurrentTime();
1544 }
1545 }
1546
1547 class TimeAdjustableWANObjectCache extends WANObjectCache {
1548 private $timeOverride = 0;
1549
1550 public function setTime( $time ) {
1551 $this->timeOverride = $time;
1552 if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1553 $this->cache->setTime( $time );
1554 }
1555 }
1556
1557 public function addTime( $time ) {
1558 $this->timeOverride += $time;
1559 if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1560 $this->cache->setTime( $this->timeOverride );
1561 }
1562
1563 return $this->timeOverride;
1564 }
1565
1566 protected function getCurrentTime() {
1567 return $this->timeOverride ?: parent::getCurrentTime();
1568 }
1569 }
1570
1571 class NearExpiringWANObjectCache extends TimeAdjustableWANObjectCache {
1572 const CLOCK_SKEW = 1;
1573
1574 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1575 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1576 }
1577 }
1578
1579 class PopularityRefreshingWANObjectCache extends TimeAdjustableWANObjectCache {
1580 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1581 return ( ( $now - $asOf ) > $timeTillRefresh );
1582 }
1583 }