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