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 global $wgObjectCaches;
24 $id = $this->getCliArg( 'use-bagostuff' );
25 $this->cache
= ObjectCache
::newFromParams( $wgObjectCaches[$id] );
27 // no type defined - use simple hash
28 $this->cache
= new HashBagOStuff
;
31 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
32 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
36 * @covers MediumSpecificBagOStuff::makeGlobalKey
37 * @covers MediumSpecificBagOStuff::makeKeyInternal
39 public function testMakeKey() {
40 $cache = new HashBagOStuff();
42 $localKey = $cache->makeKey( 'first', 'second', 'third' );
43 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
45 $this->assertStringMatchesFormat(
46 '%Sfirst%Ssecond%Sthird%S',
48 'Local key interpolates parameters'
51 $this->assertStringMatchesFormat(
52 'global%Sfirst%Ssecond%Sthird%S',
54 'Global key interpolates parameters and contains global prefix'
57 $this->assertNotEquals(
60 'Local key and global key with same parameters should not be equal'
63 $this->assertNotEquals(
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
65 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
70 * @covers MediumSpecificBagOStuff::merge
71 * @covers MediumSpecificBagOStuff::mergeViaCas
73 public function testMerge() {
74 $key = $this->cache
->makeKey( self
::TEST_KEY
);
77 $casRace = false; // emulate a race
78 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
82 $cache->set( $key, 'conflict', 5 );
85 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
88 // merge on non-existing value
89 $merged = $this->cache
->merge( $key, $callback, 5 );
90 $this->assertTrue( $merged );
91 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
93 // merge on existing value
94 $merged = $this->cache
->merge( $key, $callback, 5 );
95 $this->assertTrue( $merged );
96 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
101 $this->cache
->merge( $key, $callback, 5, 1 ),
102 'Non-blocking merge (CAS)'
105 if ( $this->cache
instanceof MultiWriteBagOStuff
) {
106 $wrapper = TestingAccessWrapper
::newFromObject( $this->cache
);
107 $this->assertEquals( count( $wrapper->caches
), $calls );
109 $this->assertEquals( 1, $calls );
114 * @covers MediumSpecificBagOStuff::changeTTL
116 public function testChangeTTLRenew() {
117 $now = microtime( true ); // need real time
118 $this->cache
->setMockTime( $now );
120 $key = $this->cache
->makeKey( self
::TEST_KEY
);
123 $this->cache
->add( $key, $value, 60 );
124 $this->assertEquals( $value, $this->cache
->get( $key ) );
125 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
126 $this->assertTrue( $this->cache
->changeTTL( $key, 120 ) );
127 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
128 $this->assertEquals( $this->cache
->get( $key ), $value );
130 $this->cache
->delete( $key );
131 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
135 * @covers MediumSpecificBagOStuff::changeTTL
137 public function testChangeTTLExpireRel() {
138 $now = microtime( true ); // need real time
139 $this->cache
->setMockTime( $now );
141 $key = $this->cache
->makeKey( self
::TEST_KEY
);
144 $this->cache
->add( $key, $value, 5 );
145 $this->assertTrue( $this->cache
->changeTTL( $key, -3600 ) );
146 $this->assertFalse( $this->cache
->get( $key ) );
150 * @covers MediumSpecificBagOStuff::changeTTL
152 public function testChangeTTLExpireAbs() {
153 $now = microtime( true ); // need real time
154 $this->cache
->setMockTime( $now );
156 $key = $this->cache
->makeKey( self
::TEST_KEY
);
159 $this->cache
->add( $key, $value, 5 );
160 $this->assertTrue( $this->cache
->changeTTL( $key, $now - 3600 ) );
161 $this->assertFalse( $this->cache
->get( $key ) );
165 * @covers MediumSpecificBagOStuff::changeTTLMulti
167 public function testChangeTTLMulti() {
169 $this->cache
->setMockTime( $now );
171 $key1 = $this->cache
->makeKey( 'test-key1' );
172 $key2 = $this->cache
->makeKey( 'test-key2' );
173 $key3 = $this->cache
->makeKey( 'test-key3' );
174 $key4 = $this->cache
->makeKey( 'test-key4' );
177 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
179 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
180 $this->assertFalse( $ok, "No keys found" );
181 $this->assertFalse( $this->cache
->get( $key1 ) );
182 $this->assertFalse( $this->cache
->get( $key2 ) );
183 $this->assertFalse( $this->cache
->get( $key3 ) );
185 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
186 $this->assertTrue( $ok, "setMulti() succeeded" );
189 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
190 "setMulti() succeeded via getMulti() check"
193 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
194 $this->assertTrue( $ok, "TTL bumped for all keys" );
195 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
196 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
197 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
199 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
200 $this->assertFalse( $ok, "One key missing" );
201 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
203 $now = microtime( true ); // real time
204 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
205 $this->assertTrue( $ok, "setMulti() succeeded" );
207 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], $now +
86400 );
208 $this->assertTrue( $ok, "Expiry set for all keys" );
209 $this->assertEquals( 1, $this->cache
->get( $key1 ), "Key still live" );
211 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
212 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
213 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
216 $this->cache
->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
220 * @covers MediumSpecificBagOStuff::add
222 public function testAdd() {
223 $key = $this->cache
->makeKey( self
::TEST_KEY
);
224 $this->assertFalse( $this->cache
->get( $key ) );
225 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
226 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
230 * @covers MediumSpecificBagOStuff::get
232 public function testGet() {
233 $value = [ 'this' => 'is', 'a' => 'test' ];
235 $key = $this->cache
->makeKey( self
::TEST_KEY
);
236 $this->cache
->add( $key, $value, 5 );
237 $this->assertEquals( $this->cache
->get( $key ), $value );
241 * @covers MediumSpecificBagOStuff::get
242 * @covers MediumSpecificBagOStuff::set
243 * @covers MediumSpecificBagOStuff::getWithSetCallback
245 public function testGetWithSetCallback() {
247 $cache = new HashBagOStuff( [] );
248 $cache->setMockTime( $now );
249 $key = $cache->makeKey( self
::TEST_KEY
);
251 $this->assertFalse( $cache->get( $key ), "No value" );
253 $value = $cache->getWithSetCallback(
259 return 'hello kitty';
263 $this->assertEquals( 'hello kitty', $value );
264 $this->assertEquals( $value, $cache->get( $key ), "Value set" );
268 $this->assertFalse( $cache->get( $key ), "Value expired" );
272 * @covers MediumSpecificBagOStuff::incr
274 public function testIncr() {
275 $key = $this->cache
->makeKey( self
::TEST_KEY
);
276 $this->cache
->add( $key, 0, 5 );
277 $this->cache
->incr( $key );
279 $actualValue = $this->cache
->get( $key );
280 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
284 * @covers MediumSpecificBagOStuff::incrWithInit
286 public function testIncrWithInit() {
287 $key = $this->cache
->makeKey( self
::TEST_KEY
);
288 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
289 $this->assertEquals( 3, $val, "Correct init value" );
291 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
292 $this->assertEquals( 4, $val, "Correct init value" );
296 * @covers MediumSpecificBagOStuff::getMulti
298 public function testGetMulti() {
299 $value1 = [ 'this' => 'is', 'a' => 'test' ];
300 $value2 = [ 'this' => 'is', 'another' => 'test' ];
301 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
302 $value4 = [ 'another test where chars in key will be encoded' ];
304 $key1 = $this->cache
->makeKey( 'test-1' );
305 $key2 = $this->cache
->makeKey( 'test-2' );
306 // internally, MemcachedBagOStuffs will encode to will-%25-encode
307 $key3 = $this->cache
->makeKey( 'will-%-encode' );
308 $key4 = $this->cache
->makeKey(
309 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
313 $this->cache
->delete( $key1 );
314 $this->cache
->delete( $key2 );
315 $this->cache
->delete( $key3 );
316 $this->cache
->delete( $key4 );
318 $this->cache
->add( $key1, $value1, 5 );
319 $this->cache
->add( $key2, $value2, 5 );
320 $this->cache
->add( $key3, $value3, 5 );
321 $this->cache
->add( $key4, $value4, 5 );
324 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
325 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
329 $this->cache
->delete( $key1 );
330 $this->cache
->delete( $key2 );
331 $this->cache
->delete( $key3 );
332 $this->cache
->delete( $key4 );
336 * @covers MediumSpecificBagOStuff::setMulti
337 * @covers MediumSpecificBagOStuff::deleteMulti
339 public function testSetDeleteMulti() {
341 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
342 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
343 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
344 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
345 $this->cache
->makeKey( 'test-5' ) => 4,
346 $this->cache
->makeKey( 'test-6' ) => 'ever'
349 $this->assertTrue( $this->cache
->setMulti( $map ) );
352 $this->cache
->getMulti( array_keys( $map ) )
355 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
359 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
363 $this->cache
->getMulti( array_keys( $map ) )
368 * @covers MediumSpecificBagOStuff::get
369 * @covers MediumSpecificBagOStuff::getMulti
370 * @covers MediumSpecificBagOStuff::merge
371 * @covers MediumSpecificBagOStuff::delete
373 public function testSetSegmentable() {
374 $key = $this->cache
->makeKey( self
::TEST_KEY
);
376 $small = wfRandomString( 32 );
377 // 64 * 8 * 32768 = 16777216 bytes
378 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
380 $callback = function ( $cache, $key, $oldValue ) {
381 return $oldValue . '!';
384 $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
385 foreach ( $cases as $case => $value ) {
386 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
387 $this->assertEquals( $value, $this->cache
->get( $key ), "get $case" );
388 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key], "get $case" );
391 $this->cache
->merge( $key, $callback, 5, 1, BagOStuff
::WRITE_ALLOW_SEGMENTS
),
396 $this->cache
->get( $key ),
401 $this->cache
->getMulti( [ $key ] )[$key],
405 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ), "delete $case" );
406 $this->assertFalse( $this->cache
->get( $key ), "deleted $case" );
407 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "deletd $case" );
409 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
410 $this->assertEquals( "@$value", $this->cache
->get( $key ), "get $case" );
412 $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
),
415 $this->assertFalse( $this->cache
->get( $key ), "pruned $case" );
416 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ), "pruned $case" );
419 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
421 $this->assertEquals( 666, $this->cache
->get( $key ) );
422 $this->assertEquals( 667, $this->cache
->incr( $key ) );
423 $this->assertEquals( 667, $this->cache
->get( $key ) );
425 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
426 $this->assertEquals( 664, $this->cache
->get( $key ) );
428 $this->assertTrue( $this->cache
->delete( $key ) );
429 $this->assertFalse( $this->cache
->get( $key ) );
433 * @covers MediumSpecificBagOStuff::getScopedLock
435 public function testGetScopedLock() {
436 $key = $this->cache
->makeKey( self
::TEST_KEY
);
437 $value1 = $this->cache
->getScopedLock( $key, 0 );
438 $value2 = $this->cache
->getScopedLock( $key, 0 );
440 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
441 $this->assertNull( $value2, 'Duplicate call returned no lock' );
445 $value3 = $this->cache
->getScopedLock( $key, 0 );
446 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
449 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
450 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
452 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
453 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
457 * @covers MediumSpecificBagOStuff::__construct
458 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
460 public function testReportDupes() {
461 $logger = $this->createMock( Psr\Log\NullLogger
::class );
462 $logger->expects( $this->once() )
463 ->method( 'warning' )
464 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
469 $cache = new HashBagOStuff( [
470 'reportDupes' => true,
471 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
474 $cache->get( 'foo' );
475 $cache->get( 'bar' );
476 $cache->get( 'foo' );
478 DeferredUpdates
::doUpdates();
482 * @covers MediumSpecificBagOStuff::lock()
483 * @covers MediumSpecificBagOStuff::unlock()
485 public function testLocking() {
487 $this->assertTrue( $this->cache
->lock( $key ) );
488 $this->assertFalse( $this->cache
->lock( $key ) );
489 $this->assertTrue( $this->cache
->unlock( $key ) );
492 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
493 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
494 $this->assertTrue( $this->cache
->unlock( $key2 ) );
495 $this->assertTrue( $this->cache
->unlock( $key2 ) );
498 public function tearDown() {
499 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
500 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );