Merge "Use classes to apply the 'editfont' preference"
[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 $priorValue = null;
165 $priorAsOf = null;
166 $wasSet = 0;
167 $func = function( $old, &$ttl, &$opts, $asOf )
168 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
169 {
170 ++$wasSet;
171 $priorValue = $old;
172 $priorAsOf = $asOf;
173 $ttl = 20; // override with another value
174 return $value;
175 };
176
177 $wasSet = 0;
178 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
179 $this->assertEquals( $value, $v, "Value returned" );
180 $this->assertEquals( 1, $wasSet, "Value regenerated" );
181 $this->assertFalse( $priorValue, "No prior value" );
182 $this->assertNull( $priorAsOf, "No prior value" );
183
184 $curTTL = null;
185 $cache->get( $key, $curTTL );
186 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
187 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
188
189 $wasSet = 0;
190 $v = $cache->getWithSetCallback( $key, 30, $func, [
191 'lowTTL' => 0,
192 'lockTSE' => 5,
193 ] + $extOpts );
194 $this->assertEquals( $value, $v, "Value returned" );
195 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
196
197 $priorTime = microtime( true );
198 usleep( 1 );
199 $wasSet = 0;
200 $v = $cache->getWithSetCallback(
201 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
202 );
203 $this->assertEquals( $value, $v, "Value returned" );
204 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
205 $this->assertEquals( $value, $priorValue, "Has prior value" );
206 $this->assertType( 'float', $priorAsOf, "Has prior value" );
207 $t1 = $cache->getCheckKeyTime( $cKey1 );
208 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
209 $t2 = $cache->getCheckKeyTime( $cKey2 );
210 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
211
212 $priorTime = microtime( true );
213 $wasSet = 0;
214 $v = $cache->getWithSetCallback(
215 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
216 );
217 $this->assertEquals( $value, $v, "Value returned" );
218 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
219 $t1 = $cache->getCheckKeyTime( $cKey1 );
220 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
221 $t2 = $cache->getCheckKeyTime( $cKey2 );
222 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
223
224 $curTTL = null;
225 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
226 if ( $versioned ) {
227 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
228 } else {
229 $this->assertEquals( $value, $v, "Value returned" );
230 }
231 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
232
233 $wasSet = 0;
234 $key = wfRandomString();
235 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
236 $this->assertEquals( $value, $v, "Value returned" );
237 $cache->delete( $key );
238 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
239 $this->assertEquals( $value, $v, "Value still returned after deleted" );
240 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
241 }
242
243 public static function getWithSetCallback_provider() {
244 return [
245 [ [], false ],
246 [ [ 'version' => 1 ], true ]
247 ];
248 }
249
250 /**
251 * @covers WANObjectCache::getWithSetCallback()
252 * @covers WANObjectCache::doGetWithSetCallback()
253 */
254 public function testLockTSE() {
255 $cache = $this->cache;
256 $key = wfRandomString();
257 $value = wfRandomString();
258
259 $calls = 0;
260 $func = function() use ( &$calls, $value, $cache, $key ) {
261 ++$calls;
262 // Immediately kill any mutex rather than waiting a second
263 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
264 return $value;
265 };
266
267 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
268 $this->assertEquals( $value, $ret );
269 $this->assertEquals( 1, $calls, 'Value was populated' );
270
271 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
272 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
273
274 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
275 $ret = $cache->getWithSetCallback( $key, 30, $func,
276 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
277 $this->assertEquals( $value, $ret, 'Old value used' );
278 $this->assertEquals( 1, $calls, 'Callback was not used' );
279
280 $cache->delete( $key );
281 $ret = $cache->getWithSetCallback( $key, 30, $func,
282 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
283 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
284 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
285
286 $ret = $cache->getWithSetCallback( $key, 30, $func,
287 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
288 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
289 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
290 }
291
292 /**
293 * @covers WANObjectCache::getWithSetCallback()
294 * @covers WANObjectCache::doGetWithSetCallback()
295 */
296 public function testLockTSESlow() {
297 $cache = $this->cache;
298 $key = wfRandomString();
299 $value = wfRandomString();
300
301 $calls = 0;
302 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
303 ++$calls;
304 $setOpts['since'] = microtime( true ) - 10;
305 // Immediately kill any mutex rather than waiting a second
306 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
307 return $value;
308 };
309
310 // Value should be marked as stale due to snapshot lag
311 $curTTL = null;
312 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
313 $this->assertEquals( $value, $ret );
314 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
315 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
316 $this->assertEquals( 1, $calls, 'Value was generated' );
317
318 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
319 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
320 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
321 $this->assertEquals( $value, $ret );
322 $this->assertEquals( 1, $calls, 'Callback was not used' );
323 }
324
325 /**
326 * @covers WANObjectCache::getWithSetCallback()
327 * @covers WANObjectCache::doGetWithSetCallback()
328 */
329 public function testBusyValue() {
330 $cache = $this->cache;
331 $key = wfRandomString();
332 $value = wfRandomString();
333 $busyValue = wfRandomString();
334
335 $calls = 0;
336 $func = function() use ( &$calls, $value, $cache, $key ) {
337 ++$calls;
338 // Immediately kill any mutex rather than waiting a second
339 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
340 return $value;
341 };
342
343 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
344 $this->assertEquals( $value, $ret );
345 $this->assertEquals( 1, $calls, 'Value was populated' );
346
347 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
348 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
349
350 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
351 $ret = $cache->getWithSetCallback( $key, 30, $func,
352 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
353 $this->assertEquals( $value, $ret, 'Callback used' );
354 $this->assertEquals( 2, $calls, 'Callback used' );
355
356 $ret = $cache->getWithSetCallback( $key, 30, $func,
357 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
358 $this->assertEquals( $value, $ret, 'Old value used' );
359 $this->assertEquals( 2, $calls, 'Callback was not used' );
360
361 $cache->delete( $key ); // no value at all anymore and still locked
362 $ret = $cache->getWithSetCallback( $key, 30, $func,
363 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
364 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
365 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
366
367 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
368 $ret = $cache->getWithSetCallback( $key, 30, $func,
369 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
370 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
371 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
372
373 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
374 $ret = $cache->getWithSetCallback( $key, 30, $func,
375 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
376 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
377 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
378 }
379
380 /**
381 * @covers WANObjectCache::getMulti()
382 */
383 public function testGetMulti() {
384 $cache = $this->cache;
385
386 $value1 = [ 'this' => 'is', 'a' => 'test' ];
387 $value2 = [ 'this' => 'is', 'another' => 'test' ];
388
389 $key1 = wfRandomString();
390 $key2 = wfRandomString();
391 $key3 = wfRandomString();
392
393 $cache->set( $key1, $value1, 5 );
394 $cache->set( $key2, $value2, 10 );
395
396 $curTTLs = [];
397 $this->assertEquals(
398 [ $key1 => $value1, $key2 => $value2 ],
399 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
400 'Result array populated'
401 );
402
403 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
404 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
405 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
406
407 $cKey1 = wfRandomString();
408 $cKey2 = wfRandomString();
409
410 $priorTime = microtime( true );
411 usleep( 1 );
412 $curTTLs = [];
413 $this->assertEquals(
414 [ $key1 => $value1, $key2 => $value2 ],
415 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
416 "Result array populated even with new check keys"
417 );
418 $t1 = $cache->getCheckKeyTime( $cKey1 );
419 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
420 $t2 = $cache->getCheckKeyTime( $cKey2 );
421 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
422 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
423 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
424 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
425
426 usleep( 1 );
427 $curTTLs = [];
428 $this->assertEquals(
429 [ $key1 => $value1, $key2 => $value2 ],
430 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
431 "Result array still populated even with new check keys"
432 );
433 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
434 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
435 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
436 }
437
438 /**
439 * @covers WANObjectCache::getMulti()
440 * @covers WANObjectCache::processCheckKeys()
441 */
442 public function testGetMultiCheckKeys() {
443 $cache = $this->cache;
444
445 $checkAll = wfRandomString();
446 $check1 = wfRandomString();
447 $check2 = wfRandomString();
448 $check3 = wfRandomString();
449 $value1 = wfRandomString();
450 $value2 = wfRandomString();
451
452 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
453 // several seconds during the test to assert the behaviour.
454 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
455 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
456 }
457 usleep( 100 );
458
459 $cache->set( 'key1', $value1, 10 );
460 $cache->set( 'key2', $value2, 10 );
461
462 $curTTLs = [];
463 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
464 'key1' => $check1,
465 $checkAll,
466 'key2' => $check2,
467 'key3' => $check3,
468 ] );
469 $this->assertEquals(
470 [ 'key1' => $value1, 'key2' => $value2 ],
471 $result,
472 'Initial values'
473 );
474 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
475 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
476 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
477 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
478
479 $cache->touchCheckKey( $check1 );
480
481 $curTTLs = [];
482 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
483 'key1' => $check1,
484 $checkAll,
485 'key2' => $check2,
486 'key3' => $check3,
487 ] );
488 $this->assertEquals(
489 [ 'key1' => $value1, 'key2' => $value2 ],
490 $result,
491 'key1 expired by check1, but value still provided'
492 );
493 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
494 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
495
496 $cache->touchCheckKey( $checkAll );
497
498 $curTTLs = [];
499 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
500 'key1' => $check1,
501 $checkAll,
502 'key2' => $check2,
503 'key3' => $check3,
504 ] );
505 $this->assertEquals(
506 [ 'key1' => $value1, 'key2' => $value2 ],
507 $result,
508 'All keys expired by checkAll, but value still provided'
509 );
510 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
511 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
512 }
513
514 /**
515 * @covers WANObjectCache::get()
516 * @covers WANObjectCache::processCheckKeys()
517 */
518 public function testCheckKeyInitHoldoff() {
519 $cache = $this->cache;
520
521 for ( $i = 0; $i < 500; ++$i ) {
522 $key = wfRandomString();
523 $checkKey = wfRandomString();
524 // miss, set, hit
525 $cache->get( $key, $curTTL, [ $checkKey ] );
526 $cache->set( $key, 'val', 10 );
527 $curTTL = null;
528 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
529
530 $this->assertEquals( 'val', $v );
531 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
532 }
533
534 for ( $i = 0; $i < 500; ++$i ) {
535 $key = wfRandomString();
536 $checkKey = wfRandomString();
537 // set, hit
538 $cache->set( $key, 'val', 10 );
539 $curTTL = null;
540 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
541
542 $this->assertEquals( 'val', $v );
543 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
544 }
545 }
546
547 /**
548 * @covers WANObjectCache::delete()
549 */
550 public function testDelete() {
551 $key = wfRandomString();
552 $value = wfRandomString();
553 $this->cache->set( $key, $value );
554
555 $curTTL = null;
556 $v = $this->cache->get( $key, $curTTL );
557 $this->assertEquals( $value, $v, "Key was created with value" );
558 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
559
560 $this->cache->delete( $key );
561
562 $curTTL = null;
563 $v = $this->cache->get( $key, $curTTL );
564 $this->assertFalse( $v, "Deleted key has false value" );
565 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
566
567 $this->cache->set( $key, $value . 'more' );
568 $v = $this->cache->get( $key, $curTTL );
569 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
570 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
571
572 $this->cache->set( $key, $value );
573 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
574
575 $curTTL = null;
576 $v = $this->cache->get( $key, $curTTL );
577 $this->assertFalse( $v, "Deleted key has false value" );
578 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
579
580 $this->cache->set( $key, $value );
581 $v = $this->cache->get( $key, $curTTL );
582 $this->assertEquals( $value, $v, "Key was created with value" );
583 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
584 }
585
586 /**
587 * @dataProvider getWithSetCallback_versions_provider
588 * @param array $extOpts
589 * @param $versioned
590 */
591 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
592 $cache = $this->cache;
593
594 $key = wfRandomString();
595 $value = wfRandomString();
596
597 $wasSet = 0;
598 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
599 ++$wasSet;
600 return $value;
601 };
602
603 // Set the main key (version N if versioned)
604 $wasSet = 0;
605 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
606 $this->assertEquals( $value, $v, "Value returned" );
607 $this->assertEquals( 1, $wasSet, "Value regenerated" );
608 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
609 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
610 // Set the key for version N+1 (if versioned)
611 if ( $versioned ) {
612 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
613
614 $wasSet = 0;
615 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
616 $this->assertEquals( $value, $v, "Value returned" );
617 $this->assertEquals( 1, $wasSet, "Value regenerated" );
618
619 $wasSet = 0;
620 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
621 $this->assertEquals( $value, $v, "Value returned" );
622 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
623 }
624
625 $wasSet = 0;
626 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
627 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
628
629 $wasSet = 0;
630 $cache->delete( $key );
631 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
632 $this->assertEquals( $value, $v, "Value returned" );
633 $this->assertEquals( 1, $wasSet, "Value regenerated" );
634
635 if ( $versioned ) {
636 $wasSet = 0;
637 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
638 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
639 $this->assertEquals( $value, $v, "Value returned" );
640 $this->assertEquals( 1, $wasSet, "Value regenerated" );
641 }
642 }
643
644 public static function getWithSetCallback_versions_provider() {
645 return [
646 [ [], false ],
647 [ [ 'version' => 1 ], true ]
648 ];
649 }
650
651 /**
652 * @covers WANObjectCache::touchCheckKey()
653 * @covers WANObjectCache::resetCheckKey()
654 * @covers WANObjectCache::getCheckKeyTime()
655 */
656 public function testTouchKeys() {
657 $key = wfRandomString();
658
659 $priorTime = microtime( true );
660 usleep( 100 );
661 $t0 = $this->cache->getCheckKeyTime( $key );
662 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
663
664 $priorTime = microtime( true );
665 usleep( 100 );
666 $this->cache->touchCheckKey( $key );
667 $t1 = $this->cache->getCheckKeyTime( $key );
668 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
669
670 $t2 = $this->cache->getCheckKeyTime( $key );
671 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
672
673 usleep( 100 );
674 $this->cache->touchCheckKey( $key );
675 $t3 = $this->cache->getCheckKeyTime( $key );
676 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
677
678 $t4 = $this->cache->getCheckKeyTime( $key );
679 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
680
681 usleep( 100 );
682 $this->cache->resetCheckKey( $key );
683 $t5 = $this->cache->getCheckKeyTime( $key );
684 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
685
686 $t6 = $this->cache->getCheckKeyTime( $key );
687 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
688 }
689
690 /**
691 * @covers WANObjectCache::getMulti()
692 */
693 public function testGetWithSeveralCheckKeys() {
694 $key = wfRandomString();
695 $tKey1 = wfRandomString();
696 $tKey2 = wfRandomString();
697 $value = 'meow';
698
699 // Two check keys are newer (given hold-off) than $key, another is older
700 $this->internalCache->set(
701 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
702 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
703 );
704 $this->internalCache->set(
705 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
706 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
707 );
708 $this->internalCache->set(
709 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
710 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
711 );
712 $this->cache->set( $key, $value, 30 );
713
714 $curTTL = null;
715 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
716 $this->assertEquals( $value, $v, "Value matches" );
717 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
718 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
719 }
720
721 /**
722 * @covers WANObjectCache::set()
723 */
724 public function testSetWithLag() {
725 $value = 1;
726
727 $key = wfRandomString();
728 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
729 $this->cache->set( $key, $value, 30, $opts );
730 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
731
732 $key = wfRandomString();
733 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
734 $this->cache->set( $key, $value, 30, $opts );
735 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
736
737 $key = wfRandomString();
738 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
739 $this->cache->set( $key, $value, 30, $opts );
740 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
741 }
742
743 /**
744 * @covers WANObjectCache::set()
745 */
746 public function testWritePending() {
747 $value = 1;
748
749 $key = wfRandomString();
750 $opts = [ 'pending' => true ];
751 $this->cache->set( $key, $value, 30, $opts );
752 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
753 }
754
755 public function testMcRouterSupport() {
756 $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
757 $localBag->expects( $this->never() )->method( 'set' );
758 $localBag->expects( $this->never() )->method( 'delete' );
759 $wanCache = new WANObjectCache( [
760 'cache' => $localBag,
761 'pool' => 'testcache-hash',
762 'relayer' => new EventRelayerNull( [] )
763 ] );
764 $valFunc = function () {
765 return 1;
766 };
767
768 // None of these should use broadcasting commands (e.g. SET, DELETE)
769 $wanCache->get( 'x' );
770 $wanCache->get( 'x', $ctl, [ 'check1' ] );
771 $wanCache->getMulti( [ 'x', 'y' ] );
772 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
773 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
774 $wanCache->getCheckKeyTime( 'zzz' );
775 }
776
777 /**
778 * @dataProvider provideAdaptiveTTL
779 * @covers WANObjectCache::adaptiveTTL()
780 */
781 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
782 $mtime = is_int( $ago ) ? time() - $ago : $ago;
783 $margin = 5;
784 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
785
786 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
787 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
788
789 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
790
791 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
792 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
793 }
794
795 public static function provideAdaptiveTTL() {
796 return [
797 [ 3600, 900, 30, .2, 720 ],
798 [ 3600, 500, 30, .2, 500 ],
799 [ 3600, 86400, 800, .2, 800 ],
800 [ false, 86400, 800, .2, 800 ],
801 [ null, 86400, 800, .2, 800 ]
802 ];
803 }
804 }