objectcache: respect process cache in getMultiWithSetCallback()
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
6 /** @var WANObjectCache */
7 private $cache;
8 /**@var BagOStuff */
9 private $internalCache;
10
11 protected function setUp() {
12 parent::setUp();
13
14 $this->cache = new WANObjectCache( [
15 'cache' => new HashBagOStuff(),
16 'pool' => 'testcache-hash',
17 'relayer' => new EventRelayerNull( [] )
18 ] );
19
20 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
21 /** @noinspection PhpUndefinedFieldInspection */
22 $this->internalCache = $wanCache->cache;
23 }
24
25 /**
26 * @dataProvider provideSetAndGet
27 * @covers WANObjectCache::set()
28 * @covers WANObjectCache::get()
29 * @covers WANObjectCache::makeKey()
30 * @param mixed $value
31 * @param integer $ttl
32 */
33 public function testSetAndGet( $value, $ttl ) {
34 $curTTL = null;
35 $asOf = null;
36 $key = $this->cache->makeKey( 'x', wfRandomString() );
37
38 $this->cache->get( $key, $curTTL, [], $asOf );
39 $this->assertNull( $curTTL, "Current TTL is null" );
40 $this->assertNull( $asOf, "Current as-of-time is infinite" );
41
42 $t = microtime( true );
43 $this->cache->set( $key, $value, $ttl );
44
45 $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
46 if ( is_infinite( $ttl ) || $ttl == 0 ) {
47 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
48 } else {
49 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
50 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
51 }
52 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
53 $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
54 }
55
56 public static function provideSetAndGet() {
57 return [
58 [ 14141, 3 ],
59 [ 3535.666, 3 ],
60 [ [], 3 ],
61 [ null, 3 ],
62 [ '0', 3 ],
63 [ (object)[ 'meow' ], 3 ],
64 [ INF, 3 ],
65 [ '', 3 ],
66 [ 'pizzacat', INF ],
67 ];
68 }
69
70 /**
71 * @covers WANObjectCache::get()
72 * @covers WANObjectCache::makeGlobalKey()
73 */
74 public function testGetNotExists() {
75 $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
76 $curTTL = null;
77 $value = $this->cache->get( $key, $curTTL );
78
79 $this->assertFalse( $value, "Non-existing key has false value" );
80 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
81 }
82
83 /**
84 * @covers WANObjectCache::set()
85 */
86 public function testSetOver() {
87 $key = wfRandomString();
88 for ( $i = 0; $i < 3; ++$i ) {
89 $value = wfRandomString();
90 $this->cache->set( $key, $value, 3 );
91
92 $this->assertEquals( $this->cache->get( $key ), $value );
93 }
94 }
95
96 /**
97 * @covers WANObjectCache::set()
98 */
99 public function testStaleSet() {
100 $key = wfRandomString();
101 $value = wfRandomString();
102 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
103
104 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
105 }
106
107 public function testProcessCache() {
108 $hit = 0;
109 $callback = function () use ( &$hit ) {
110 ++$hit;
111 return 42;
112 };
113 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
114 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
115
116 foreach ( $keys as $i => $key ) {
117 $this->cache->getWithSetCallback(
118 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
119 }
120 $this->assertEquals( 3, $hit );
121
122 foreach ( $keys as $i => $key ) {
123 $this->cache->getWithSetCallback(
124 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
125 }
126 $this->assertEquals( 3, $hit, "Values cached" );
127
128 foreach ( $keys as $i => $key ) {
129 $this->cache->getWithSetCallback(
130 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
131 }
132 $this->assertEquals( 6, $hit );
133
134 foreach ( $keys as $i => $key ) {
135 $this->cache->getWithSetCallback(
136 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
137 }
138 $this->assertEquals( 6, $hit, "New values cached" );
139
140 foreach ( $keys as $i => $key ) {
141 $this->cache->delete( $key );
142 $this->cache->getWithSetCallback(
143 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
144 }
145 $this->assertEquals( 9, $hit, "Values evicted" );
146
147 $key = reset( $keys );
148 // Get into cache
149 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
150 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
151 $this->assertEquals( 10, $hit, "Value cached" );
152 $outerCallback = function () use ( &$callback, $key ) {
153 $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
154
155 return 43 + $v;
156 };
157 $this->cache->getWithSetCallback( $key, 100, $outerCallback );
158 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
159 }
160
161 /**
162 * @dataProvider getWithSetCallback_provider
163 * @covers WANObjectCache::getWithSetCallback()
164 * @covers WANObjectCache::doGetWithSetCallback()
165 * @param array $extOpts
166 * @param bool $versioned
167 */
168 public function testGetWithSetCallback( array $extOpts, $versioned ) {
169 $cache = $this->cache;
170
171 $key = wfRandomString();
172 $value = wfRandomString();
173 $cKey1 = wfRandomString();
174 $cKey2 = wfRandomString();
175
176 $priorValue = null;
177 $priorAsOf = null;
178 $wasSet = 0;
179 $func = function( $old, &$ttl, &$opts, $asOf )
180 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
181 {
182 ++$wasSet;
183 $priorValue = $old;
184 $priorAsOf = $asOf;
185 $ttl = 20; // override with another value
186 return $value;
187 };
188
189 $wasSet = 0;
190 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
191 $this->assertEquals( $value, $v, "Value returned" );
192 $this->assertEquals( 1, $wasSet, "Value regenerated" );
193 $this->assertFalse( $priorValue, "No prior value" );
194 $this->assertNull( $priorAsOf, "No prior value" );
195
196 $curTTL = null;
197 $cache->get( $key, $curTTL );
198 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
199 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
200
201 $wasSet = 0;
202 $v = $cache->getWithSetCallback( $key, 30, $func, [
203 'lowTTL' => 0,
204 'lockTSE' => 5,
205 ] + $extOpts );
206 $this->assertEquals( $value, $v, "Value returned" );
207 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
208
209 $priorTime = microtime( true );
210 usleep( 1 );
211 $wasSet = 0;
212 $v = $cache->getWithSetCallback(
213 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
214 );
215 $this->assertEquals( $value, $v, "Value returned" );
216 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
217 $this->assertEquals( $value, $priorValue, "Has prior value" );
218 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
219 $t1 = $cache->getCheckKeyTime( $cKey1 );
220 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
221 $t2 = $cache->getCheckKeyTime( $cKey2 );
222 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
223
224 $priorTime = microtime( true );
225 $wasSet = 0;
226 $v = $cache->getWithSetCallback(
227 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
228 );
229 $this->assertEquals( $value, $v, "Value returned" );
230 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
231 $t1 = $cache->getCheckKeyTime( $cKey1 );
232 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
233 $t2 = $cache->getCheckKeyTime( $cKey2 );
234 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
235
236 $curTTL = null;
237 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
238 if ( $versioned ) {
239 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
240 } else {
241 $this->assertEquals( $value, $v, "Value returned" );
242 }
243 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
244
245 $wasSet = 0;
246 $key = wfRandomString();
247 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
248 $this->assertEquals( $value, $v, "Value returned" );
249 $cache->delete( $key );
250 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
251 $this->assertEquals( $value, $v, "Value still returned after deleted" );
252 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
253 }
254
255 public static function getWithSetCallback_provider() {
256 return [
257 [ [], false ],
258 [ [ 'version' => 1 ], true ]
259 ];
260 }
261
262 /**
263 * @dataProvider getMultiWithSetCallback_provider
264 * @covers WANObjectCache::getMultiWithSetCallback()
265 * @covers WANObjectCache::makeMultiKeys()
266 * @param array $extOpts
267 * @param bool $versioned
268 */
269 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
270 $cache = $this->cache;
271
272 $keyA = wfRandomString();
273 $keyB = wfRandomString();
274 $keyC = wfRandomString();
275 $cKey1 = wfRandomString();
276 $cKey2 = wfRandomString();
277
278 $priorValue = null;
279 $priorAsOf = null;
280 $wasSet = 0;
281 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
282 &$wasSet, &$priorValue, &$priorAsOf
283 ) {
284 ++$wasSet;
285 $priorValue = $old;
286 $priorAsOf = $asOf;
287 $ttl = 20; // override with another value
288 return "@$id$";
289 };
290
291 $wasSet = 0;
292 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
293 $value = "@3353$";
294 $v = $cache->getMultiWithSetCallback(
295 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
296 $this->assertEquals( $value, $v[$keyA], "Value returned" );
297 $this->assertEquals( 1, $wasSet, "Value regenerated" );
298 $this->assertFalse( $priorValue, "No prior value" );
299 $this->assertNull( $priorAsOf, "No prior value" );
300
301 $curTTL = null;
302 $cache->get( $keyA, $curTTL );
303 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
304 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
305
306 $wasSet = 0;
307 $value = "@efef$";
308 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
309 $v = $cache->getMultiWithSetCallback(
310 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
311 $this->assertEquals( $value, $v[$keyB], "Value returned" );
312 $this->assertEquals( 1, $wasSet, "Value regenerated" );
313 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
314 $v = $cache->getMultiWithSetCallback(
315 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
316 $this->assertEquals( $value, $v[$keyB], "Value returned" );
317 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
318 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
319
320 $priorTime = microtime( true );
321 usleep( 1 );
322 $wasSet = 0;
323 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
324 $v = $cache->getMultiWithSetCallback(
325 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
326 );
327 $this->assertEquals( $value, $v[$keyB], "Value returned" );
328 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
329 $this->assertEquals( $value, $priorValue, "Has prior value" );
330 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
331 $t1 = $cache->getCheckKeyTime( $cKey1 );
332 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
333 $t2 = $cache->getCheckKeyTime( $cKey2 );
334 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
335
336 $priorTime = microtime( true );
337 $value = "@43636$";
338 $wasSet = 0;
339 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
340 $v = $cache->getMultiWithSetCallback(
341 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
342 );
343 $this->assertEquals( $value, $v[$keyC], "Value returned" );
344 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
345 $t1 = $cache->getCheckKeyTime( $cKey1 );
346 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
347 $t2 = $cache->getCheckKeyTime( $cKey2 );
348 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
349
350 $curTTL = null;
351 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
352 if ( $versioned ) {
353 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
354 } else {
355 $this->assertEquals( $value, $v, "Value returned" );
356 }
357 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
358
359 $wasSet = 0;
360 $key = wfRandomString();
361 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
362 $v = $cache->getMultiWithSetCallback(
363 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
364 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
365 $cache->delete( $key );
366 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
367 $v = $cache->getMultiWithSetCallback(
368 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
369 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
370 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
371
372 $calls = 0;
373 $ids = [ 1, 2, 3, 4, 5, 6 ];
374 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
375 return $wanCache->makeKey( 'test', $id );
376 };
377 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
378 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
379 ++$calls;
380
381 return "val-{$id}";
382 };
383 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
384
385 $this->assertEquals(
386 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
387 array_values( $values ),
388 "Correct values in correct order"
389 );
390 $this->assertEquals(
391 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
392 array_keys( $values ),
393 "Correct keys in correct order"
394 );
395 $this->assertEquals( count( $ids ), $calls );
396
397 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
398 $this->assertEquals( count( $ids ), $calls, "Values cached" );
399
400 // Mock the BagOStuff to assure only one getMulti() call given process caching
401 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
402 ->setMethods( [ 'getMulti' ] )->getMock();
403 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
404 WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
405 WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
406 ] );
407 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
408
409 // Warm the process cache
410 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
411 $this->assertEquals(
412 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
413 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
414 );
415 // Use the process cache
416 $this->assertEquals(
417 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
418 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
419 );
420 }
421
422 public static function getMultiWithSetCallback_provider() {
423 return [
424 [ [], false ],
425 [ [ 'version' => 1 ], true ]
426 ];
427 }
428
429 /**
430 * @covers WANObjectCache::getWithSetCallback()
431 * @covers WANObjectCache::doGetWithSetCallback()
432 */
433 public function testLockTSE() {
434 $cache = $this->cache;
435 $key = wfRandomString();
436 $value = wfRandomString();
437
438 $calls = 0;
439 $func = function() use ( &$calls, $value, $cache, $key ) {
440 ++$calls;
441 // Immediately kill any mutex rather than waiting a second
442 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
443 return $value;
444 };
445
446 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
447 $this->assertEquals( $value, $ret );
448 $this->assertEquals( 1, $calls, 'Value was populated' );
449
450 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
451 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
452
453 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
454 $ret = $cache->getWithSetCallback( $key, 30, $func,
455 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
456 $this->assertEquals( $value, $ret, 'Old value used' );
457 $this->assertEquals( 1, $calls, 'Callback was not used' );
458
459 $cache->delete( $key );
460 $ret = $cache->getWithSetCallback( $key, 30, $func,
461 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
462 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
463 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
464
465 $ret = $cache->getWithSetCallback( $key, 30, $func,
466 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
467 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
468 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
469 }
470
471 /**
472 * @covers WANObjectCache::getWithSetCallback()
473 * @covers WANObjectCache::doGetWithSetCallback()
474 */
475 public function testLockTSESlow() {
476 $cache = $this->cache;
477 $key = wfRandomString();
478 $value = wfRandomString();
479
480 $calls = 0;
481 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
482 ++$calls;
483 $setOpts['since'] = microtime( true ) - 10;
484 // Immediately kill any mutex rather than waiting a second
485 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
486 return $value;
487 };
488
489 // Value should be marked as stale due to snapshot lag
490 $curTTL = null;
491 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
492 $this->assertEquals( $value, $ret );
493 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
494 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
495 $this->assertEquals( 1, $calls, 'Value was generated' );
496
497 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
498 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
499 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
500 $this->assertEquals( $value, $ret );
501 $this->assertEquals( 1, $calls, 'Callback was not used' );
502 }
503
504 /**
505 * @covers WANObjectCache::getWithSetCallback()
506 * @covers WANObjectCache::doGetWithSetCallback()
507 */
508 public function testBusyValue() {
509 $cache = $this->cache;
510 $key = wfRandomString();
511 $value = wfRandomString();
512 $busyValue = wfRandomString();
513
514 $calls = 0;
515 $func = function() use ( &$calls, $value, $cache, $key ) {
516 ++$calls;
517 // Immediately kill any mutex rather than waiting a second
518 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
519 return $value;
520 };
521
522 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
523 $this->assertEquals( $value, $ret );
524 $this->assertEquals( 1, $calls, 'Value was populated' );
525
526 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
527 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
528
529 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
530 $ret = $cache->getWithSetCallback( $key, 30, $func,
531 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
532 $this->assertEquals( $value, $ret, 'Callback used' );
533 $this->assertEquals( 2, $calls, 'Callback used' );
534
535 $ret = $cache->getWithSetCallback( $key, 30, $func,
536 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
537 $this->assertEquals( $value, $ret, 'Old value used' );
538 $this->assertEquals( 2, $calls, 'Callback was not used' );
539
540 $cache->delete( $key ); // no value at all anymore and still locked
541 $ret = $cache->getWithSetCallback( $key, 30, $func,
542 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
543 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
544 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
545
546 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
547 $ret = $cache->getWithSetCallback( $key, 30, $func,
548 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
549 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
550 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
551
552 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
553 $ret = $cache->getWithSetCallback( $key, 30, $func,
554 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
555 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
556 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
557 }
558
559 /**
560 * @covers WANObjectCache::getMulti()
561 */
562 public function testGetMulti() {
563 $cache = $this->cache;
564
565 $value1 = [ 'this' => 'is', 'a' => 'test' ];
566 $value2 = [ 'this' => 'is', 'another' => 'test' ];
567
568 $key1 = wfRandomString();
569 $key2 = wfRandomString();
570 $key3 = wfRandomString();
571
572 $cache->set( $key1, $value1, 5 );
573 $cache->set( $key2, $value2, 10 );
574
575 $curTTLs = [];
576 $this->assertEquals(
577 [ $key1 => $value1, $key2 => $value2 ],
578 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
579 'Result array populated'
580 );
581
582 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
583 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
584 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
585
586 $cKey1 = wfRandomString();
587 $cKey2 = wfRandomString();
588
589 $priorTime = microtime( true );
590 usleep( 1 );
591 $curTTLs = [];
592 $this->assertEquals(
593 [ $key1 => $value1, $key2 => $value2 ],
594 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
595 "Result array populated even with new check keys"
596 );
597 $t1 = $cache->getCheckKeyTime( $cKey1 );
598 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
599 $t2 = $cache->getCheckKeyTime( $cKey2 );
600 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
601 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
602 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
603 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
604
605 usleep( 1 );
606 $curTTLs = [];
607 $this->assertEquals(
608 [ $key1 => $value1, $key2 => $value2 ],
609 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
610 "Result array still populated even with new check keys"
611 );
612 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
613 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
614 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
615 }
616
617 /**
618 * @covers WANObjectCache::getMulti()
619 * @covers WANObjectCache::processCheckKeys()
620 */
621 public function testGetMultiCheckKeys() {
622 $cache = $this->cache;
623
624 $checkAll = wfRandomString();
625 $check1 = wfRandomString();
626 $check2 = wfRandomString();
627 $check3 = wfRandomString();
628 $value1 = wfRandomString();
629 $value2 = wfRandomString();
630
631 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
632 // several seconds during the test to assert the behaviour.
633 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
634 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
635 }
636 usleep( 100 );
637
638 $cache->set( 'key1', $value1, 10 );
639 $cache->set( 'key2', $value2, 10 );
640
641 $curTTLs = [];
642 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
643 'key1' => $check1,
644 $checkAll,
645 'key2' => $check2,
646 'key3' => $check3,
647 ] );
648 $this->assertEquals(
649 [ 'key1' => $value1, 'key2' => $value2 ],
650 $result,
651 'Initial values'
652 );
653 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
654 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
655 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
656 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
657
658 $cache->touchCheckKey( $check1 );
659
660 $curTTLs = [];
661 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
662 'key1' => $check1,
663 $checkAll,
664 'key2' => $check2,
665 'key3' => $check3,
666 ] );
667 $this->assertEquals(
668 [ 'key1' => $value1, 'key2' => $value2 ],
669 $result,
670 'key1 expired by check1, but value still provided'
671 );
672 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
673 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
674
675 $cache->touchCheckKey( $checkAll );
676
677 $curTTLs = [];
678 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
679 'key1' => $check1,
680 $checkAll,
681 'key2' => $check2,
682 'key3' => $check3,
683 ] );
684 $this->assertEquals(
685 [ 'key1' => $value1, 'key2' => $value2 ],
686 $result,
687 'All keys expired by checkAll, but value still provided'
688 );
689 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
690 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
691 }
692
693 /**
694 * @covers WANObjectCache::get()
695 * @covers WANObjectCache::processCheckKeys()
696 */
697 public function testCheckKeyInitHoldoff() {
698 $cache = $this->cache;
699
700 for ( $i = 0; $i < 500; ++$i ) {
701 $key = wfRandomString();
702 $checkKey = wfRandomString();
703 // miss, set, hit
704 $cache->get( $key, $curTTL, [ $checkKey ] );
705 $cache->set( $key, 'val', 10 );
706 $curTTL = null;
707 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
708
709 $this->assertEquals( 'val', $v );
710 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
711 }
712
713 for ( $i = 0; $i < 500; ++$i ) {
714 $key = wfRandomString();
715 $checkKey = wfRandomString();
716 // set, hit
717 $cache->set( $key, 'val', 10 );
718 $curTTL = null;
719 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
720
721 $this->assertEquals( 'val', $v );
722 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
723 }
724 }
725
726 /**
727 * @covers WANObjectCache::delete()
728 */
729 public function testDelete() {
730 $key = wfRandomString();
731 $value = wfRandomString();
732 $this->cache->set( $key, $value );
733
734 $curTTL = null;
735 $v = $this->cache->get( $key, $curTTL );
736 $this->assertEquals( $value, $v, "Key was created with value" );
737 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
738
739 $this->cache->delete( $key );
740
741 $curTTL = null;
742 $v = $this->cache->get( $key, $curTTL );
743 $this->assertFalse( $v, "Deleted key has false value" );
744 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
745
746 $this->cache->set( $key, $value . 'more' );
747 $v = $this->cache->get( $key, $curTTL );
748 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
749 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
750
751 $this->cache->set( $key, $value );
752 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
753
754 $curTTL = null;
755 $v = $this->cache->get( $key, $curTTL );
756 $this->assertFalse( $v, "Deleted key has false value" );
757 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
758
759 $this->cache->set( $key, $value );
760 $v = $this->cache->get( $key, $curTTL );
761 $this->assertEquals( $value, $v, "Key was created with value" );
762 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
763 }
764
765 /**
766 * @dataProvider getWithSetCallback_versions_provider
767 * @param array $extOpts
768 * @param $versioned
769 */
770 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
771 $cache = $this->cache;
772
773 $key = wfRandomString();
774 $value = wfRandomString();
775
776 $wasSet = 0;
777 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
778 ++$wasSet;
779 return $value;
780 };
781
782 // Set the main key (version N if versioned)
783 $wasSet = 0;
784 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
785 $this->assertEquals( $value, $v, "Value returned" );
786 $this->assertEquals( 1, $wasSet, "Value regenerated" );
787 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
788 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
789 // Set the key for version N+1 (if versioned)
790 if ( $versioned ) {
791 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
792
793 $wasSet = 0;
794 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
795 $this->assertEquals( $value, $v, "Value returned" );
796 $this->assertEquals( 1, $wasSet, "Value regenerated" );
797
798 $wasSet = 0;
799 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
800 $this->assertEquals( $value, $v, "Value returned" );
801 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
802 }
803
804 $wasSet = 0;
805 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
806 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
807
808 $wasSet = 0;
809 $cache->delete( $key );
810 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
811 $this->assertEquals( $value, $v, "Value returned" );
812 $this->assertEquals( 1, $wasSet, "Value regenerated" );
813
814 if ( $versioned ) {
815 $wasSet = 0;
816 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
817 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
818 $this->assertEquals( $value, $v, "Value returned" );
819 $this->assertEquals( 1, $wasSet, "Value regenerated" );
820 }
821 }
822
823 public static function getWithSetCallback_versions_provider() {
824 return [
825 [ [], false ],
826 [ [ 'version' => 1 ], true ]
827 ];
828 }
829
830 /**
831 * @covers WANObjectCache::touchCheckKey()
832 * @covers WANObjectCache::resetCheckKey()
833 * @covers WANObjectCache::getCheckKeyTime()
834 */
835 public function testTouchKeys() {
836 $key = wfRandomString();
837
838 $priorTime = microtime( true );
839 usleep( 100 );
840 $t0 = $this->cache->getCheckKeyTime( $key );
841 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
842
843 $priorTime = microtime( true );
844 usleep( 100 );
845 $this->cache->touchCheckKey( $key );
846 $t1 = $this->cache->getCheckKeyTime( $key );
847 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
848
849 $t2 = $this->cache->getCheckKeyTime( $key );
850 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
851
852 usleep( 100 );
853 $this->cache->touchCheckKey( $key );
854 $t3 = $this->cache->getCheckKeyTime( $key );
855 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
856
857 $t4 = $this->cache->getCheckKeyTime( $key );
858 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
859
860 usleep( 100 );
861 $this->cache->resetCheckKey( $key );
862 $t5 = $this->cache->getCheckKeyTime( $key );
863 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
864
865 $t6 = $this->cache->getCheckKeyTime( $key );
866 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
867 }
868
869 /**
870 * @covers WANObjectCache::getMulti()
871 */
872 public function testGetWithSeveralCheckKeys() {
873 $key = wfRandomString();
874 $tKey1 = wfRandomString();
875 $tKey2 = wfRandomString();
876 $value = 'meow';
877
878 // Two check keys are newer (given hold-off) than $key, another is older
879 $this->internalCache->set(
880 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
881 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
882 );
883 $this->internalCache->set(
884 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
885 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
886 );
887 $this->internalCache->set(
888 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
889 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
890 );
891 $this->cache->set( $key, $value, 30 );
892
893 $curTTL = null;
894 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
895 $this->assertEquals( $value, $v, "Value matches" );
896 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
897 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
898 }
899
900 /**
901 * @covers WANObjectCache::reap()
902 * @covers WANObjectCache::reapCheckKey()
903 */
904 public function testReap() {
905 $vKey1 = wfRandomString();
906 $vKey2 = wfRandomString();
907 $tKey1 = wfRandomString();
908 $tKey2 = wfRandomString();
909 $value = 'moo';
910
911 $knownPurge = time() - 60;
912 $goodTime = microtime( true ) - 5;
913 $badTime = microtime( true ) - 300;
914
915 $this->internalCache->set(
916 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
917 [
918 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
919 WANObjectCache::FLD_VALUE => $value,
920 WANObjectCache::FLD_TTL => 3600,
921 WANObjectCache::FLD_TIME => $goodTime
922 ]
923 );
924 $this->internalCache->set(
925 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
926 [
927 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
928 WANObjectCache::FLD_VALUE => $value,
929 WANObjectCache::FLD_TTL => 3600,
930 WANObjectCache::FLD_TIME => $badTime
931 ]
932 );
933 $this->internalCache->set(
934 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
935 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
936 );
937 $this->internalCache->set(
938 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
939 WANObjectCache::PURGE_VAL_PREFIX . $badTime
940 );
941
942 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
943 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
944 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
945 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
946
947 $this->assertFalse( $bad1 );
948 $this->assertTrue( $bad2 );
949
950 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
951 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
952 $this->assertFalse( $tBad1 );
953 $this->assertTrue( $tBad2 );
954 }
955
956 /**
957 * @covers WANObjectCache::set()
958 */
959 public function testSetWithLag() {
960 $value = 1;
961
962 $key = wfRandomString();
963 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
964 $this->cache->set( $key, $value, 30, $opts );
965 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
966
967 $key = wfRandomString();
968 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
969 $this->cache->set( $key, $value, 30, $opts );
970 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
971
972 $key = wfRandomString();
973 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
974 $this->cache->set( $key, $value, 30, $opts );
975 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
976 }
977
978 /**
979 * @covers WANObjectCache::set()
980 */
981 public function testWritePending() {
982 $value = 1;
983
984 $key = wfRandomString();
985 $opts = [ 'pending' => true ];
986 $this->cache->set( $key, $value, 30, $opts );
987 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
988 }
989
990 public function testMcRouterSupport() {
991 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
992 ->setMethods( [ 'set', 'delete' ] )->getMock();
993 $localBag->expects( $this->never() )->method( 'set' );
994 $localBag->expects( $this->never() )->method( 'delete' );
995 $wanCache = new WANObjectCache( [
996 'cache' => $localBag,
997 'pool' => 'testcache-hash',
998 'relayer' => new EventRelayerNull( [] )
999 ] );
1000 $valFunc = function () {
1001 return 1;
1002 };
1003
1004 // None of these should use broadcasting commands (e.g. SET, DELETE)
1005 $wanCache->get( 'x' );
1006 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1007 $wanCache->getMulti( [ 'x', 'y' ] );
1008 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1009 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1010 $wanCache->getCheckKeyTime( 'zzz' );
1011 $wanCache->reap( 'x', time() - 300 );
1012 $wanCache->reap( 'zzz', time() - 300 );
1013 }
1014
1015 /**
1016 * @dataProvider provideAdaptiveTTL
1017 * @covers WANObjectCache::adaptiveTTL()
1018 * @param float|int $ago
1019 * @param int $maxTTL
1020 * @param int $minTTL
1021 * @param float $factor
1022 * @param int $adaptiveTTL
1023 */
1024 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1025 $mtime = $ago ? time() - $ago : $ago;
1026 $margin = 5;
1027 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1028
1029 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1030 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1031
1032 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1033
1034 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1035 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1036 }
1037
1038 public static function provideAdaptiveTTL() {
1039 return [
1040 [ 3600, 900, 30, .2, 720 ],
1041 [ 3600, 500, 30, .2, 500 ],
1042 [ 3600, 86400, 800, .2, 800 ],
1043 [ false, 86400, 800, .2, 800 ],
1044 [ null, 86400, 800, .2, 800 ]
1045 ];
1046 }
1047 }