Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
blobefc37d250838ac1ea33fe6b3b266eef0e370adfc
1 <?php
3 class WANObjectCacheTest extends MediaWikiTestCase {
4 /** @var WANObjectCache */
5 private $cache;
6 /**@var BagOStuff */
7 private $internalCache;
9 protected function setUp() {
10 parent::setUp();
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
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 ) );
24 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
25 $this->internalCache = $wanCache->cache;
28 /**
29 * @dataProvider provideSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
32 * @param mixed $value
33 * @param integer $ttl
35 public function testSetAndGet( $value, $ttl ) {
36 $key = wfRandomString();
37 $this->cache->set( $key, $value, $ttl );
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" );
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 ),
63 /**
64 * @covers WANObjectCache::get()
66 public function testGetNotExists() {
67 $key = wfRandomString();
68 $curTTL = null;
69 $value = $this->cache->get( $key, $curTTL );
71 $this->assertFalse( $value, "Non-existing key has false value" );
72 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
75 /**
76 * @covers WANObjectCache::set()
78 public function testSetOver() {
79 $key = wfRandomString();
80 for ( $i = 0; $i < 3; ++$i ) {
81 $value = wfRandomString();
82 $this->cache->set( $key, $value, 3 );
84 $this->assertEquals( $this->cache->get( $key ), $value );
88 /**
89 * @covers WANObjectCache::set()
91 public function testStaleSet() {
92 $key = wfRandomString();
93 $value = wfRandomString();
94 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
96 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
99 /**
100 * @covers WANObjectCache::getWithSetCallback()
101 * @covers WANObjectCache::doGetWithSetCallback()
103 public function testGetWithSetCallback() {
104 $cache = $this->cache;
106 $key = wfRandomString();
107 $value = wfRandomString();
108 $cKey1 = wfRandomString();
109 $cKey2 = wfRandomString();
111 $wasSet = 0;
112 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
113 ++$wasSet;
114 $ttl = 20; // override with another value
115 return $value;
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" );
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)' );
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" );
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' );
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' );
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" );
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" );
175 * @covers WANObjectCache::getWithSetCallback()
176 * @covers WANObjectCache::doGetWithSetCallback()
178 public function testLockTSE() {
179 $cache = $this->cache;
180 $key = wfRandomString();
181 $value = wfRandomString();
183 $calls = 0;
184 $func = function() use ( &$calls, $value ) {
185 ++$calls;
186 return $value;
189 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
190 $this->assertEquals( $value, $ret );
191 $this->assertEquals( 1, $calls, 'Value was populated' );
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' );
201 * @covers WANObjectCache::getWithSetCallback()
202 * @covers WANObjectCache::doGetWithSetCallback()
204 public function testLockTSESlow() {
205 $cache = $this->cache;
206 $key = wfRandomString();
207 $value = wfRandomString();
209 $calls = 0;
210 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
211 ++$calls;
212 $setOpts['since'] = microtime( true ) - 10;
213 return $value;
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' );
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' );
232 * @covers WANObjectCache::getMulti()
234 public function testGetMulti() {
235 $cache = $this->cache;
237 $value1 = array( 'this' => 'is', 'a' => 'test' );
238 $value2 = array( 'this' => 'is', 'another' => 'test' );
240 $key1 = wfRandomString();
241 $key2 = wfRandomString();
242 $key3 = wfRandomString();
244 $cache->set( $key1, $value1, 5 );
245 $cache->set( $key2, $value2, 10 );
247 $curTTLs = array();
248 $this->assertEquals(
249 array( $key1 => $value1, $key2 => $value2 ),
250 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
251 'Result array populated'
254 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
255 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
256 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
258 $cKey1 = wfRandomString();
259 $cKey2 = wfRandomString();
261 $priorTime = microtime( true );
262 usleep( 1 );
263 $curTTLs = array();
264 $this->assertEquals(
265 array( $key1 => $value1, $key2 => $value2 ),
266 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
267 "Result array populated even with new check keys"
269 $t1 = $cache->getCheckKeyTime( $cKey1 );
270 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
271 $t2 = $cache->getCheckKeyTime( $cKey2 );
272 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
273 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
274 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
275 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
277 usleep( 1 );
278 $curTTLs = array();
279 $this->assertEquals(
280 array( $key1 => $value1, $key2 => $value2 ),
281 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
282 "Result array still populated even with new check keys"
284 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
285 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
286 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
290 * @covers WANObjectCache::getMulti()
291 * @covers WANObjectCache::processCheckKeys()
293 public function testGetMultiCheckKeys() {
294 $cache = $this->cache;
296 $checkAll = wfRandomString();
297 $check1 = wfRandomString();
298 $check2 = wfRandomString();
299 $check3 = wfRandomString();
300 $value1 = wfRandomString();
301 $value2 = wfRandomString();
303 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
304 // several seconds during the test to assert the behaviour.
305 foreach ( array( $checkAll, $check1, $check2 ) as $checkKey ) {
306 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
308 usleep( 100 );
310 $cache->set( 'key1', $value1, 10 );
311 $cache->set( 'key2', $value2, 10 );
313 $curTTLs = array();
314 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
315 'key1' => $check1,
316 $checkAll,
317 'key2' => $check2,
318 'key3' => $check3,
319 ) );
320 $this->assertEquals(
321 array( 'key1' => $value1, 'key2' => $value2 ),
322 $result,
323 'Initial values'
325 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
326 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
327 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
328 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
330 $cache->touchCheckKey( $check1 );
332 $curTTLs = array();
333 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
334 'key1' => $check1,
335 $checkAll,
336 'key2' => $check2,
337 'key3' => $check3,
338 ) );
339 $this->assertEquals(
340 array( 'key1' => $value1, 'key2' => $value2 ),
341 $result,
342 'key1 expired by check1, but value still provided'
344 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
345 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
347 $cache->touchCheckKey( $checkAll );
349 $curTTLs = array();
350 $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
351 'key1' => $check1,
352 $checkAll,
353 'key2' => $check2,
354 'key3' => $check3,
355 ) );
356 $this->assertEquals(
357 array( 'key1' => $value1, 'key2' => $value2 ),
358 $result,
359 'All keys expired by checkAll, but value still provided'
361 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
362 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
366 * @covers WANObjectCache::get()
367 * @covers WANObjectCache::processCheckKeys()
369 public function testCheckKeyInitHoldoff() {
370 $cache = $this->cache;
372 for ( $i = 0; $i < 500; ++$i ) {
373 $key = wfRandomString();
374 $checkKey = wfRandomString();
375 // miss, set, hit
376 $cache->get( $key, $curTTL, array( $checkKey ) );
377 $cache->set( $key, 'val', 10 );
378 $curTTL = null;
379 $v = $cache->get( $key, $curTTL, array( $checkKey ) );
381 $this->assertEquals( 'val', $v );
382 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
385 for ( $i = 0; $i < 500; ++$i ) {
386 $key = wfRandomString();
387 $checkKey = wfRandomString();
388 // set, hit
389 $cache->set( $key, 'val', 10 );
390 $curTTL = null;
391 $v = $cache->get( $key, $curTTL, array( $checkKey ) );
393 $this->assertEquals( 'val', $v );
394 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
399 * @covers WANObjectCache::delete()
401 public function testDelete() {
402 $key = wfRandomString();
403 $value = wfRandomString();
404 $this->cache->set( $key, $value );
406 $curTTL = null;
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" );
411 $this->cache->delete( $key );
413 $curTTL = null;
414 $v = $this->cache->get( $key, $curTTL );
415 $this->assertFalse( $v, "Deleted key has false value" );
416 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
418 $this->cache->set( $key, $value . 'more' );
419 $v = $this->cache->get( $key, $curTTL );
420 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
421 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
423 $this->cache->set( $key, $value );
424 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
426 $curTTL = null;
427 $v = $this->cache->get( $key, $curTTL );
428 $this->assertFalse( $v, "Deleted key has false value" );
429 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
431 $this->cache->set( $key, $value );
432 $v = $this->cache->get( $key, $curTTL );
433 $this->assertEquals( $value, $v, "Key was created with value" );
434 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
438 * @covers WANObjectCache::touchCheckKey()
439 * @covers WANObjectCache::resetCheckKey()
440 * @covers WANObjectCache::getCheckKeyTime()
442 public function testTouchKeys() {
443 $key = wfRandomString();
445 $priorTime = microtime( true );
446 usleep( 100 );
447 $t0 = $this->cache->getCheckKeyTime( $key );
448 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
450 $priorTime = microtime( true );
451 usleep( 100 );
452 $this->cache->touchCheckKey( $key );
453 $t1 = $this->cache->getCheckKeyTime( $key );
454 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
456 $t2 = $this->cache->getCheckKeyTime( $key );
457 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
459 usleep( 100 );
460 $this->cache->touchCheckKey( $key );
461 $t3 = $this->cache->getCheckKeyTime( $key );
462 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
464 $t4 = $this->cache->getCheckKeyTime( $key );
465 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
467 usleep( 100 );
468 $this->cache->resetCheckKey( $key );
469 $t5 = $this->cache->getCheckKeyTime( $key );
470 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
472 $t6 = $this->cache->getCheckKeyTime( $key );
473 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
477 * @covers WANObjectCache::getMulti()
479 public function testGetWithSeveralCheckKeys() {
480 $key = wfRandomString();
481 $tKey1 = wfRandomString();
482 $tKey2 = wfRandomString();
483 $value = 'meow';
485 // Two check keys are newer (given hold-off) than $key, another is older
486 $this->internalCache->set(
487 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
488 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
490 $this->internalCache->set(
491 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
492 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
494 $this->internalCache->set(
495 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
496 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
498 $this->cache->set( $key, $value, 30 );
500 $curTTL = null;
501 $v = $this->cache->get( $key, $curTTL, array( $tKey1, $tKey2 ) );
502 $this->assertEquals( $value, $v, "Value matches" );
503 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
504 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
508 * @covers WANObjectCache::set()
510 public function testSetWithLag() {
511 $value = 1;
513 $key = wfRandomString();
514 $opts = array( 'lag' => 300, 'since' => microtime( true ) );
515 $this->cache->set( $key, $value, 30, $opts );
516 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
518 $key = wfRandomString();
519 $opts = array( 'lag' => 0, 'since' => microtime( true ) - 300 );
520 $this->cache->set( $key, $value, 30, $opts );
521 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
523 $key = wfRandomString();
524 $opts = array( 'lag' => 5, 'since' => microtime( true ) - 5 );
525 $this->cache->set( $key, $value, 30, $opts );
526 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
530 * @covers WANObjectCache::set()
532 public function testWritePending() {
533 $value = 1;
535 $key = wfRandomString();
536 $opts = array( 'pending' => true );
537 $this->cache->set( $key, $value, 30, $opts );
538 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );