3 use Wikimedia\ScopedCallback
;
6 * @author Matthias Mullie <mmullie@wikimedia.org>
9 class BagOStuffTest
extends MediaWikiTestCase
{
13 const TEST_KEY
= 'test';
15 protected function setUp() {
18 // type defined through parameter
19 if ( $this->getCliArg( 'use-bagostuff' ) ) {
20 $name = $this->getCliArg( 'use-bagostuff' );
22 $this->cache
= ObjectCache
::newFromId( $name );
24 // no type defined - use simple hash
25 $this->cache
= new HashBagOStuff
;
28 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
29 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );
33 * @covers BagOStuff::makeGlobalKey
34 * @covers BagOStuff::makeKeyInternal
36 public function testMakeKey() {
37 $cache = ObjectCache
::newFromId( 'hash' );
39 $localKey = $cache->makeKey( 'first', 'second', 'third' );
40 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
42 $this->assertStringMatchesFormat(
43 '%Sfirst%Ssecond%Sthird%S',
45 'Local key interpolates parameters'
48 $this->assertStringMatchesFormat(
49 'global%Sfirst%Ssecond%Sthird%S',
51 'Global key interpolates parameters and contains global prefix'
54 $this->assertNotEquals(
57 'Local key and global key with same parameters should not be equal'
60 $this->assertNotEquals(
61 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
62 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
67 * @covers BagOStuff::merge
68 * @covers BagOStuff::mergeViaLock
69 * @covers BagOStuff::mergeViaCas
71 public function testMerge() {
72 $key = $this->cache
->makeKey( self
::TEST_KEY
);
74 $checkLockingCallback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$locks ) {
75 $locks = $cache->get( "$key:lock" );
80 $this->cache
->merge( $key, $checkLockingCallback, 5 );
81 $this->assertFalse( $this->cache
->get( $key ) );
84 $casRace = false; // emulate a race
85 $callback = function ( BagOStuff
$cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
89 $cache->set( $key, 'conflict', 5 );
92 return ( $oldVal === false ) ?
'merged' : $oldVal . 'merged';
95 // merge on non-existing value
96 $merged = $this->cache
->merge( $key, $callback, 5 );
97 $this->assertTrue( $merged );
98 $this->assertEquals( 'merged', $this->cache
->get( $key ) );
100 // merge on existing value
101 $merged = $this->cache
->merge( $key, $callback, 5 );
102 $this->assertTrue( $merged );
103 $this->assertEquals( 'mergedmerged', $this->cache
->get( $key ) );
107 // merge were something else already was merging (e.g. had the lock)
108 $this->cache
->lock( $key );
110 $this->cache
->merge( $key, $callback, 5, 1 ),
111 'Non-blocking merge (locking)'
113 $this->cache
->unlock( $key );
114 $this->assertEquals( 0, $calls );
118 $this->cache
->merge( $key, $callback, 5, 1 ),
119 'Non-blocking merge (CAS)'
121 $this->assertEquals( 1, $calls );
126 * @covers BagOStuff::merge
127 * @covers BagOStuff::mergeViaLock
128 * @dataProvider provideTestMerge_fork
130 public function testMerge_fork( $exists, $winsLocking, $resLocking, $resCAS ) {
131 $key = $this->cache
->makeKey( self
::TEST_KEY
);
132 $pCallback = function ( BagOStuff
$cache, $key, $oldVal ) {
133 return ( $oldVal === false ) ?
'init-parent' : $oldVal . '-merged-parent';
135 $cCallback = function ( BagOStuff
$cache, $key, $oldVal ) {
136 return ( $oldVal === false ) ?
'init-child' : $oldVal . '-merged-child';
140 $this->cache
->set( $key, 'x', 5 );
144 * Test concurrent merges by forking this process, if:
145 * - not manually called with --use-bagostuff
146 * - pcntl_fork is supported by the system
147 * - cache type will correctly support calls over forks
149 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
150 $fork &= function_exists( 'pcntl_fork' );
151 $fork &= !$this->cache
instanceof HashBagOStuff
;
152 $fork &= !$this->cache
instanceof EmptyBagOStuff
;
153 $fork &= !$this->cache
instanceof MultiWriteBagOStuff
;
157 // Function to start merge(), run another merge() midway through, then finish
158 $func = function ( BagOStuff
$cache, $key, $cur )
159 use ( $pCallback, $cCallback, &$pid, &$locked )
165 $locked = $cache->get( "$key:lock" ); // parent has lock?
166 pcntl_wait( $status );
168 return $pCallback( $cache, $key, $cur );
170 $this->cache
->merge( $key, $cCallback, 0, 1 );
171 // Bail out of the outer merge() in the child process since it does not
172 // need to attempt to write anything. Success is checked by the parent.
173 parent
::tearDown(); // avoid phpunit notices
178 // attempt a merge - this should fail
179 $merged = $this->cache
->merge( $key, $func, 0, 1 );
182 return; // can't fork, ignore this test...
186 // merge succeed since child was locked out
187 $this->assertEquals( $winsLocking, $merged );
188 $this->assertEquals( $this->cache
->get( $key ), $resLocking );
190 // merge has failed because child process was merging (and we only attempted once)
191 $this->assertEquals( !$winsLocking, $merged );
192 $this->assertEquals( $this->cache
->get( $key ), $resCAS );
195 $this->markTestSkipped( 'No pcntl methods available' );
199 function provideTestMerge_fork() {
201 // (already exists, parent wins if locking, result if locking, result if CAS)
202 [ false, true, 'init-parent', 'init-child' ],
203 [ true, true, 'x-merged-parent', 'x-merged-child' ]
208 * @covers BagOStuff::changeTTL
210 public function testChangeTTL() {
211 $key = $this->cache
->makeKey( self
::TEST_KEY
);
214 $this->cache
->add( $key, $value, 5 );
215 $this->assertTrue( $this->cache
->changeTTL( $key, 5 ) );
216 $this->assertEquals( $this->cache
->get( $key ), $value );
217 $this->cache
->delete( $key );
218 $this->assertFalse( $this->cache
->changeTTL( $key, 5 ) );
222 * @covers BagOStuff::add
224 public function testAdd() {
225 $key = $this->cache
->makeKey( self
::TEST_KEY
);
226 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
230 * @covers BagOStuff::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 BagOStuff::get
242 * @covers BagOStuff::set
243 * @covers BagOStuff::getWithSetCallback
245 public function testGetWithSetCallback() {
246 $key = $this->cache
->makeKey( self
::TEST_KEY
);
247 $value = $this->cache
->getWithSetCallback(
251 return 'hello kitty';
255 $this->assertEquals( 'hello kitty', $value );
256 $this->assertEquals( $value, $this->cache
->get( $key ) );
260 * @covers BagOStuff::incr
262 public function testIncr() {
263 $key = $this->cache
->makeKey( self
::TEST_KEY
);
264 $this->cache
->add( $key, 0, 5 );
265 $this->cache
->incr( $key );
267 $actualValue = $this->cache
->get( $key );
268 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
272 * @covers BagOStuff::incrWithInit
274 public function testIncrWithInit() {
275 $key = $this->cache
->makeKey( self
::TEST_KEY
);
276 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
277 $this->assertEquals( 3, $val, "Correct init value" );
279 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
280 $this->assertEquals( 4, $val, "Correct init value" );
284 * @covers BagOStuff::getMulti
286 public function testGetMulti() {
287 $value1 = [ 'this' => 'is', 'a' => 'test' ];
288 $value2 = [ 'this' => 'is', 'another' => 'test' ];
289 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
290 $value4 = [ 'another test where chars in key will be encoded' ];
292 $key1 = $this->cache
->makeKey( 'test-1' );
293 $key2 = $this->cache
->makeKey( 'test-2' );
294 // internally, MemcachedBagOStuffs will encode to will-%25-encode
295 $key3 = $this->cache
->makeKey( 'will-%-encode' );
296 $key4 = $this->cache
->makeKey(
297 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
301 $this->cache
->delete( $key1 );
302 $this->cache
->delete( $key2 );
303 $this->cache
->delete( $key3 );
304 $this->cache
->delete( $key4 );
306 $this->cache
->add( $key1, $value1, 5 );
307 $this->cache
->add( $key2, $value2, 5 );
308 $this->cache
->add( $key3, $value3, 5 );
309 $this->cache
->add( $key4, $value4, 5 );
312 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
313 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
317 $this->cache
->delete( $key1 );
318 $this->cache
->delete( $key2 );
319 $this->cache
->delete( $key3 );
320 $this->cache
->delete( $key4 );
324 * @covers BagOStuff::setMulti
325 * @covers BagOStuff::deleteMulti
327 public function testSetDeleteMulti() {
329 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
330 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
331 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
332 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
333 $this->cache
->makeKey( 'test-5' ) => 4,
334 $this->cache
->makeKey( 'test-6' ) => 'ever'
337 $this->cache
->setMulti( $map, 5 );
340 $this->cache
->getMulti( array_keys( $map ) )
343 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ), 5 ) );
347 $this->cache
->getMulti( array_keys( $map ) )
352 * @covers BagOStuff::getScopedLock
354 public function testGetScopedLock() {
355 $key = $this->cache
->makeKey( self
::TEST_KEY
);
356 $value1 = $this->cache
->getScopedLock( $key, 0 );
357 $value2 = $this->cache
->getScopedLock( $key, 0 );
359 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
360 $this->assertNull( $value2, 'Duplicate call returned no lock' );
364 $value3 = $this->cache
->getScopedLock( $key, 0 );
365 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
368 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
369 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
371 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
372 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
376 * @covers BagOStuff::__construct
377 * @covers BagOStuff::trackDuplicateKeys
379 public function testReportDupes() {
380 $logger = $this->createMock( Psr\Log\NullLogger
::class );
381 $logger->expects( $this->once() )
382 ->method( 'warning' )
383 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
388 $cache = new HashBagOStuff( [
389 'reportDupes' => true,
390 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
393 $cache->get( 'foo' );
394 $cache->get( 'bar' );
395 $cache->get( 'foo' );
397 DeferredUpdates
::doUpdates();
401 * @covers BagOStuff::lock()
402 * @covers BagOStuff::unlock()
404 public function testLocking() {
406 $this->assertTrue( $this->cache
->lock( $key ) );
407 $this->assertFalse( $this->cache
->lock( $key ) );
408 $this->assertTrue( $this->cache
->unlock( $key ) );
411 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
412 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
413 $this->assertTrue( $this->cache
->unlock( $key2 ) );
414 $this->assertTrue( $this->cache
->unlock( $key2 ) );