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