3 use Wikimedia\ScopedCallback
;
4 use Wikimedia\TestingAccessWrapper
;
7 * @author Matthias Mullie <mmullie@wikimedia.org>
11 class BagOStuffTest
extends MediaWikiTestCase
{
15 const TEST_KEY
= 'test';
17 protected function setUp() {
20 // type defined through parameter
21 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
22 $name = $this->getCliArg( 'use-bagostuff' );
24 $this->cache
= ObjectCache
::newFromId( $name );
26 // no type defined - use simple hash
27 $this->cache
= new HashBagOStuff
;
30 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
31 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
35 * @covers MediumSpecificBagOStuff::makeGlobalKey
36 * @covers MediumSpecificBagOStuff::makeKeyInternal
38 public function testMakeKey() {
39 $cache = ObjectCache
::newFromId( 'hash' );
41 $localKey = $cache->makeKey( 'first', 'second', 'third' );
42 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
44 $this->assertStringMatchesFormat(
45 '%Sfirst%Ssecond%Sthird%S',
47 'Local key interpolates parameters'
50 $this->assertStringMatchesFormat(
51 'global%Sfirst%Ssecond%Sthird%S',
53 'Global key interpolates parameters and contains global prefix'
56 $this->assertNotEquals(
59 'Local key and global key with same parameters should not be equal'
62 $this->assertNotEquals(
63 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
69 * @covers MediumSpecificBagOStuff::merge
70 * @covers MediumSpecificBagOStuff::mergeViaCas
72 public function testMerge() {
73 $key = $this->cache
->makeKey( self
::TEST_KEY
);
76 $casRace = false; // emulate a race
77 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
81 $cache->set( $key, 'conflict', 5 );
84 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
87 // merge on non-existing value
88 $merged = $this->cache
->merge( $key, $callback, 5 );
89 $this->assertTrue( $merged );
90 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
92 // merge on existing value
93 $merged = $this->cache
->merge( $key, $callback, 5 );
94 $this->assertTrue( $merged );
95 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
100 $this->cache
->merge( $key, $callback, 5, 1 ),
101 'Non-blocking merge (CAS)'
104 if ( $this->cache
instanceof MultiWriteBagOStuff
) {
105 $wrapper = TestingAccessWrapper
::newFromObject( $this->cache
);
106 $this->assertEquals( count( $wrapper->caches
), $calls );
108 $this->assertEquals( 1, $calls );
113 * @covers MediumSpecificBagOStuff::changeTTL
115 public function testChangeTTLRenew() {
116 $now = microtime( true ); // need real time
117 $this->cache
->setMockTime( $now );
119 $key = $this->cache
->makeKey( self
::TEST_KEY
);
122 $this->cache
->add( $key, $value, 60 );
123 $this->assertEquals( $value, $this->cache
->get( $key ) );
124 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
125 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
126 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
127 $this->assertEquals( $this->cache
->get( $key ), $value );
129 $this->cache
->delete( $key );
130 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
134 * @covers MediumSpecificBagOStuff::changeTTL
136 public function testChangeTTLExpireRel() {
137 $now = microtime( true ); // need real time
138 $this->cache
->setMockTime( $now );
140 $key = $this->cache
->makeKey( self
::TEST_KEY
);
143 $this->cache
->add( $key, $value, 5 );
144 $this->assertTrue( $this->cache
->changeTTL( $key, -3600 ) );
145 $this->assertFalse( $this->cache
->get( $key ) );
149 * @covers MediumSpecificBagOStuff::changeTTL
151 public function testChangeTTLExpireAbs() {
152 $now = microtime( true ); // need real time
153 $this->cache
->setMockTime( $now );
155 $key = $this->cache
->makeKey( self
::TEST_KEY
);
158 $this->cache
->add( $key, $value, 5 );
159 $this->assertTrue( $this->cache
->changeTTL( $key, $now - 3600 ) );
160 $this->assertFalse( $this->cache
->get( $key ) );
164 * @covers MediumSpecificBagOStuff::changeTTLMulti
166 public function testChangeTTLMulti() {
168 $this->cache
->setMockTime( $now );
170 $key1 = $this->cache
->makeKey( 'test-key1' );
171 $key2 = $this->cache
->makeKey( 'test-key2' );
172 $key3 = $this->cache
->makeKey( 'test-key3' );
173 $key4 = $this->cache
->makeKey( 'test-key4' );
176 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
178 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
179 $this->assertFalse( $ok, "No keys found" );
180 $this->assertFalse( $this->cache
->get( $key1 ) );
181 $this->assertFalse( $this->cache
->get( $key2 ) );
182 $this->assertFalse( $this->cache
->get( $key3 ) );
184 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
185 $this->assertTrue( $ok, "setMulti() succeeded" );
188 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
189 "setMulti() succeeded via getMulti() check"
192 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
193 $this->assertTrue( $ok, "TTL bumped for all keys" );
194 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
195 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
196 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
198 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
199 $this->assertFalse( $ok, "One key missing" );
200 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
202 $now = microtime( true ); // real time
203 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
204 $this->assertTrue( $ok, "setMulti() succeeded" );
206 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], $now +
86400 );
207 $this->assertTrue( $ok, "Expiry set for all keys" );
208 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
210 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
211 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
212 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
215 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
219 * @covers MediumSpecificBagOStuff::add
221 public function testAdd() {
222 $key = $this->cache
->makeKey( self
::TEST_KEY
);
223 $this->assertFalse( $this->cache
->get( $key ) );
224 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
225 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
229 * @covers MediumSpecificBagOStuff::get
231 public function testGet() {
232 $value = [ 'this' => 'is', 'a' => 'test' ];
234 $key = $this->cache
->makeKey( self
::TEST_KEY
);
235 $this->cache
->add( $key, $value, 5 );
236 $this->assertEquals( $this->cache
->get( $key ), $value );
240 * @covers MediumSpecificBagOStuff::get
241 * @covers MediumSpecificBagOStuff::set
242 * @covers MediumSpecificBagOStuff::getWithSetCallback
244 public function testGetWithSetCallback() {
246 $cache = new HashBagOStuff( [] );
247 $cache->setMockTime( $now );
248 $key = $cache->makeKey( self
::TEST_KEY
);
250 $this->assertFalse( $cache->get( $key ), "No value" );
252 $value = $cache->getWithSetCallback(
258 return 'hello kitty';
262 $this->assertEquals( 'hello kitty', $value );
263 $this->assertEquals( $value, $cache->get( $key ), "Value set" );
267 $this->assertFalse( $cache->get( $key ), "Value expired" );
271 * @covers MediumSpecificBagOStuff::incr
273 public function testIncr() {
274 $key = $this->cache
->makeKey( self
::TEST_KEY
);
275 $this->cache
->add( $key, 0, 5 );
276 $this->cache
->incr( $key );
278 $actualValue = $this->cache
->get( $key );
279 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
283 * @covers MediumSpecificBagOStuff::incrWithInit
285 public function testIncrWithInit() {
286 $key = $this->cache
->makeKey( self
::TEST_KEY
);
287 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
288 $this->assertEquals( 3, $val, "Correct init value" );
290 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
291 $this->assertEquals( 4, $val, "Correct init value" );
295 * @covers MediumSpecificBagOStuff::getMulti
297 public function testGetMulti() {
298 $value1 = [ 'this' => 'is', 'a' => 'test' ];
299 $value2 = [ 'this' => 'is', 'another' => 'test' ];
300 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
301 $value4 = [ 'another test where chars in key will be encoded' ];
303 $key1 = $this->cache
->makeKey( 'test-1' );
304 $key2 = $this->cache
->makeKey( 'test-2' );
305 // internally, MemcachedBagOStuffs will encode to will-%25-encode
306 $key3 = $this->cache
->makeKey( 'will-%-encode' );
307 $key4 = $this->cache
->makeKey(
308 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
312 $this->cache
->delete( $key1 );
313 $this->cache
->delete( $key2 );
314 $this->cache
->delete( $key3 );
315 $this->cache
->delete( $key4 );
317 $this->cache
->add( $key1, $value1, 5 );
318 $this->cache
->add( $key2, $value2, 5 );
319 $this->cache
->add( $key3, $value3, 5 );
320 $this->cache
->add( $key4, $value4, 5 );
323 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
324 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
328 $this->cache
->delete( $key1 );
329 $this->cache
->delete( $key2 );
330 $this->cache
->delete( $key3 );
331 $this->cache
->delete( $key4 );
335 * @covers MediumSpecificBagOStuff::setMulti
336 * @covers MediumSpecificBagOStuff::deleteMulti
338 public function testSetDeleteMulti() {
340 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
341 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
342 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
343 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
344 $this->cache
->makeKey( 'test-5' ) => 4,
345 $this->cache
->makeKey( 'test-6' ) => 'ever'
348 $this->assertTrue( $this->cache
->setMulti( $map ) );
351 $this->cache
->getMulti( array_keys( $map ) )
354 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
358 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
362 $this->cache
->getMulti( array_keys( $map ) )
367 * @covers MediumSpecificBagOStuff::get
368 * @covers MediumSpecificBagOStuff::getMulti
369 * @covers MediumSpecificBagOStuff::merge
370 * @covers MediumSpecificBagOStuff::delete
372 public function testSetSegmentable() {
373 $key = $this->cache
->makeKey( self
::TEST_KEY
);
375 $small = wfRandomString( 32 );
376 // 64 * 8 * 32768 = 16777216 bytes
377 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
379 $callback = function ( $cache, $key, $oldValue ) {
380 return $oldValue . '!';
383 $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
384 foreach ( $cases as $case => $value ) {
385 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
386 $this->assertEquals( $value, $this->cache
->get( $key ), "get $case" );
387 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key], "get $case" );
390 $this->cache
->merge( $key, $callback, 5, 1, BagOStuff
::WRITE_ALLOW_SEGMENTS
),
395 $this->cache
->get( $key ),
400 $this->cache
->getMulti( [ $key ] )[$key],
404 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ), "delete $case" );
405 $this->assertFalse( $this->cache
->get( $key ), "deleted $case" );
406 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "deletd $case" );
408 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
409 $this->assertEquals( "@$value", $this->cache
->get( $key ), "get $case" );
411 $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
),
414 $this->assertFalse( $this->cache
->get( $key ), "pruned $case" );
415 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "pruned $case" );
418 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
420 $this->assertEquals( 666, $this->cache
->get( $key ) );
421 $this->assertEquals( 667, $this->cache
->incr( $key ) );
422 $this->assertEquals( 667, $this->cache
->get( $key ) );
424 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
425 $this->assertEquals( 664, $this->cache
->get( $key ) );
427 $this->assertTrue( $this->cache
->delete( $key ) );
428 $this->assertFalse( $this->cache
->get( $key ) );
432 * @covers MediumSpecificBagOStuff::getScopedLock
434 public function testGetScopedLock() {
435 $key = $this->cache
->makeKey( self
::TEST_KEY
);
436 $value1 = $this->cache
->getScopedLock( $key, 0 );
437 $value2 = $this->cache
->getScopedLock( $key, 0 );
439 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
440 $this->assertNull( $value2, 'Duplicate call returned no lock' );
444 $value3 = $this->cache
->getScopedLock( $key, 0 );
445 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
448 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
449 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
451 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
452 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
456 * @covers MediumSpecificBagOStuff::__construct
457 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
459 public function testReportDupes() {
460 $logger = $this->createMock( Psr\Log\NullLogger
::class );
461 $logger->expects( $this->once() )
462 ->method( 'warning' )
463 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
468 $cache = new HashBagOStuff( [
469 'reportDupes' => true,
470 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
473 $cache->get( 'foo' );
474 $cache->get( 'bar' );
475 $cache->get( 'foo' );
477 DeferredUpdates
::doUpdates();
481 * @covers MediumSpecificBagOStuff::lock()
482 * @covers MediumSpecificBagOStuff::unlock()
484 public function testLocking() {
486 $this->assertTrue( $this->cache
->lock( $key ) );
487 $this->assertFalse( $this->cache
->lock( $key ) );
488 $this->assertTrue( $this->cache
->unlock( $key ) );
491 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
492 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
493 $this->assertTrue( $this->cache
->unlock( $key2 ) );
494 $this->assertTrue( $this->cache
->unlock( $key2 ) );
497 public function tearDown() {
498 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
499 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );