objectcache: fix cache warmup bug 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
401 public static function getMultiWithSetCallback_provider() {
402 return [
403 [ [], false ],
404 [ [ 'version' => 1 ], true ]
405 ];
406 }
407
408 /**
409 * @covers WANObjectCache::getWithSetCallback()
410 * @covers WANObjectCache::doGetWithSetCallback()
411 */
412 public function testLockTSE() {
413 $cache = $this->cache;
414 $key = wfRandomString();
415 $value = wfRandomString();
416
417 $calls = 0;
418 $func = function() use ( &$calls, $value, $cache, $key ) {
419 ++$calls;
420 // Immediately kill any mutex rather than waiting a second
421 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
422 return $value;
423 };
424
425 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
426 $this->assertEquals( $value, $ret );
427 $this->assertEquals( 1, $calls, 'Value was populated' );
428
429 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
430 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
431
432 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
433 $ret = $cache->getWithSetCallback( $key, 30, $func,
434 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
435 $this->assertEquals( $value, $ret, 'Old value used' );
436 $this->assertEquals( 1, $calls, 'Callback was not used' );
437
438 $cache->delete( $key );
439 $ret = $cache->getWithSetCallback( $key, 30, $func,
440 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
441 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
442 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
443
444 $ret = $cache->getWithSetCallback( $key, 30, $func,
445 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
446 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
447 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
448 }
449
450 /**
451 * @covers WANObjectCache::getWithSetCallback()
452 * @covers WANObjectCache::doGetWithSetCallback()
453 */
454 public function testLockTSESlow() {
455 $cache = $this->cache;
456 $key = wfRandomString();
457 $value = wfRandomString();
458
459 $calls = 0;
460 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
461 ++$calls;
462 $setOpts['since'] = microtime( true ) - 10;
463 // Immediately kill any mutex rather than waiting a second
464 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
465 return $value;
466 };
467
468 // Value should be marked as stale due to snapshot lag
469 $curTTL = null;
470 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
471 $this->assertEquals( $value, $ret );
472 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
473 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
474 $this->assertEquals( 1, $calls, 'Value was generated' );
475
476 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
477 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
478 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
479 $this->assertEquals( $value, $ret );
480 $this->assertEquals( 1, $calls, 'Callback was not used' );
481 }
482
483 /**
484 * @covers WANObjectCache::getWithSetCallback()
485 * @covers WANObjectCache::doGetWithSetCallback()
486 */
487 public function testBusyValue() {
488 $cache = $this->cache;
489 $key = wfRandomString();
490 $value = wfRandomString();
491 $busyValue = wfRandomString();
492
493 $calls = 0;
494 $func = function() use ( &$calls, $value, $cache, $key ) {
495 ++$calls;
496 // Immediately kill any mutex rather than waiting a second
497 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
498 return $value;
499 };
500
501 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
502 $this->assertEquals( $value, $ret );
503 $this->assertEquals( 1, $calls, 'Value was populated' );
504
505 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
506 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
507
508 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
509 $ret = $cache->getWithSetCallback( $key, 30, $func,
510 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
511 $this->assertEquals( $value, $ret, 'Callback used' );
512 $this->assertEquals( 2, $calls, 'Callback used' );
513
514 $ret = $cache->getWithSetCallback( $key, 30, $func,
515 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
516 $this->assertEquals( $value, $ret, 'Old value used' );
517 $this->assertEquals( 2, $calls, 'Callback was not used' );
518
519 $cache->delete( $key ); // no value at all anymore and still locked
520 $ret = $cache->getWithSetCallback( $key, 30, $func,
521 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
522 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
523 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
524
525 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
526 $ret = $cache->getWithSetCallback( $key, 30, $func,
527 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
528 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
529 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
530
531 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
532 $ret = $cache->getWithSetCallback( $key, 30, $func,
533 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
534 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
535 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
536 }
537
538 /**
539 * @covers WANObjectCache::getMulti()
540 */
541 public function testGetMulti() {
542 $cache = $this->cache;
543
544 $value1 = [ 'this' => 'is', 'a' => 'test' ];
545 $value2 = [ 'this' => 'is', 'another' => 'test' ];
546
547 $key1 = wfRandomString();
548 $key2 = wfRandomString();
549 $key3 = wfRandomString();
550
551 $cache->set( $key1, $value1, 5 );
552 $cache->set( $key2, $value2, 10 );
553
554 $curTTLs = [];
555 $this->assertEquals(
556 [ $key1 => $value1, $key2 => $value2 ],
557 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
558 'Result array populated'
559 );
560
561 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
562 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
563 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
564
565 $cKey1 = wfRandomString();
566 $cKey2 = wfRandomString();
567
568 $priorTime = microtime( true );
569 usleep( 1 );
570 $curTTLs = [];
571 $this->assertEquals(
572 [ $key1 => $value1, $key2 => $value2 ],
573 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
574 "Result array populated even with new check keys"
575 );
576 $t1 = $cache->getCheckKeyTime( $cKey1 );
577 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
578 $t2 = $cache->getCheckKeyTime( $cKey2 );
579 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
580 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
581 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
582 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
583
584 usleep( 1 );
585 $curTTLs = [];
586 $this->assertEquals(
587 [ $key1 => $value1, $key2 => $value2 ],
588 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
589 "Result array still populated even with new check keys"
590 );
591 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
592 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
593 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
594 }
595
596 /**
597 * @covers WANObjectCache::getMulti()
598 * @covers WANObjectCache::processCheckKeys()
599 */
600 public function testGetMultiCheckKeys() {
601 $cache = $this->cache;
602
603 $checkAll = wfRandomString();
604 $check1 = wfRandomString();
605 $check2 = wfRandomString();
606 $check3 = wfRandomString();
607 $value1 = wfRandomString();
608 $value2 = wfRandomString();
609
610 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
611 // several seconds during the test to assert the behaviour.
612 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
613 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
614 }
615 usleep( 100 );
616
617 $cache->set( 'key1', $value1, 10 );
618 $cache->set( 'key2', $value2, 10 );
619
620 $curTTLs = [];
621 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
622 'key1' => $check1,
623 $checkAll,
624 'key2' => $check2,
625 'key3' => $check3,
626 ] );
627 $this->assertEquals(
628 [ 'key1' => $value1, 'key2' => $value2 ],
629 $result,
630 'Initial values'
631 );
632 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
633 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
634 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
635 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
636
637 $cache->touchCheckKey( $check1 );
638
639 $curTTLs = [];
640 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
641 'key1' => $check1,
642 $checkAll,
643 'key2' => $check2,
644 'key3' => $check3,
645 ] );
646 $this->assertEquals(
647 [ 'key1' => $value1, 'key2' => $value2 ],
648 $result,
649 'key1 expired by check1, but value still provided'
650 );
651 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
652 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
653
654 $cache->touchCheckKey( $checkAll );
655
656 $curTTLs = [];
657 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
658 'key1' => $check1,
659 $checkAll,
660 'key2' => $check2,
661 'key3' => $check3,
662 ] );
663 $this->assertEquals(
664 [ 'key1' => $value1, 'key2' => $value2 ],
665 $result,
666 'All keys expired by checkAll, but value still provided'
667 );
668 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
669 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
670 }
671
672 /**
673 * @covers WANObjectCache::get()
674 * @covers WANObjectCache::processCheckKeys()
675 */
676 public function testCheckKeyInitHoldoff() {
677 $cache = $this->cache;
678
679 for ( $i = 0; $i < 500; ++$i ) {
680 $key = wfRandomString();
681 $checkKey = wfRandomString();
682 // miss, set, hit
683 $cache->get( $key, $curTTL, [ $checkKey ] );
684 $cache->set( $key, 'val', 10 );
685 $curTTL = null;
686 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
687
688 $this->assertEquals( 'val', $v );
689 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
690 }
691
692 for ( $i = 0; $i < 500; ++$i ) {
693 $key = wfRandomString();
694 $checkKey = wfRandomString();
695 // set, hit
696 $cache->set( $key, 'val', 10 );
697 $curTTL = null;
698 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
699
700 $this->assertEquals( 'val', $v );
701 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
702 }
703 }
704
705 /**
706 * @covers WANObjectCache::delete()
707 */
708 public function testDelete() {
709 $key = wfRandomString();
710 $value = wfRandomString();
711 $this->cache->set( $key, $value );
712
713 $curTTL = null;
714 $v = $this->cache->get( $key, $curTTL );
715 $this->assertEquals( $value, $v, "Key was created with value" );
716 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
717
718 $this->cache->delete( $key );
719
720 $curTTL = null;
721 $v = $this->cache->get( $key, $curTTL );
722 $this->assertFalse( $v, "Deleted key has false value" );
723 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
724
725 $this->cache->set( $key, $value . 'more' );
726 $v = $this->cache->get( $key, $curTTL );
727 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
728 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
729
730 $this->cache->set( $key, $value );
731 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
732
733 $curTTL = null;
734 $v = $this->cache->get( $key, $curTTL );
735 $this->assertFalse( $v, "Deleted key has false value" );
736 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
737
738 $this->cache->set( $key, $value );
739 $v = $this->cache->get( $key, $curTTL );
740 $this->assertEquals( $value, $v, "Key was created with value" );
741 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
742 }
743
744 /**
745 * @dataProvider getWithSetCallback_versions_provider
746 * @param array $extOpts
747 * @param $versioned
748 */
749 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
750 $cache = $this->cache;
751
752 $key = wfRandomString();
753 $value = wfRandomString();
754
755 $wasSet = 0;
756 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
757 ++$wasSet;
758 return $value;
759 };
760
761 // Set the main key (version N if versioned)
762 $wasSet = 0;
763 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
764 $this->assertEquals( $value, $v, "Value returned" );
765 $this->assertEquals( 1, $wasSet, "Value regenerated" );
766 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
767 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
768 // Set the key for version N+1 (if versioned)
769 if ( $versioned ) {
770 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
771
772 $wasSet = 0;
773 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
774 $this->assertEquals( $value, $v, "Value returned" );
775 $this->assertEquals( 1, $wasSet, "Value regenerated" );
776
777 $wasSet = 0;
778 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
779 $this->assertEquals( $value, $v, "Value returned" );
780 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
781 }
782
783 $wasSet = 0;
784 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
785 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
786
787 $wasSet = 0;
788 $cache->delete( $key );
789 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
790 $this->assertEquals( $value, $v, "Value returned" );
791 $this->assertEquals( 1, $wasSet, "Value regenerated" );
792
793 if ( $versioned ) {
794 $wasSet = 0;
795 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
796 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
797 $this->assertEquals( $value, $v, "Value returned" );
798 $this->assertEquals( 1, $wasSet, "Value regenerated" );
799 }
800 }
801
802 public static function getWithSetCallback_versions_provider() {
803 return [
804 [ [], false ],
805 [ [ 'version' => 1 ], true ]
806 ];
807 }
808
809 /**
810 * @covers WANObjectCache::touchCheckKey()
811 * @covers WANObjectCache::resetCheckKey()
812 * @covers WANObjectCache::getCheckKeyTime()
813 */
814 public function testTouchKeys() {
815 $key = wfRandomString();
816
817 $priorTime = microtime( true );
818 usleep( 100 );
819 $t0 = $this->cache->getCheckKeyTime( $key );
820 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
821
822 $priorTime = microtime( true );
823 usleep( 100 );
824 $this->cache->touchCheckKey( $key );
825 $t1 = $this->cache->getCheckKeyTime( $key );
826 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
827
828 $t2 = $this->cache->getCheckKeyTime( $key );
829 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
830
831 usleep( 100 );
832 $this->cache->touchCheckKey( $key );
833 $t3 = $this->cache->getCheckKeyTime( $key );
834 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
835
836 $t4 = $this->cache->getCheckKeyTime( $key );
837 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
838
839 usleep( 100 );
840 $this->cache->resetCheckKey( $key );
841 $t5 = $this->cache->getCheckKeyTime( $key );
842 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
843
844 $t6 = $this->cache->getCheckKeyTime( $key );
845 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
846 }
847
848 /**
849 * @covers WANObjectCache::getMulti()
850 */
851 public function testGetWithSeveralCheckKeys() {
852 $key = wfRandomString();
853 $tKey1 = wfRandomString();
854 $tKey2 = wfRandomString();
855 $value = 'meow';
856
857 // Two check keys are newer (given hold-off) than $key, another is older
858 $this->internalCache->set(
859 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
860 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
861 );
862 $this->internalCache->set(
863 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
864 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
865 );
866 $this->internalCache->set(
867 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
868 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
869 );
870 $this->cache->set( $key, $value, 30 );
871
872 $curTTL = null;
873 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
874 $this->assertEquals( $value, $v, "Value matches" );
875 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
876 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
877 }
878
879 /**
880 * @covers WANObjectCache::reap()
881 * @covers WANObjectCache::reapCheckKey()
882 */
883 public function testReap() {
884 $vKey1 = wfRandomString();
885 $vKey2 = wfRandomString();
886 $tKey1 = wfRandomString();
887 $tKey2 = wfRandomString();
888 $value = 'moo';
889
890 $knownPurge = time() - 60;
891 $goodTime = microtime( true ) - 5;
892 $badTime = microtime( true ) - 300;
893
894 $this->internalCache->set(
895 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
896 [
897 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
898 WANObjectCache::FLD_VALUE => $value,
899 WANObjectCache::FLD_TTL => 3600,
900 WANObjectCache::FLD_TIME => $goodTime
901 ]
902 );
903 $this->internalCache->set(
904 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
905 [
906 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
907 WANObjectCache::FLD_VALUE => $value,
908 WANObjectCache::FLD_TTL => 3600,
909 WANObjectCache::FLD_TIME => $badTime
910 ]
911 );
912 $this->internalCache->set(
913 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
914 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
915 );
916 $this->internalCache->set(
917 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
918 WANObjectCache::PURGE_VAL_PREFIX . $badTime
919 );
920
921 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
922 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
923 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
924 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
925
926 $this->assertFalse( $bad1 );
927 $this->assertTrue( $bad2 );
928
929 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
930 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
931 $this->assertFalse( $tBad1 );
932 $this->assertTrue( $tBad2 );
933 }
934
935 /**
936 * @covers WANObjectCache::set()
937 */
938 public function testSetWithLag() {
939 $value = 1;
940
941 $key = wfRandomString();
942 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
943 $this->cache->set( $key, $value, 30, $opts );
944 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
945
946 $key = wfRandomString();
947 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
948 $this->cache->set( $key, $value, 30, $opts );
949 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
950
951 $key = wfRandomString();
952 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
953 $this->cache->set( $key, $value, 30, $opts );
954 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
955 }
956
957 /**
958 * @covers WANObjectCache::set()
959 */
960 public function testWritePending() {
961 $value = 1;
962
963 $key = wfRandomString();
964 $opts = [ 'pending' => true ];
965 $this->cache->set( $key, $value, 30, $opts );
966 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
967 }
968
969 public function testMcRouterSupport() {
970 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
971 ->setMethods( [ 'set', 'delete' ] )->getMock();
972 $localBag->expects( $this->never() )->method( 'set' );
973 $localBag->expects( $this->never() )->method( 'delete' );
974 $wanCache = new WANObjectCache( [
975 'cache' => $localBag,
976 'pool' => 'testcache-hash',
977 'relayer' => new EventRelayerNull( [] )
978 ] );
979 $valFunc = function () {
980 return 1;
981 };
982
983 // None of these should use broadcasting commands (e.g. SET, DELETE)
984 $wanCache->get( 'x' );
985 $wanCache->get( 'x', $ctl, [ 'check1' ] );
986 $wanCache->getMulti( [ 'x', 'y' ] );
987 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
988 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
989 $wanCache->getCheckKeyTime( 'zzz' );
990 $wanCache->reap( 'x', time() - 300 );
991 $wanCache->reap( 'zzz', time() - 300 );
992 }
993
994 /**
995 * @dataProvider provideAdaptiveTTL
996 * @covers WANObjectCache::adaptiveTTL()
997 * @param float|int $ago
998 * @param int $maxTTL
999 * @param int $minTTL
1000 * @param float $factor
1001 * @param int $adaptiveTTL
1002 */
1003 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1004 $mtime = $ago ? time() - $ago : $ago;
1005 $margin = 5;
1006 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1007
1008 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1009 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1010
1011 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1012
1013 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1014 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1015 }
1016
1017 public static function provideAdaptiveTTL() {
1018 return [
1019 [ 3600, 900, 30, .2, 720 ],
1020 [ 3600, 500, 30, .2, 500 ],
1021 [ 3600, 86400, 800, .2, 800 ],
1022 [ false, 86400, 800, .2, 800 ],
1023 [ null, 86400, 800, .2, 800 ]
1024 ];
1025 }
1026 }