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