Change wording of 'wlshowtime' for ease of localisation
[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( array(
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( array() )
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 $key = wfRandomString();
37 $this->cache->set( $key, $value, $ttl );
38
39 $curTTL = null;
40 $this->assertEquals( $value, $this->cache->get( $key, $curTTL ) );
41 if ( is_infinite( $ttl ) || $ttl == 0 ) {
42 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
43 } else {
44 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
45 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
46 }
47 }
48
49 public static function provideSetAndGet() {
50 return array(
51 array( 14141, 3 ),
52 array( 3535.666, 3 ),
53 array( array(), 3 ),
54 array( null, 3 ),
55 array( '0', 3 ),
56 array( (object)array( 'meow' ), 3 ),
57 array( INF, 3 ),
58 array( '', 3 ),
59 array( 'pizzacat', INF ),
60 );
61 }
62
63 /**
64 * @covers WANObjectCache::get()
65 */
66 public function testGetNotExists() {
67 $key = wfRandomString();
68 $curTTL = null;
69 $value = $this->cache->get( $key, $curTTL );
70
71 $this->assertFalse( $value, "Non-existing key has false value" );
72 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
73 }
74
75 /**
76 * @covers WANObjectCache::set()
77 */
78 public function testSetOver() {
79 $key = wfRandomString();
80 for ( $i = 0; $i < 3; ++$i ) {
81 $value = wfRandomString();
82 $this->cache->set( $key, $value, 3 );
83
84 $this->assertEquals( $this->cache->get( $key ), $value );
85 }
86 }
87
88 /**
89 * @covers WANObjectCache::set()
90 */
91 public function testStaleSet() {
92 $key = wfRandomString();
93 $value = wfRandomString();
94 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
95
96 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
97 }
98
99 /**
100 * @covers WANObjectCache::getWithSetCallback()
101 * @covers WANObjectCache::doGetWithSetCallback()
102 */
103 public function testGetWithSetCallback() {
104 $cache = $this->cache;
105
106 $key = wfRandomString();
107 $value = wfRandomString();
108 $cKey1 = wfRandomString();
109 $cKey2 = wfRandomString();
110
111 $wasSet = 0;
112 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
113 ++$wasSet;
114 $ttl = 20; // override with another value
115 return $value;
116 };
117
118 $wasSet = 0;
119 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
120 $this->assertEquals( $value, $v, "Value returned" );
121 $this->assertEquals( 1, $wasSet, "Value regenerated" );
122
123 $curTTL = null;
124 $cache->get( $key, $curTTL );
125 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
126 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
127
128 $wasSet = 0;
129 $v = $cache->getWithSetCallback( $key, 30, $func, array(
130 'lowTTL' => 0,
131 'lockTSE' => 5,
132 ) );
133 $this->assertEquals( $value, $v, "Value returned" );
134 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
135
136 $priorTime = microtime( true );
137 usleep( 1 );
138 $wasSet = 0;
139 $v = $cache->getWithSetCallback( $key, 30, $func,
140 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
141 $this->assertEquals( $value, $v, "Value returned" );
142 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
143 $t1 = $cache->getCheckKeyTime( $cKey1 );
144 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
145 $t2 = $cache->getCheckKeyTime( $cKey2 );
146 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
147
148 $priorTime = microtime( true );
149 $wasSet = 0;
150 $v = $cache->getWithSetCallback( $key, 30, $func,
151 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
152 $this->assertEquals( $value, $v, "Value returned" );
153 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
154 $t1 = $cache->getCheckKeyTime( $cKey1 );
155 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
156 $t2 = $cache->getCheckKeyTime( $cKey2 );
157 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
158
159 $curTTL = null;
160 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
161 $this->assertEquals( $value, $v, "Value returned" );
162 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
163
164 $wasSet = 0;
165 $key = wfRandomString();
166 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
167 $this->assertEquals( $value, $v, "Value returned" );
168 $cache->delete( $key );
169 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
170 $this->assertEquals( $value, $v, "Value still returned after deleted" );
171 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
172 }
173
174 /**
175 * @covers WANObjectCache::getWithSetCallback()
176 * @covers WANObjectCache::doGetWithSetCallback()
177 */
178 public function testLockTSE() {
179 $cache = $this->cache;
180 $key = wfRandomString();
181 $value = wfRandomString();
182
183 $calls = 0;
184 $func = function() use ( &$calls, $value ) {
185 ++$calls;
186 return $value;
187 };
188
189 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
190 $this->assertEquals( $value, $ret );
191 $this->assertEquals( 1, $calls, 'Value was populated' );
192
193 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
194 $this->internalCache->lock( $key, 0 );
195 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
196 $this->assertEquals( $value, $ret );
197 $this->assertEquals( 1, $calls, 'Callback was not used' );
198 }
199
200 /**
201 * @covers WANObjectCache::getWithSetCallback()
202 * @covers WANObjectCache::doGetWithSetCallback()
203 */
204 public function testLockTSESlow() {
205 $cache = $this->cache;
206 $key = wfRandomString();
207 $value = wfRandomString();
208
209 $calls = 0;
210 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
211 ++$calls;
212 $setOpts['since'] = microtime( true ) - 10;
213 return $value;
214 };
215
216 // Value should be marked as stale due to snapshot lag
217 $curTTL = null;
218 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
219 $this->assertEquals( $value, $ret );
220 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
221 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
222 $this->assertEquals( 1, $calls, 'Value was generated' );
223
224 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
225 $this->internalCache->lock( $key, 0 );
226 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
227 $this->assertEquals( $value, $ret );
228 $this->assertEquals( 1, $calls, 'Callback was not used' );
229 }
230
231 /**
232 * @covers WANObjectCache::getMulti()
233 */
234 public function testGetMulti() {
235 $cache = $this->cache;
236
237 $value1 = array( 'this' => 'is', 'a' => 'test' );
238 $value2 = array( 'this' => 'is', 'another' => 'test' );
239
240 $key1 = wfRandomString();
241 $key2 = wfRandomString();
242 $key3 = wfRandomString();
243
244 $cache->set( $key1, $value1, 5 );
245 $cache->set( $key2, $value2, 10 );
246
247 $curTTLs = array();
248 $this->assertEquals(
249 array( $key1 => $value1, $key2 => $value2 ),
250 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
251 );
252
253 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
254 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
255 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
256
257 $cKey1 = wfRandomString();
258 $cKey2 = wfRandomString();
259 $curTTLs = array();
260 $this->assertEquals(
261 array( $key1 => $value1, $key2 => $value2 ),
262 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
263 'Result array populated'
264 );
265
266 $priorTime = microtime( true );
267 usleep( 1 );
268 $curTTLs = array();
269 $this->assertEquals(
270 array( $key1 => $value1, $key2 => $value2 ),
271 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
272 "Result array populated even with new check keys"
273 );
274 $t1 = $cache->getCheckKeyTime( $cKey1 );
275 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
276 $t2 = $cache->getCheckKeyTime( $cKey2 );
277 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
278 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
279 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
280 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
281
282 usleep( 1 );
283 $curTTLs = array();
284 $this->assertEquals(
285 array( $key1 => $value1, $key2 => $value2 ),
286 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
287 "Result array still populated even with new check keys"
288 );
289 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
290 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
291 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
292 }
293
294 /**
295 * @covers WANObjectCache::getMulti()
296 * @covers WANObjectCache::processCheckKeys()
297 */
298 public function testGetMultiCheckKeys() {
299 $cache = $this->cache;
300
301 $checkAll = wfRandomString();
302 $check1 = wfRandomString();
303 $check2 = wfRandomString();
304 $check3 = wfRandomString();
305 $value1 = wfRandomString();
306 $value2 = wfRandomString();
307
308 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
309 // several seconds during the test to assert the behaviour.
310 foreach ( array( $checkAll, $check1, $check2 ) as $checkKey ) {
311 $this->internalCache->set( $cache::TIME_KEY_PREFIX . $checkKey,
312 $cache::PURGE_VAL_PREFIX . microtime( true ) - $cache::HOLDOFF_TTL, $cache::CHECK_KEY_TTL );
313 }
314
315 $cache->set( 'key1', $value1, 10 );
316 $cache->set( 'key2', $value2, 10 );
317
318 $curTTLs = array();
319 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
320 'key1' => $check1,
321 $checkAll,
322 'key2' => $check2,
323 'key3' => $check3,
324 ) );
325 $this->assertEquals(
326 array( 'key1' => $value1, 'key2' => $value2 ),
327 $result,
328 'Initial values'
329 );
330 $this->assertEquals(
331 array( 'key1' => 0, 'key2' => 0 ),
332 $curTTLs,
333 'Initial ttls'
334 );
335
336 $cache->touchCheckKey( $check1 );
337 usleep( 100 );
338
339 $curTTLs = array();
340 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
341 'key1' => $check1,
342 $checkAll,
343 'key2' => $check2,
344 'key3' => $check3,
345 ) );
346 $this->assertEquals(
347 array( 'key1' => $value1, 'key2' => $value2 ),
348 $result,
349 'key1 expired by check1, but value still provided'
350 );
351 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
352 $this->assertEquals( 0, $curTTLs['key2'], 'key2 still valid' );
353
354 $cache->touchCheckKey( $checkAll );
355 usleep( 100 );
356
357 $curTTLs = array();
358 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
359 'key1' => $check1,
360 $checkAll,
361 'key2' => $check2,
362 'key3' => $check3,
363 ) );
364 $this->assertEquals(
365 array( 'key1' => $value1, 'key2' => $value2 ),
366 $result,
367 'All keys expired by checkAll, but value still provided'
368 );
369 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
370 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
371 }
372
373 /**
374 * @covers WANObjectCache::delete()
375 */
376 public function testDelete() {
377 $key = wfRandomString();
378 $value = wfRandomString();
379 $this->cache->set( $key, $value );
380
381 $curTTL = null;
382 $v = $this->cache->get( $key, $curTTL );
383 $this->assertEquals( $value, $v, "Key was created with value" );
384 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
385
386 $this->cache->delete( $key );
387
388 $curTTL = null;
389 $v = $this->cache->get( $key, $curTTL );
390 $this->assertFalse( $v, "Deleted key has false value" );
391 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
392
393 $this->cache->set( $key, $value . 'more' );
394 $v = $this->cache->get( $key, $curTTL );
395 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
396 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
397
398 $this->cache->set( $key, $value );
399 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
400
401 $curTTL = null;
402 $v = $this->cache->get( $key, $curTTL );
403 $this->assertFalse( $v, "Deleted key has false value" );
404 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
405
406 $this->cache->set( $key, $value );
407 $v = $this->cache->get( $key, $curTTL );
408 $this->assertEquals( $value, $v, "Key was created with value" );
409 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
410 }
411
412 /**
413 * @covers WANObjectCache::touchCheckKey()
414 * @covers WANObjectCache::resetCheckKey()
415 * @covers WANObjectCache::getCheckKeyTime()
416 */
417 public function testTouchKeys() {
418 $key = wfRandomString();
419
420 $priorTime = microtime( true );
421 usleep( 100 );
422 $t0 = $this->cache->getCheckKeyTime( $key );
423 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
424
425 $priorTime = microtime( true );
426 usleep( 100 );
427 $this->cache->touchCheckKey( $key );
428 $t1 = $this->cache->getCheckKeyTime( $key );
429 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
430
431 $t2 = $this->cache->getCheckKeyTime( $key );
432 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
433
434 usleep( 100 );
435 $this->cache->touchCheckKey( $key );
436 $t3 = $this->cache->getCheckKeyTime( $key );
437 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
438
439 $t4 = $this->cache->getCheckKeyTime( $key );
440 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
441
442 usleep( 100 );
443 $this->cache->resetCheckKey( $key );
444 $t5 = $this->cache->getCheckKeyTime( $key );
445 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
446
447 $t6 = $this->cache->getCheckKeyTime( $key );
448 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
449 }
450
451 /**
452 * @covers WANObjectCache::getMulti()
453 */
454 public function testGetWithSeveralCheckKeys() {
455 $key = wfRandomString();
456 $tKey1 = wfRandomString();
457 $tKey2 = wfRandomString();
458 $value = 'meow';
459
460 // Two check keys are newer (given hold-off) than $key, another is older
461 $this->internalCache->set(
462 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
463 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
464 );
465 $this->internalCache->set(
466 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
467 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
468 );
469 $this->internalCache->set(
470 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
471 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
472 );
473 $this->cache->set( $key, $value, 30 );
474
475 $curTTL = null;
476 $v = $this->cache->get( $key, $curTTL, array( $tKey1, $tKey2 ) );
477 $this->assertEquals( $value, $v, "Value matches" );
478 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
479 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
480 }
481
482 /**
483 * @covers WANObjectCache::set()
484 */
485 public function testSetWithLag() {
486 $value = 1;
487
488 $key = wfRandomString();
489 $opts = array( 'lag' => 300, 'since' => microtime( true ) );
490 $this->cache->set( $key, $value, 30, $opts );
491 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
492
493 $key = wfRandomString();
494 $opts = array( 'lag' => 0, 'since' => microtime( true ) - 300 );
495 $this->cache->set( $key, $value, 30, $opts );
496 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
497
498 $key = wfRandomString();
499 $opts = array( 'lag' => 5, 'since' => microtime( true ) - 5 );
500 $this->cache->set( $key, $value, 30, $opts );
501 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
502 }
503
504 /**
505 * @covers WANObjectCache::set()
506 */
507 public function testWritePending() {
508 $value = 1;
509
510 $key = wfRandomString();
511 $opts = array( 'pending' => true );
512 $this->cache->set( $key, $value, 30, $opts );
513 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
514 }
515 }