3 * @author Matthias Mullie <mmullie@wikimedia.org>
6 class BagOStuffTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff' );
17 $this->cache
= ObjectCache
::newFromId( $name );
19 // no type defined - use simple hash
20 $this->cache
= new HashBagOStuff
;
23 $this->cache
->delete( wfMemcKey( 'test' ) );
27 * @covers BagOStuff::makeGlobalKey
28 * @covers BagOStuff::makeKeyInternal
30 public function testMakeKey() {
31 $cache = ObjectCache
::newFromId( 'hash' );
33 $localKey = $cache->makeKey( 'first', 'second', 'third' );
34 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
36 $this->assertStringMatchesFormat(
37 '%Sfirst%Ssecond%Sthird%S',
39 'Local key interpolates parameters'
42 $this->assertStringMatchesFormat(
43 'global%Sfirst%Ssecond%Sthird%S',
45 'Global key interpolates parameters and contains global prefix'
48 $this->assertNotEquals(
51 'Local key and global key with same parameters should not be equal'
54 $this->assertNotEquals(
55 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
56 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
61 * @covers BagOStuff::merge
62 * @covers BagOStuff::mergeViaLock
64 public function testMerge() {
65 $key = wfMemcKey( 'test' );
70 * Callback method: append "merged" to whatever is in cache.
72 * @param BagOStuff $cache
74 * @param int $existingValue
78 $callback = function ( BagOStuff
$cache, $key, $existingValue ) use ( &$usleep ) {
79 // let's pretend this is an expensive callback to test concurrent merge attempts
82 if ( $existingValue === false ) {
86 return $existingValue . 'merged';
89 // merge on non-existing value
90 $merged = $this->cache
->merge( $key, $callback, 0 );
91 $this->assertTrue( $merged );
92 $this->assertEquals( $this->cache
->get( $key ), 'merged' );
94 // merge on existing value
95 $merged = $this->cache
->merge( $key, $callback, 0 );
96 $this->assertTrue( $merged );
97 $this->assertEquals( $this->cache
->get( $key ), 'mergedmerged' );
100 * Test concurrent merges by forking this process, if:
101 * - not manually called with --use-bagostuff
102 * - pcntl_fork is supported by the system
103 * - cache type will correctly support calls over forks
105 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
106 $fork &= function_exists( 'pcntl_fork' );
107 $fork &= !$this->cache
instanceof HashBagOStuff
;
108 $fork &= !$this->cache
instanceof EmptyBagOStuff
;
109 $fork &= !$this->cache
instanceof MultiWriteBagOStuff
;
111 // callback should take awhile now so that we can test concurrent merge attempts
114 // can't fork, ignore this test...
116 // wait a little, making sure that the child process is calling merge
119 // attempt a merge - this should fail
120 $merged = $this->cache
->merge( $key, $callback, 0, 1 );
122 // merge has failed because child process was merging (and we only attempted once)
123 $this->assertFalse( $merged );
125 // make sure the child's merge is completed and verify
127 $this->assertEquals( $this->cache
->get( $key ), 'mergedmergedmerged' );
129 $this->cache
->merge( $key, $callback, 0, 1 );
131 // Note: I'm not even going to check if the merge worked, I'll
132 // compare values in the parent process to test if this merge worked.
133 // I'm just going to exit this child process, since I don't want the
134 // child to output any test results (would be rather confusing to
135 // have test output twice)
142 * @covers BagOStuff::changeTTL
144 public function testChangeTTL() {
145 $key = wfMemcKey( 'test' );
148 $this->cache
->add( $key, $value );
149 $this->assertTrue( $this->cache
->changeTTL( $key, 5 ) );
150 $this->assertEquals( $this->cache
->get( $key ), $value );
151 $this->cache
->delete( $key );
152 $this->assertFalse( $this->cache
->changeTTL( $key, 5 ) );
156 * @covers BagOStuff::add
158 public function testAdd() {
159 $key = wfMemcKey( 'test' );
160 $this->assertTrue( $this->cache
->add( $key, 'test' ) );
163 public function testGet() {
164 $value = [ 'this' => 'is', 'a' => 'test' ];
166 $key = wfMemcKey( 'test' );
167 $this->cache
->add( $key, $value );
168 $this->assertEquals( $this->cache
->get( $key ), $value );
172 * @covers BagOStuff::getWithSetCallback
174 public function testGetWithSetCallback() {
175 $key = wfMemcKey( 'test' );
176 $value = $this->cache
->getWithSetCallback(
180 return 'hello kitty';
184 $this->assertEquals( 'hello kitty', $value );
185 $this->assertEquals( $value, $this->cache
->get( $key ) );
189 * @covers BagOStuff::incr
191 public function testIncr() {
192 $key = wfMemcKey( 'test' );
193 $this->cache
->add( $key, 0 );
194 $this->cache
->incr( $key );
196 $actualValue = $this->cache
->get( $key );
197 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
201 * @covers BagOStuff::incrWithInit
203 public function testIncrWithInit() {
204 $key = wfMemcKey( 'test' );
205 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
206 $this->assertEquals( 3, $val, "Correct init value" );
208 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
209 $this->assertEquals( 4, $val, "Correct init value" );
213 * @covers BagOStuff::getMulti
215 public function testGetMulti() {
216 $value1 = [ 'this' => 'is', 'a' => 'test' ];
217 $value2 = [ 'this' => 'is', 'another' => 'test' ];
218 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
219 $value4 = [ 'another test where chars in key will be encoded' ];
221 $key1 = wfMemcKey( 'test1' );
222 $key2 = wfMemcKey( 'test2' );
223 // internally, MemcachedBagOStuffs will encode to will-%25-encode
224 $key3 = wfMemcKey( 'will-%-encode' );
226 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
229 $this->cache
->add( $key1, $value1 );
230 $this->cache
->add( $key2, $value2 );
231 $this->cache
->add( $key3, $value3 );
232 $this->cache
->add( $key4, $value4 );
235 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
236 $this->cache
->getMulti( [ $key1, $key2, $key3, $key4 ] )
240 $this->cache
->delete( $key1 );
241 $this->cache
->delete( $key2 );
242 $this->cache
->delete( $key3 );
243 $this->cache
->delete( $key4 );
247 * @covers BagOStuff::getScopedLock
249 public function testGetScopedLock() {
250 $key = wfMemcKey( 'test' );
251 $value1 = $this->cache
->getScopedLock( $key, 0 );
252 $value2 = $this->cache
->getScopedLock( $key, 0 );
254 $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
255 $this->assertNull( $value2, 'Duplicate call returned no lock' );
259 $value3 = $this->cache
->getScopedLock( $key, 0 );
260 $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
263 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
264 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
266 $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
267 $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );
271 * @covers BagOStuff::__construct
272 * @covers BagOStuff::trackDuplicateKeys
274 public function testReportDupes() {
275 $logger = $this->getMock( 'Psr\Log\NullLogger' );
276 $logger->expects( $this->once() )
277 ->method( 'warning' )
278 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
283 $cache = new HashBagOStuff( [
284 'reportDupes' => true,
285 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
288 $cache->get( 'foo' );
289 $cache->get( 'bar' );
290 $cache->get( 'foo' );
292 DeferredUpdates
::doUpdates();