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