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