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::changeTTLMulti
135 public function testChangeTTLMulti() {
136 $key1 = $this->cache
->makeKey( 'test-key1' );
137 $key2 = $this->cache
->makeKey( 'test-key2' );
138 $key3 = $this->cache
->makeKey( 'test-key3' );
139 $key4 = $this->cache
->makeKey( 'test-key4' );
142 $this->cache
->delete( $key1 );
143 $this->cache
->delete( $key2 );
144 $this->cache
->delete( $key3 );
145 $this->cache
->delete( $key4 );
147 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
148 $this->assertFalse( $ok, "No keys found" );
149 $this->assertFalse( $this->cache
->get( $key1 ) );
150 $this->assertFalse( $this->cache
->get( $key2 ) );
151 $this->assertFalse( $this->cache
->get( $key3 ) );
153 $ok = $this->cache
->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
155 $this->assertTrue( $ok, "setMulti() succeeded" );
158 count( $this->cache
->getMulti( [ $key1, $key2, $key3 ] ) ),
159 "setMulti() succeeded via getMulti() check"
162 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
163 $this->assertTrue( $ok, "TTL bumped for all keys" );
164 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
165 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
166 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
168 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], time() +
86400 );
169 $this->assertTrue( $ok, "Expiry set for all keys" );
171 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
172 $this->assertFalse( $ok, "One key missing" );
174 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
175 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
176 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
179 $this->cache
->delete( $key1 );
180 $this->cache
->delete( $key2 );
181 $this->cache
->delete( $key3 );
182 $this->cache
->delete( $key4 );
186 * @covers BagOStuff::add
188 public function testAdd() {
189 $key = $this->cache
->makeKey( self
::TEST_KEY
);
190 $this->assertFalse( $this->cache
->get( $key ) );
191 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
192 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
196 * @covers BagOStuff::get
198 public function testGet() {
199 $value = [ 'this' => 'is', 'a' => 'test' ];
201 $key = $this->cache
->makeKey( self
::TEST_KEY
);
202 $this->cache
->add( $key, $value, 5 );
203 $this->assertEquals( $this->cache
->get( $key ), $value );
207 * @covers BagOStuff::get
208 * @covers BagOStuff::set
209 * @covers BagOStuff::getWithSetCallback
211 public function testGetWithSetCallback() {
212 $key = $this->cache
->makeKey( self
::TEST_KEY
);
213 $value = $this->cache
->getWithSetCallback(
217 return 'hello kitty';
221 $this->assertEquals( 'hello kitty', $value );
222 $this->assertEquals( $value, $this->cache
->get( $key ) );
226 * @covers BagOStuff::incr
228 public function testIncr() {
229 $key = $this->cache
->makeKey( self
::TEST_KEY
);
230 $this->cache
->add( $key, 0, 5 );
231 $this->cache
->incr( $key );
233 $actualValue = $this->cache
->get( $key );
234 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
238 * @covers BagOStuff::incrWithInit
240 public function testIncrWithInit() {
241 $key = $this->cache
->makeKey( self
::TEST_KEY
);
242 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
243 $this->assertEquals( 3, $val, "Correct init value" );
245 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
246 $this->assertEquals( 4, $val, "Correct init value" );
250 * @covers BagOStuff::getMulti
252 public function testGetMulti() {
253 $value1 = [ 'this' => 'is', 'a' => 'test' ];
254 $value2 = [ 'this' => 'is', 'another' => 'test' ];
255 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
256 $value4 = [ 'another test where chars in key will be encoded' ];
258 $key1 = $this->cache
->makeKey( 'test-1' );
259 $key2 = $this->cache
->makeKey( 'test-2' );
260 // internally, MemcachedBagOStuffs will encode to will-%25-encode
261 $key3 = $this->cache
->makeKey( 'will-%-encode' );
262 $key4 = $this->cache
->makeKey(
263 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
267 $this->cache
->delete( $key1 );
268 $this->cache
->delete( $key2 );
269 $this->cache
->delete( $key3 );
270 $this->cache
->delete( $key4 );
272 $this->cache
->add( $key1, $value1, 5 );
273 $this->cache
->add( $key2, $value2, 5 );
274 $this->cache
->add( $key3, $value3, 5 );
275 $this->cache
->add( $key4, $value4, 5 );
278 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
279 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
283 $this->cache
->delete( $key1 );
284 $this->cache
->delete( $key2 );
285 $this->cache
->delete( $key3 );
286 $this->cache
->delete( $key4 );
290 * @covers BagOStuff::setMulti
291 * @covers BagOStuff::deleteMulti
293 public function testSetDeleteMulti() {
295 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
296 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
297 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
298 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
299 $this->cache
->makeKey( 'test-5' ) => 4,
300 $this->cache
->makeKey( 'test-6' ) => 'ever'
303 $this->assertTrue( $this->cache
->setMulti( $map ) );
306 $this->cache
->getMulti( array_keys( $map ) )
309 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
313 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
317 $this->cache
->getMulti( array_keys( $map ) )
322 * @covers BagOStuff::get
323 * @covers BagOStuff::getMulti
324 * @covers BagOStuff::merge
325 * @covers BagOStuff::delete
327 public function testSetSegmentable() {
328 $key = $this->cache
->makeKey( self
::TEST_KEY
);
330 $small = wfRandomString( 32 );
331 // 64 * 8 * 32768 = 16777216 bytes
332 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
334 $callback = function ( $cache, $key, $oldValue ) {
335 return $oldValue . '!';
338 foreach ( [ $tiny, $small, $big ] as $value ) {
339 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
340 $this->assertEquals( $value, $this->cache
->get( $key ) );
341 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key] );
343 $this->assertTrue( $this->cache
->merge( $key, $callback, 5 ) );
344 $this->assertEquals( "$value!", $this->cache
->get( $key ) );
345 $this->assertEquals( "$value!", $this->cache
->getMulti( [ $key ] )[$key] );
347 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ) );
348 $this->assertFalse( $this->cache
->get( $key ) );
349 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
351 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
352 $this->assertEquals( "@$value", $this->cache
->get( $key ) );
353 $this->assertTrue( $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
) );
354 $this->assertFalse( $this->cache
->get( $key ) );
355 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
358 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
360 $this->assertEquals( 666, $this->cache
->get( $key ) );
361 $this->assertEquals( 667, $this->cache
->incr( $key ) );
362 $this->assertEquals( 667, $this->cache
->get( $key ) );
364 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
365 $this->assertEquals( 664, $this->cache
->get( $key ) );
367 $this->assertTrue( $this->cache
->delete( $key ) );
368 $this->assertFalse( $this->cache
->get( $key ) );
372 * @covers BagOStuff::getScopedLock
374 public function testGetScopedLock() {
375 $key = $this->cache
->makeKey( self
::TEST_KEY
);
376 $value1 = $this->cache
->getScopedLock( $key, 0 );
377 $value2 = $this->cache
->getScopedLock( $key, 0 );
379 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
380 $this->assertNull( $value2, 'Duplicate call returned no lock' );
384 $value3 = $this->cache
->getScopedLock( $key, 0 );
385 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
388 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
389 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
391 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
392 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
396 * @covers BagOStuff::__construct
397 * @covers BagOStuff::trackDuplicateKeys
399 public function testReportDupes() {
400 $logger = $this->createMock( Psr\Log\NullLogger
::class );
401 $logger->expects( $this->once() )
402 ->method( 'warning' )
403 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
408 $cache = new HashBagOStuff( [
409 'reportDupes' => true,
410 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
413 $cache->get( 'foo' );
414 $cache->get( 'bar' );
415 $cache->get( 'foo' );
417 DeferredUpdates
::doUpdates();
421 * @covers BagOStuff::lock()
422 * @covers BagOStuff::unlock()
424 public function testLocking() {
426 $this->assertTrue( $this->cache
->lock( $key ) );
427 $this->assertFalse( $this->cache
->lock( $key ) );
428 $this->assertTrue( $this->cache
->unlock( $key ) );
431 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
432 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
433 $this->assertTrue( $this->cache
->unlock( $key2 ) );
434 $this->assertTrue( $this->cache
->unlock( $key2 ) );
437 public function tearDown() {
438 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
439 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );