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 testChangeTTL() {
116 $key = $this->cache
->makeKey( self
::TEST_KEY
);
119 $this->cache
->add( $key, $value, 5 );
120 $this->assertEquals( $value, $this->cache
->get( $key ) );
121 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
122 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
123 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
124 $this->assertEquals( $this->cache
->get( $key ), $value );
125 $this->cache
->delete( $key );
126 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
128 $this->cache
->add( $key, $value, 5 );
129 $this->assertTrue( $this->cache
->changeTTL( $key, time() - 3600 ) );
130 $this->assertFalse( $this->cache
->get( $key ) );
134 * @covers MediumSpecificBagOStuff::changeTTLMulti
136 public function testChangeTTLMulti() {
137 $key1 = $this->cache
->makeKey( 'test-key1' );
138 $key2 = $this->cache
->makeKey( 'test-key2' );
139 $key3 = $this->cache
->makeKey( 'test-key3' );
140 $key4 = $this->cache
->makeKey( 'test-key4' );
143 $this->cache
->delete( $key1 );
144 $this->cache
->delete( $key2 );
145 $this->cache
->delete( $key3 );
146 $this->cache
->delete( $key4 );
148 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
149 $this->assertFalse( $ok, "No keys found" );
150 $this->assertFalse( $this->cache
->get( $key1 ) );
151 $this->assertFalse( $this->cache
->get( $key2 ) );
152 $this->assertFalse( $this->cache
->get( $key3 ) );
154 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
156 $this->assertTrue( $ok, "setMulti() succeeded" );
159 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
160 "setMulti() succeeded via getMulti() check"
163 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
164 $this->assertTrue( $ok, "TTL bumped for all keys" );
165 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
166 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
167 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
169 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], time() +
86400 );
170 $this->assertTrue( $ok, "Expiry set for all keys" );
172 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
173 $this->assertFalse( $ok, "One key missing" );
175 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
176 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
177 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
180 $this->cache
->delete( $key1 );
181 $this->cache
->delete( $key2 );
182 $this->cache
->delete( $key3 );
183 $this->cache
->delete( $key4 );
187 * @covers MediumSpecificBagOStuff::add
189 public function testAdd() {
190 $key = $this->cache
->makeKey( self
::TEST_KEY
);
191 $this->assertFalse( $this->cache
->get( $key ) );
192 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
193 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
197 * @covers MediumSpecificBagOStuff::get
199 public function testGet() {
200 $value = [ 'this' => 'is', 'a' => 'test' ];
202 $key = $this->cache
->makeKey( self
::TEST_KEY
);
203 $this->cache
->add( $key, $value, 5 );
204 $this->assertEquals( $this->cache
->get( $key ), $value );
208 * @covers MediumSpecificBagOStuff::get
209 * @covers MediumSpecificBagOStuff::set
210 * @covers MediumSpecificBagOStuff::getWithSetCallback
212 public function testGetWithSetCallback() {
213 $key = $this->cache
->makeKey( self
::TEST_KEY
);
214 $value = $this->cache
->getWithSetCallback(
218 return 'hello kitty';
222 $this->assertEquals( 'hello kitty', $value );
223 $this->assertEquals( $value, $this->cache
->get( $key ) );
227 * @covers MediumSpecificBagOStuff::incr
229 public function testIncr() {
230 $key = $this->cache
->makeKey( self
::TEST_KEY
);
231 $this->cache
->add( $key, 0, 5 );
232 $this->cache
->incr( $key );
234 $actualValue = $this->cache
->get( $key );
235 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
239 * @covers MediumSpecificBagOStuff::incrWithInit
241 public function testIncrWithInit() {
242 $key = $this->cache
->makeKey( self
::TEST_KEY
);
243 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
244 $this->assertEquals( 3, $val, "Correct init value" );
246 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
247 $this->assertEquals( 4, $val, "Correct init value" );
251 * @covers MediumSpecificBagOStuff::getMulti
253 public function testGetMulti() {
254 $value1 = [ 'this' => 'is', 'a' => 'test' ];
255 $value2 = [ 'this' => 'is', 'another' => 'test' ];
256 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
257 $value4 = [ 'another test where chars in key will be encoded' ];
259 $key1 = $this->cache
->makeKey( 'test-1' );
260 $key2 = $this->cache
->makeKey( 'test-2' );
261 // internally, MemcachedBagOStuffs will encode to will-%25-encode
262 $key3 = $this->cache
->makeKey( 'will-%-encode' );
263 $key4 = $this->cache
->makeKey(
264 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
268 $this->cache
->delete( $key1 );
269 $this->cache
->delete( $key2 );
270 $this->cache
->delete( $key3 );
271 $this->cache
->delete( $key4 );
273 $this->cache
->add( $key1, $value1, 5 );
274 $this->cache
->add( $key2, $value2, 5 );
275 $this->cache
->add( $key3, $value3, 5 );
276 $this->cache
->add( $key4, $value4, 5 );
279 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
280 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
284 $this->cache
->delete( $key1 );
285 $this->cache
->delete( $key2 );
286 $this->cache
->delete( $key3 );
287 $this->cache
->delete( $key4 );
291 * @covers MediumSpecificBagOStuff::setMulti
292 * @covers MediumSpecificBagOStuff::deleteMulti
294 public function testSetDeleteMulti() {
296 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
297 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
298 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
299 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
300 $this->cache
->makeKey( 'test-5' ) => 4,
301 $this->cache
->makeKey( 'test-6' ) => 'ever'
304 $this->assertTrue( $this->cache
->setMulti( $map ) );
307 $this->cache
->getMulti( array_keys( $map ) )
310 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
314 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
318 $this->cache
->getMulti( array_keys( $map ) )
323 * @covers MediumSpecificBagOStuff::get
324 * @covers MediumSpecificBagOStuff::getMulti
325 * @covers MediumSpecificBagOStuff::merge
326 * @covers MediumSpecificBagOStuff::delete
328 public function testSetSegmentable() {
329 $key = $this->cache
->makeKey( self
::TEST_KEY
);
331 $small = wfRandomString( 32 );
332 // 64 * 8 * 32768 = 16777216 bytes
333 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
335 $callback = function ( $cache, $key, $oldValue ) {
336 return $oldValue . '!';
339 foreach ( [ $tiny, $small, $big ] as $value ) {
340 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
341 $this->assertEquals( $value, $this->cache
->get( $key ) );
342 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key] );
344 $this->assertTrue( $this->cache
->merge( $key, $callback, 5 ) );
345 $this->assertEquals( "$value!", $this->cache
->get( $key ) );
346 $this->assertEquals( "$value!", $this->cache
->getMulti( [ $key ] )[$key] );
348 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ) );
349 $this->assertFalse( $this->cache
->get( $key ) );
350 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
352 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
353 $this->assertEquals( "@$value", $this->cache
->get( $key ) );
354 $this->assertTrue( $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
) );
355 $this->assertFalse( $this->cache
->get( $key ) );
356 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
359 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
361 $this->assertEquals( 666, $this->cache
->get( $key ) );
362 $this->assertEquals( 667, $this->cache
->incr( $key ) );
363 $this->assertEquals( 667, $this->cache
->get( $key ) );
365 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
366 $this->assertEquals( 664, $this->cache
->get( $key ) );
368 $this->assertTrue( $this->cache
->delete( $key ) );
369 $this->assertFalse( $this->cache
->get( $key ) );
373 * @covers MediumSpecificBagOStuff::getScopedLock
375 public function testGetScopedLock() {
376 $key = $this->cache
->makeKey( self
::TEST_KEY
);
377 $value1 = $this->cache
->getScopedLock( $key, 0 );
378 $value2 = $this->cache
->getScopedLock( $key, 0 );
380 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
381 $this->assertNull( $value2, 'Duplicate call returned no lock' );
385 $value3 = $this->cache
->getScopedLock( $key, 0 );
386 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
389 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
390 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
392 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
393 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
397 * @covers MediumSpecificBagOStuff::__construct
398 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
400 public function testReportDupes() {
401 $logger = $this->createMock( Psr\Log\NullLogger
::class );
402 $logger->expects( $this->once() )
403 ->method( 'warning' )
404 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
409 $cache = new HashBagOStuff( [
410 'reportDupes' => true,
411 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
414 $cache->get( 'foo' );
415 $cache->get( 'bar' );
416 $cache->get( 'foo' );
418 DeferredUpdates
::doUpdates();
422 * @covers MediumSpecificBagOStuff::lock()
423 * @covers MediumSpecificBagOStuff::unlock()
425 public function testLocking() {
427 $this->assertTrue( $this->cache
->lock( $key ) );
428 $this->assertFalse( $this->cache
->lock( $key ) );
429 $this->assertTrue( $this->cache
->unlock( $key ) );
432 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
433 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
434 $this->assertTrue( $this->cache
->unlock( $key2 ) );
435 $this->assertTrue( $this->cache
->unlock( $key2 ) );
438 public function tearDown() {
439 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
440 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );