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