3 use Wikimedia\ScopedCallback
;
4 use Wikimedia\TestingAccessWrapper
;
7 * @author Matthias Mullie <mmullie@wikimedia.org>
10 class BagOStuffTest
extends MediaWikiTestCase
{
14 const TEST_KEY
= 'test';
16 protected function setUp() {
19 // type defined through parameter
20 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
21 $name = $this->getCliArg( 'use-bagostuff' );
23 $this->cache
= ObjectCache
::newFromId( $name );
25 // no type defined - use simple hash
26 $this->cache
= new HashBagOStuff
;
29 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
30 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
34 * @covers BagOStuff::makeGlobalKey
35 * @covers BagOStuff::makeKeyInternal
37 public function testMakeKey() {
38 $cache = ObjectCache
::newFromId( 'hash' );
40 $localKey = $cache->makeKey( 'first', 'second', 'third' );
41 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
43 $this->assertStringMatchesFormat(
44 '%Sfirst%Ssecond%Sthird%S',
46 'Local key interpolates parameters'
49 $this->assertStringMatchesFormat(
50 'global%Sfirst%Ssecond%Sthird%S',
52 'Global key interpolates parameters and contains global prefix'
55 $this->assertNotEquals(
58 'Local key and global key with same parameters should not be equal'
61 $this->assertNotEquals(
62 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
63 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
68 * @covers BagOStuff::merge
69 * @covers BagOStuff::mergeViaCas
71 public function testMerge() {
72 $key = $this->cache
->makeKey( self
::TEST_KEY
);
75 $casRace = false; // emulate a race
76 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
80 $cache->set( $key, 'conflict', 5 );
83 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
86 // merge on non-existing value
87 $merged = $this->cache
->merge( $key, $callback, 5 );
88 $this->assertTrue( $merged );
89 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
91 // merge on existing value
92 $merged = $this->cache
->merge( $key, $callback, 5 );
93 $this->assertTrue( $merged );
94 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
99 $this->cache
->merge( $key, $callback, 5, 1 ),
100 'Non-blocking merge (CAS)'
103 if ( $this->cache
instanceof MultiWriteBagOStuff
) {
104 $wrapper = TestingAccessWrapper
::newFromObject( $this->cache
);
105 $this->assertEquals( count( $wrapper->caches
), $calls );
107 $this->assertEquals( 1, $calls );
112 * @covers BagOStuff::changeTTL
114 public function testChangeTTL() {
115 $key = $this->cache
->makeKey( self
::TEST_KEY
);
118 $this->cache
->add( $key, $value, 5 );
119 $this->assertEquals( $value, $this->cache
->get( $key ) );
120 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
121 $this->assertTrue( $this->cache
->changeTTL( $key, 10 ) );
122 $this->assertTrue( $this->cache
->changeTTL( $key, 0 ) );
123 $this->assertEquals( $this->cache
->get( $key ), $value );
124 $this->cache
->delete( $key );
125 $this->assertFalse( $this->cache
->changeTTL( $key, 15 ) );
127 $this->cache
->add( $key, $value, 5 );
128 $this->assertTrue( $this->cache
->changeTTL( $key, time() - 3600 ) );
129 $this->assertFalse( $this->cache
->get( $key ) );
133 * @covers BagOStuff::add
135 public function testAdd() {
136 $key = $this->cache
->makeKey( self
::TEST_KEY
);
137 $this->assertFalse( $this->cache
->get( $key ) );
138 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
139 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
143 * @covers BagOStuff::get
145 public function testGet() {
146 $value = [ 'this' => 'is', 'a' => 'test' ];
148 $key = $this->cache
->makeKey( self
::TEST_KEY
);
149 $this->cache
->add( $key, $value, 5 );
150 $this->assertEquals( $this->cache
->get( $key ), $value );
154 * @covers BagOStuff::get
155 * @covers BagOStuff::set
156 * @covers BagOStuff::getWithSetCallback
158 public function testGetWithSetCallback() {
159 $key = $this->cache
->makeKey( self
::TEST_KEY
);
160 $value = $this->cache
->getWithSetCallback(
164 return 'hello kitty';
168 $this->assertEquals( 'hello kitty', $value );
169 $this->assertEquals( $value, $this->cache
->get( $key ) );
173 * @covers BagOStuff::incr
175 public function testIncr() {
176 $key = $this->cache
->makeKey( self
::TEST_KEY
);
177 $this->cache
->add( $key, 0, 5 );
178 $this->cache
->incr( $key );
180 $actualValue = $this->cache
->get( $key );
181 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
185 * @covers BagOStuff::incrWithInit
187 public function testIncrWithInit() {
188 $key = $this->cache
->makeKey( self
::TEST_KEY
);
189 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
190 $this->assertEquals( 3, $val, "Correct init value" );
192 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
193 $this->assertEquals( 4, $val, "Correct init value" );
197 * @covers BagOStuff::getMulti
199 public function testGetMulti() {
200 $value1 = [ 'this' => 'is', 'a' => 'test' ];
201 $value2 = [ 'this' => 'is', 'another' => 'test' ];
202 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
203 $value4 = [ 'another test where chars in key will be encoded' ];
205 $key1 = $this->cache
->makeKey( 'test-1' );
206 $key2 = $this->cache
->makeKey( 'test-2' );
207 // internally, MemcachedBagOStuffs will encode to will-%25-encode
208 $key3 = $this->cache
->makeKey( 'will-%-encode' );
209 $key4 = $this->cache
->makeKey(
210 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
214 $this->cache
->delete( $key1 );
215 $this->cache
->delete( $key2 );
216 $this->cache
->delete( $key3 );
217 $this->cache
->delete( $key4 );
219 $this->cache
->add( $key1, $value1, 5 );
220 $this->cache
->add( $key2, $value2, 5 );
221 $this->cache
->add( $key3, $value3, 5 );
222 $this->cache
->add( $key4, $value4, 5 );
225 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
226 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
230 $this->cache
->delete( $key1 );
231 $this->cache
->delete( $key2 );
232 $this->cache
->delete( $key3 );
233 $this->cache
->delete( $key4 );
237 * @covers BagOStuff::setMulti
238 * @covers BagOStuff::deleteMulti
240 public function testSetDeleteMulti() {
242 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
243 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
244 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
245 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
246 $this->cache
->makeKey( 'test-5' ) => 4,
247 $this->cache
->makeKey( 'test-6' ) => 'ever'
250 $this->assertTrue( $this->cache
->setMulti( $map ) );
253 $this->cache
->getMulti( array_keys( $map ) )
256 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
260 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
264 $this->cache
->getMulti( array_keys( $map ) )
269 * @covers BagOStuff::get
270 * @covers BagOStuff::getMulti
271 * @covers BagOStuff::merge
272 * @covers BagOStuff::delete
274 public function testSetSegmentable() {
275 $key = $this->cache
->makeKey( self
::TEST_KEY
);
277 $small = wfRandomString( 32 );
278 // 64 * 8 * 32768 = 16777216 bytes
279 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
281 $callback = function ( $cache, $key, $oldValue ) {
282 return $oldValue . '!';
285 foreach ( [ $tiny, $small, $big ] as $value ) {
286 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
287 $this->assertEquals( $value, $this->cache
->get( $key ) );
288 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key] );
290 $this->assertTrue( $this->cache
->merge( $key, $callback, 5 ) );
291 $this->assertEquals( "$value!", $this->cache
->get( $key ) );
292 $this->assertEquals( "$value!", $this->cache
->getMulti( [ $key ] )[$key] );
294 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ) );
295 $this->assertFalse( $this->cache
->get( $key ) );
296 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
298 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
299 $this->assertEquals( "@$value", $this->cache
->get( $key ) );
300 $this->assertTrue( $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
) );
301 $this->assertFalse( $this->cache
->get( $key ) );
302 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
305 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
307 $this->assertEquals( 667, $this->cache
->incr( $key ) );
308 $this->assertEquals( 667, $this->cache
->get( $key ) );
310 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
311 $this->assertEquals( 664, $this->cache
->get( $key ) );
313 $this->assertTrue( $this->cache
->delete( $key ) );
314 $this->assertFalse( $this->cache
->get( $key ) );
318 * @covers BagOStuff::getScopedLock
320 public function testGetScopedLock() {
321 $key = $this->cache
->makeKey( self
::TEST_KEY
);
322 $value1 = $this->cache
->getScopedLock( $key, 0 );
323 $value2 = $this->cache
->getScopedLock( $key, 0 );
325 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
326 $this->assertNull( $value2, 'Duplicate call returned no lock' );
330 $value3 = $this->cache
->getScopedLock( $key, 0 );
331 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
334 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
335 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
337 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
338 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
342 * @covers BagOStuff::__construct
343 * @covers BagOStuff::trackDuplicateKeys
345 public function testReportDupes() {
346 $logger = $this->createMock( Psr\Log\NullLogger
::class );
347 $logger->expects( $this->once() )
348 ->method( 'warning' )
349 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
354 $cache = new HashBagOStuff( [
355 'reportDupes' => true,
356 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
359 $cache->get( 'foo' );
360 $cache->get( 'bar' );
361 $cache->get( 'foo' );
363 DeferredUpdates
::doUpdates();
367 * @covers BagOStuff::lock()
368 * @covers BagOStuff::unlock()
370 public function testLocking() {
372 $this->assertTrue( $this->cache
->lock( $key ) );
373 $this->assertFalse( $this->cache
->lock( $key ) );
374 $this->assertTrue( $this->cache
->unlock( $key ) );
377 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
378 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
379 $this->assertTrue( $this->cache
->unlock( $key2 ) );
380 $this->assertTrue( $this->cache
->unlock( $key2 ) );
383 public function tearDown() {
384 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
385 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );