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', array( 'a', 'bc:', 'de' ) ),
56 $cache->makeKeyInternal( 'prefix', array( '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::add
144 public function testAdd() {
145 $key = wfMemcKey( 'test' );
146 $this->assertTrue( $this->cache
->add( $key, 'test' ) );
149 public function testGet() {
150 $value = array( 'this' => 'is', 'a' => 'test' );
152 $key = wfMemcKey( 'test' );
153 $this->cache
->add( $key, $value );
154 $this->assertEquals( $this->cache
->get( $key ), $value );
158 * @covers BagOStuff::getWithSetCallback
160 public function testGetWithSetCallback() {
161 $key = wfMemcKey( 'test' );
162 $value = $this->cache
->getWithSetCallback(
166 return 'hello kitty';
170 $this->assertEquals( 'hello kitty', $value );
171 $this->assertEquals( $value, $this->cache
->get( $key ) );
175 * @covers BagOStuff::incr
177 public function testIncr() {
178 $key = wfMemcKey( 'test' );
179 $this->cache
->add( $key, 0 );
180 $this->cache
->incr( $key );
182 $actualValue = $this->cache
->get( $key );
183 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
187 * @covers BagOStuff::incrWithInit
189 public function testIncrWithInit() {
190 $key = wfMemcKey( 'test' );
191 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
192 $this->assertEquals( 3, $val, "Correct init value" );
194 $val = $this->cache
->incrWithInit( $key, 0, 1, 3 );
195 $this->assertEquals( 4, $val, "Correct init value" );
199 * @covers BagOStuff::getMulti
201 public function testGetMulti() {
202 $value1 = array( 'this' => 'is', 'a' => 'test' );
203 $value2 = array( 'this' => 'is', 'another' => 'test' );
204 $value3 = array( 'testing a key that may be encoded when sent to cache backend' );
205 $value4 = array( 'another test where chars in key will be encoded' );
207 $key1 = wfMemcKey( 'test1' );
208 $key2 = wfMemcKey( 'test2' );
209 // internally, MemcachedBagOStuffs will encode to will-%25-encode
210 $key3 = wfMemcKey( 'will-%-encode' );
212 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
215 $this->cache
->add( $key1, $value1 );
216 $this->cache
->add( $key2, $value2 );
217 $this->cache
->add( $key3, $value3 );
218 $this->cache
->add( $key4, $value4 );
221 array( $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ),
222 $this->cache
->getMulti( array( $key1, $key2, $key3, $key4 ) )
226 $this->cache
->delete( $key1 );
227 $this->cache
->delete( $key2 );
228 $this->cache
->delete( $key3 );
229 $this->cache
->delete( $key4 );
233 * @covers BagOStuff::getScopedLock
235 public function testGetScopedLock() {
236 $key = wfMemcKey( 'test' );
237 $value1 = $this->cache
->getScopedLock( $key, 0 );
238 $value2 = $this->cache
->getScopedLock( $key, 0 );
240 $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
241 $this->assertNull( $value2, 'Duplicate call returned no lock' );
245 $value3 = $this->cache
->getScopedLock( $key, 0 );
246 $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
249 $value1 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
250 $value2 = $this->cache
->getScopedLock( $key, 0, 5, 'reentry' );
252 $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
253 $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );