Merge "Simplify Block::getBy and Block::getByName"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4
5 /**
6 * @author Matthias Mullie <mmullie@wikimedia.org>
7 * @group BagOStuff
8 */
9 class BagOStuffTest extends MediaWikiTestCase {
10 /** @var BagOStuff */
11 private $cache;
12
13 const TEST_KEY = 'test';
14
15 protected function setUp() {
16 parent::setUp();
17
18 // type defined through parameter
19 if ( $this->getCliArg( 'use-bagostuff' ) ) {
20 $name = $this->getCliArg( 'use-bagostuff' );
21
22 $this->cache = ObjectCache::newFromId( $name );
23 } else {
24 // no type defined - use simple hash
25 $this->cache = new HashBagOStuff;
26 }
27
28 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
29 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
30 }
31
32 /**
33 * @covers BagOStuff::makeGlobalKey
34 * @covers BagOStuff::makeKeyInternal
35 */
36 public function testMakeKey() {
37 $cache = ObjectCache::newFromId( 'hash' );
38
39 $localKey = $cache->makeKey( 'first', 'second', 'third' );
40 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
41
42 $this->assertStringMatchesFormat(
43 '%Sfirst%Ssecond%Sthird%S',
44 $localKey,
45 'Local key interpolates parameters'
46 );
47
48 $this->assertStringMatchesFormat(
49 'global%Sfirst%Ssecond%Sthird%S',
50 $globalKey,
51 'Global key interpolates parameters and contains global prefix'
52 );
53
54 $this->assertNotEquals(
55 $localKey,
56 $globalKey,
57 'Local key and global key with same parameters should not be equal'
58 );
59
60 $this->assertNotEquals(
61 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
62 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
63 );
64 }
65
66 /**
67 * @covers BagOStuff::merge
68 * @covers BagOStuff::mergeViaCas
69 */
70 public function testMerge() {
71 $key = $this->cache->makeKey( self::TEST_KEY );
72
73 $calls = 0;
74 $casRace = false; // emulate a race
75 $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
76 ++$calls;
77 if ( $casRace ) {
78 // Uses CAS instead?
79 $cache->set( $key, 'conflict', 5 );
80 }
81
82 return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
83 };
84
85 // merge on non-existing value
86 $merged = $this->cache->merge( $key, $callback, 5 );
87 $this->assertTrue( $merged );
88 $this->assertEquals( 'merged', $this->cache->get( $key ) );
89
90 // merge on existing value
91 $merged = $this->cache->merge( $key, $callback, 5 );
92 $this->assertTrue( $merged );
93 $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
94
95 $calls = 0;
96 $casRace = true;
97 $this->assertFalse(
98 $this->cache->merge( $key, $callback, 5, 1 ),
99 'Non-blocking merge (CAS)'
100 );
101 $this->assertEquals( 1, $calls );
102 }
103
104 /**
105 * @covers BagOStuff::merge
106 * @dataProvider provideTestMerge_fork
107 */
108 public function testMerge_fork( $exists, $childWins, $resCAS ) {
109 $key = $this->cache->makeKey( self::TEST_KEY );
110 $pCallback = function ( BagOStuff $cache, $key, $oldVal ) {
111 return ( $oldVal === false ) ? 'init-parent' : $oldVal . '-merged-parent';
112 };
113 $cCallback = function ( BagOStuff $cache, $key, $oldVal ) {
114 return ( $oldVal === false ) ? 'init-child' : $oldVal . '-merged-child';
115 };
116
117 if ( $exists ) {
118 $this->cache->set( $key, 'x', 5 );
119 }
120
121 /*
122 * Test concurrent merges by forking this process, if:
123 * - not manually called with --use-bagostuff
124 * - pcntl_fork is supported by the system
125 * - cache type will correctly support calls over forks
126 */
127 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
128 $fork &= function_exists( 'pcntl_fork' );
129 $fork &= !$this->cache instanceof HashBagOStuff;
130 $fork &= !$this->cache instanceof EmptyBagOStuff;
131 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
132 if ( $fork ) {
133 $pid = null;
134 // Function to start merge(), run another merge() midway through, then finish
135 $func = function ( $cache, $key, $cur ) use ( $pCallback, $cCallback, &$pid ) {
136 $pid = pcntl_fork();
137 if ( $pid == -1 ) {
138 return false;
139 } elseif ( $pid ) {
140 pcntl_wait( $status );
141
142 return $pCallback( $cache, $key, $cur );
143 } else {
144 $this->cache->merge( $key, $cCallback, 0, 1 );
145 // Bail out of the outer merge() in the child process since it does not
146 // need to attempt to write anything. Success is checked by the parent.
147 parent::tearDown(); // avoid phpunit notices
148 exit;
149 }
150 };
151
152 // attempt a merge - this should fail
153 $merged = $this->cache->merge( $key, $func, 0, 1 );
154
155 if ( $pid == -1 ) {
156 return; // can't fork, ignore this test...
157 }
158
159 // merge has failed because child process was merging (and we only attempted once)
160 $this->assertEquals( !$childWins, $merged );
161 $this->assertEquals( $this->cache->get( $key ), $resCAS );
162 } else {
163 $this->markTestSkipped( 'No pcntl methods available' );
164 }
165 }
166
167 function provideTestMerge_fork() {
168 return [
169 // (already exists, child wins CAS, result of CAS)
170 [ false, true, 'init-child' ],
171 [ true, true, 'x-merged-child' ]
172 ];
173 }
174
175 /**
176 * @covers BagOStuff::changeTTL
177 */
178 public function testChangeTTL() {
179 $key = $this->cache->makeKey( self::TEST_KEY );
180 $value = 'meow';
181
182 $this->cache->add( $key, $value, 5 );
183 $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
184 $this->assertEquals( $this->cache->get( $key ), $value );
185 $this->cache->delete( $key );
186 $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
187 }
188
189 /**
190 * @covers BagOStuff::add
191 */
192 public function testAdd() {
193 $key = $this->cache->makeKey( self::TEST_KEY );
194 $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
195 }
196
197 /**
198 * @covers BagOStuff::get
199 */
200 public function testGet() {
201 $value = [ 'this' => 'is', 'a' => 'test' ];
202
203 $key = $this->cache->makeKey( self::TEST_KEY );
204 $this->cache->add( $key, $value, 5 );
205 $this->assertEquals( $this->cache->get( $key ), $value );
206 }
207
208 /**
209 * @covers BagOStuff::get
210 * @covers BagOStuff::set
211 * @covers BagOStuff::getWithSetCallback
212 */
213 public function testGetWithSetCallback() {
214 $key = $this->cache->makeKey( self::TEST_KEY );
215 $value = $this->cache->getWithSetCallback(
216 $key,
217 30,
218 function () {
219 return 'hello kitty';
220 }
221 );
222
223 $this->assertEquals( 'hello kitty', $value );
224 $this->assertEquals( $value, $this->cache->get( $key ) );
225 }
226
227 /**
228 * @covers BagOStuff::incr
229 */
230 public function testIncr() {
231 $key = $this->cache->makeKey( self::TEST_KEY );
232 $this->cache->add( $key, 0, 5 );
233 $this->cache->incr( $key );
234 $expectedValue = 1;
235 $actualValue = $this->cache->get( $key );
236 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
237 }
238
239 /**
240 * @covers BagOStuff::incrWithInit
241 */
242 public function testIncrWithInit() {
243 $key = $this->cache->makeKey( self::TEST_KEY );
244 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
245 $this->assertEquals( 3, $val, "Correct init value" );
246
247 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
248 $this->assertEquals( 4, $val, "Correct init value" );
249 }
250
251 /**
252 * @covers BagOStuff::getMulti
253 */
254 public function testGetMulti() {
255 $value1 = [ 'this' => 'is', 'a' => 'test' ];
256 $value2 = [ 'this' => 'is', 'another' => 'test' ];
257 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
258 $value4 = [ 'another test where chars in key will be encoded' ];
259
260 $key1 = $this->cache->makeKey( 'test-1' );
261 $key2 = $this->cache->makeKey( 'test-2' );
262 // internally, MemcachedBagOStuffs will encode to will-%25-encode
263 $key3 = $this->cache->makeKey( 'will-%-encode' );
264 $key4 = $this->cache->makeKey(
265 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
266 );
267
268 // cleanup
269 $this->cache->delete( $key1 );
270 $this->cache->delete( $key2 );
271 $this->cache->delete( $key3 );
272 $this->cache->delete( $key4 );
273
274 $this->cache->add( $key1, $value1, 5 );
275 $this->cache->add( $key2, $value2, 5 );
276 $this->cache->add( $key3, $value3, 5 );
277 $this->cache->add( $key4, $value4, 5 );
278
279 $this->assertEquals(
280 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
281 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
282 );
283
284 // cleanup
285 $this->cache->delete( $key1 );
286 $this->cache->delete( $key2 );
287 $this->cache->delete( $key3 );
288 $this->cache->delete( $key4 );
289 }
290
291 /**
292 * @covers BagOStuff::setMulti
293 * @covers BagOStuff::deleteMulti
294 */
295 public function testSetDeleteMulti() {
296 $map = [
297 $this->cache->makeKey( 'test-1' ) => 'Siberian',
298 $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
299 $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
300 $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
301 $this->cache->makeKey( 'test-5' ) => 4,
302 $this->cache->makeKey( 'test-6' ) => 'ever'
303 ];
304
305 $this->cache->setMulti( $map, 5 );
306 $this->assertEquals(
307 $map,
308 $this->cache->getMulti( array_keys( $map ) )
309 );
310
311 $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ), 5 ) );
312
313 $this->assertEquals(
314 [],
315 $this->cache->getMulti( array_keys( $map ) )
316 );
317 }
318
319 /**
320 * @covers BagOStuff::getScopedLock
321 */
322 public function testGetScopedLock() {
323 $key = $this->cache->makeKey( self::TEST_KEY );
324 $value1 = $this->cache->getScopedLock( $key, 0 );
325 $value2 = $this->cache->getScopedLock( $key, 0 );
326
327 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
328 $this->assertNull( $value2, 'Duplicate call returned no lock' );
329
330 unset( $value1 );
331
332 $value3 = $this->cache->getScopedLock( $key, 0 );
333 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
334 unset( $value3 );
335
336 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
337 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
338
339 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
340 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
341 }
342
343 /**
344 * @covers BagOStuff::__construct
345 * @covers BagOStuff::trackDuplicateKeys
346 */
347 public function testReportDupes() {
348 $logger = $this->createMock( Psr\Log\NullLogger::class );
349 $logger->expects( $this->once() )
350 ->method( 'warning' )
351 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
352 'key' => 'foo',
353 'count' => 2,
354 ] );
355
356 $cache = new HashBagOStuff( [
357 'reportDupes' => true,
358 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
359 'logger' => $logger,
360 ] );
361 $cache->get( 'foo' );
362 $cache->get( 'bar' );
363 $cache->get( 'foo' );
364
365 DeferredUpdates::doUpdates();
366 }
367
368 /**
369 * @covers BagOStuff::lock()
370 * @covers BagOStuff::unlock()
371 */
372 public function testLocking() {
373 $key = 'test';
374 $this->assertTrue( $this->cache->lock( $key ) );
375 $this->assertFalse( $this->cache->lock( $key ) );
376 $this->assertTrue( $this->cache->unlock( $key ) );
377
378 $key2 = 'test2';
379 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
380 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
381 $this->assertTrue( $this->cache->unlock( $key2 ) );
382 $this->assertTrue( $this->cache->unlock( $key2 ) );
383 }
384 }