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 $this->cache
->setMulti( [
159 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
160 $this->assertTrue( $ok, "TTL bumped for all keys" );
161 $this->assertEquals( 1, $this->cache
->get( $key1 ) );
162 $this->assertEquals( 2, $this->cache
->get( $key2 ) );
163 $this->assertEquals( 3, $this->cache
->get( $key3 ) );
165 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3 ], time() +
86400 );
166 $this->assertTrue( $ok, "Expiry set for all keys" );
168 $ok = $this->cache
->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
169 $this->assertFalse( $ok, "One key missing" );
171 $this->assertEquals( 2, $this->cache
->incr( $key1 ) );
172 $this->assertEquals( 3, $this->cache
->incr( $key2 ) );
173 $this->assertEquals( 4, $this->cache
->incr( $key3 ) );
176 $this->cache
->delete( $key1 );
177 $this->cache
->delete( $key2 );
178 $this->cache
->delete( $key3 );
179 $this->cache
->delete( $key4 );
183 * @covers BagOStuff::add
185 public function testAdd() {
186 $key = $this->cache
->makeKey( self
::TEST_KEY
);
187 $this->assertFalse( $this->cache
->get( $key ) );
188 $this->assertTrue( $this->cache
->add( $key, 'test', 5 ) );
189 $this->assertFalse( $this->cache
->add( $key, 'test', 5 ) );
193 * @covers BagOStuff::get
195 public function testGet() {
196 $value = [ 'this' => 'is', 'a' => 'test' ];
198 $key = $this->cache
->makeKey( self
::TEST_KEY
);
199 $this->cache
->add( $key, $value, 5 );
200 $this->assertEquals( $this->cache
->get( $key ), $value );
204 * @covers BagOStuff::get
205 * @covers BagOStuff::set
206 * @covers BagOStuff::getWithSetCallback
208 public function testGetWithSetCallback() {
209 $key = $this->cache
->makeKey( self
::TEST_KEY
);
210 $value = $this->cache
->getWithSetCallback(
214 return 'hello kitty';
218 $this->assertEquals( 'hello kitty', $value );
219 $this->assertEquals( $value, $this->cache
->get( $key ) );
223 * @covers BagOStuff::incr
225 public function testIncr() {
226 $key = $this->cache
->makeKey( self
::TEST_KEY
);
227 $this->cache
->add( $key, 0, 5 );
228 $this->cache
->incr( $key );
230 $actualValue = $this->cache
->get( $key );
231 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
235 * @covers BagOStuff::incrWithInit
237 public function testIncrWithInit() {
238 $key = $this->cache
->makeKey( self
::TEST_KEY
);
239 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
240 $this->assertEquals( 3, $val, "Correct init value" );
242 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
243 $this->assertEquals( 4, $val, "Correct init value" );
247 * @covers BagOStuff::getMulti
249 public function testGetMulti() {
250 $value1 = [ 'this' => 'is', 'a' => 'test' ];
251 $value2 = [ 'this' => 'is', 'another' => 'test' ];
252 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
253 $value4 = [ 'another test where chars in key will be encoded' ];
255 $key1 = $this->cache
->makeKey( 'test-1' );
256 $key2 = $this->cache
->makeKey( 'test-2' );
257 // internally, MemcachedBagOStuffs will encode to will-%25-encode
258 $key3 = $this->cache
->makeKey( 'will-%-encode' );
259 $key4 = $this->cache
->makeKey(
260 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
264 $this->cache
->delete( $key1 );
265 $this->cache
->delete( $key2 );
266 $this->cache
->delete( $key3 );
267 $this->cache
->delete( $key4 );
269 $this->cache
->add( $key1, $value1, 5 );
270 $this->cache
->add( $key2, $value2, 5 );
271 $this->cache
->add( $key3, $value3, 5 );
272 $this->cache
->add( $key4, $value4, 5 );
275 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
276 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
280 $this->cache
->delete( $key1 );
281 $this->cache
->delete( $key2 );
282 $this->cache
->delete( $key3 );
283 $this->cache
->delete( $key4 );
287 * @covers BagOStuff::setMulti
288 * @covers BagOStuff::deleteMulti
290 public function testSetDeleteMulti() {
292 $this->cache
->makeKey( 'test-1' ) => 'Siberian',
293 $this->cache
->makeKey( 'test-2' ) => [ 'Huskies' ],
294 $this->cache
->makeKey( 'test-3' ) => [ 'are' => 'the' ],
295 $this->cache
->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
296 $this->cache
->makeKey( 'test-5' ) => 4,
297 $this->cache
->makeKey( 'test-6' ) => 'ever'
300 $this->assertTrue( $this->cache
->setMulti( $map ) );
303 $this->cache
->getMulti( array_keys( $map ) )
306 $this->assertTrue( $this->cache
->deleteMulti( array_keys( $map ) ) );
310 $this->cache
->getMulti( array_keys( $map ), BagOStuff
::READ_LATEST
)
314 $this->cache
->getMulti( array_keys( $map ) )
319 * @covers BagOStuff::get
320 * @covers BagOStuff::getMulti
321 * @covers BagOStuff::merge
322 * @covers BagOStuff::delete
324 public function testSetSegmentable() {
325 $key = $this->cache
->makeKey( self
::TEST_KEY
);
327 $small = wfRandomString( 32 );
328 // 64 * 8 * 32768 = 16777216 bytes
329 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
331 $callback = function ( $cache, $key, $oldValue ) {
332 return $oldValue . '!';
335 foreach ( [ $tiny, $small, $big ] as $value ) {
336 $this->cache
->set( $key, $value, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
337 $this->assertEquals( $value, $this->cache
->get( $key ) );
338 $this->assertEquals( $value, $this->cache
->getMulti( [ $key ] )[$key] );
340 $this->assertTrue( $this->cache
->merge( $key, $callback, 5 ) );
341 $this->assertEquals( "$value!", $this->cache
->get( $key ) );
342 $this->assertEquals( "$value!", $this->cache
->getMulti( [ $key ] )[$key] );
344 $this->assertTrue( $this->cache
->deleteMulti( [ $key ] ) );
345 $this->assertFalse( $this->cache
->get( $key ) );
346 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
348 $this->cache
->set( $key, "@$value", 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
349 $this->assertEquals( "@$value", $this->cache
->get( $key ) );
350 $this->assertTrue( $this->cache
->delete( $key, BagOStuff
::WRITE_PRUNE_SEGMENTS
) );
351 $this->assertFalse( $this->cache
->get( $key ) );
352 $this->assertEquals( [], $this->cache
->getMulti( [ $key ] ) );
355 $this->cache
->set( $key, 666, 10, BagOStuff
::WRITE_ALLOW_SEGMENTS
);
357 $this->assertEquals( 666, $this->cache
->get( $key ) );
358 $this->assertEquals( 667, $this->cache
->incr( $key ) );
359 $this->assertEquals( 667, $this->cache
->get( $key ) );
361 $this->assertEquals( 664, $this->cache
->decr( $key, 3 ) );
362 $this->assertEquals( 664, $this->cache
->get( $key ) );
364 $this->assertTrue( $this->cache
->delete( $key ) );
365 $this->assertFalse( $this->cache
->get( $key ) );
369 * @covers BagOStuff::getScopedLock
371 public function testGetScopedLock() {
372 $key = $this->cache
->makeKey( self
::TEST_KEY
);
373 $value1 = $this->cache
->getScopedLock( $key, 0 );
374 $value2 = $this->cache
->getScopedLock( $key, 0 );
376 $this->assertType( ScopedCallback
::class, $value1, 'First call returned lock' );
377 $this->assertNull( $value2, 'Duplicate call returned no lock' );
381 $value3 = $this->cache
->getScopedLock( $key, 0 );
382 $this->assertType( ScopedCallback
::class, $value3, 'Lock returned callback after release' );
385 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
386 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
388 $this->assertType( ScopedCallback
::class, $value1, 'First reentrant call returned lock' );
389 $this->assertType( ScopedCallback
::class, $value1, 'Second reentrant call returned lock' );
393 * @covers BagOStuff::__construct
394 * @covers BagOStuff::trackDuplicateKeys
396 public function testReportDupes() {
397 $logger = $this->createMock( Psr\Log\NullLogger
::class );
398 $logger->expects( $this->once() )
399 ->method( 'warning' )
400 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
405 $cache = new HashBagOStuff( [
406 'reportDupes' => true,
407 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
410 $cache->get( 'foo' );
411 $cache->get( 'bar' );
412 $cache->get( 'foo' );
414 DeferredUpdates
::doUpdates();
418 * @covers BagOStuff::lock()
419 * @covers BagOStuff::unlock()
421 public function testLocking() {
423 $this->assertTrue( $this->cache
->lock( $key ) );
424 $this->assertFalse( $this->cache
->lock( $key ) );
425 $this->assertTrue( $this->cache
->unlock( $key ) );
428 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
429 $this->assertTrue( $this->cache
->lock( $key2, 5, 5, 'rclass' ) );
430 $this->assertTrue( $this->cache
->unlock( $key2 ) );
431 $this->assertTrue( $this->cache
->unlock( $key2 ) );
434 public function tearDown() {
435 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) );
436 $this->cache
->delete( $this->cache
->makeKey( self
::TEST_KEY
) . ':lock' );