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