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