Merge "Fix WatchedItemStore last-seen stashing logic"
[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::mergeViaLock
69 * @covers BagOStuff::mergeViaCas
70 */
71 public function testMerge() {
72 $key = $this->cache->makeKey( self::TEST_KEY );
73 $locks = false;
74 $checkLockingCallback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$locks ) {
75 $locks = $cache->get( "$key:lock" );
76
77 return false;
78 };
79
80 $this->cache->merge( $key, $checkLockingCallback, 5 );
81 $this->assertFalse( $this->cache->get( $key ) );
82
83 $calls = 0;
84 $casRace = false; // emulate a race
85 $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
86 ++$calls;
87 if ( $casRace ) {
88 // Uses CAS instead?
89 $cache->set( $key, 'conflict', 5 );
90 }
91
92 return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
93 };
94
95 // merge on non-existing value
96 $merged = $this->cache->merge( $key, $callback, 5 );
97 $this->assertTrue( $merged );
98 $this->assertEquals( 'merged', $this->cache->get( $key ) );
99
100 // merge on existing value
101 $merged = $this->cache->merge( $key, $callback, 5 );
102 $this->assertTrue( $merged );
103 $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
104
105 $calls = 0;
106 if ( $locks ) {
107 // merge were something else already was merging (e.g. had the lock)
108 $this->cache->lock( $key );
109 $this->assertFalse(
110 $this->cache->merge( $key, $callback, 5, 1 ),
111 'Non-blocking merge (locking)'
112 );
113 $this->cache->unlock( $key );
114 $this->assertEquals( 0, $calls );
115 } else {
116 $casRace = true;
117 $this->assertFalse(
118 $this->cache->merge( $key, $callback, 5, 1 ),
119 'Non-blocking merge (CAS)'
120 );
121 $this->assertEquals( 1, $calls );
122 }
123 }
124
125 /**
126 * @covers BagOStuff::merge
127 * @covers BagOStuff::mergeViaLock
128 * @dataProvider provideTestMerge_fork
129 */
130 public function testMerge_fork( $exists, $winsLocking, $resLocking, $resCAS ) {
131 $key = $this->cache->makeKey( self::TEST_KEY );
132 $pCallback = function ( BagOStuff $cache, $key, $oldVal ) {
133 return ( $oldVal === false ) ? 'init-parent' : $oldVal . '-merged-parent';
134 };
135 $cCallback = function ( BagOStuff $cache, $key, $oldVal ) {
136 return ( $oldVal === false ) ? 'init-child' : $oldVal . '-merged-child';
137 };
138
139 if ( $exists ) {
140 $this->cache->set( $key, 'x', 5 );
141 }
142
143 /*
144 * Test concurrent merges by forking this process, if:
145 * - not manually called with --use-bagostuff
146 * - pcntl_fork is supported by the system
147 * - cache type will correctly support calls over forks
148 */
149 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
150 $fork &= function_exists( 'pcntl_fork' );
151 $fork &= !$this->cache instanceof HashBagOStuff;
152 $fork &= !$this->cache instanceof EmptyBagOStuff;
153 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
154 if ( $fork ) {
155 $pid = null;
156 $locked = false;
157 // Function to start merge(), run another merge() midway through, then finish
158 $func = function ( BagOStuff $cache, $key, $cur )
159 use ( $pCallback, $cCallback, &$pid, &$locked )
160 {
161 $pid = pcntl_fork();
162 if ( $pid == -1 ) {
163 return false;
164 } elseif ( $pid ) {
165 $locked = $cache->get( "$key:lock" ); // parent has lock?
166 pcntl_wait( $status );
167
168 return $pCallback( $cache, $key, $cur );
169 } else {
170 $this->cache->merge( $key, $cCallback, 0, 1 );
171 // Bail out of the outer merge() in the child process since it does not
172 // need to attempt to write anything. Success is checked by the parent.
173 parent::tearDown(); // avoid phpunit notices
174 exit;
175 }
176 };
177
178 // attempt a merge - this should fail
179 $merged = $this->cache->merge( $key, $func, 0, 1 );
180
181 if ( $pid == -1 ) {
182 return; // can't fork, ignore this test...
183 }
184
185 if ( $locked ) {
186 // merge succeed since child was locked out
187 $this->assertEquals( $winsLocking, $merged );
188 $this->assertEquals( $this->cache->get( $key ), $resLocking );
189 } else {
190 // merge has failed because child process was merging (and we only attempted once)
191 $this->assertEquals( !$winsLocking, $merged );
192 $this->assertEquals( $this->cache->get( $key ), $resCAS );
193 }
194 } else {
195 $this->markTestSkipped( 'No pcntl methods available' );
196 }
197 }
198
199 function provideTestMerge_fork() {
200 return [
201 // (already exists, parent wins if locking, result if locking, result if CAS)
202 [ false, true, 'init-parent', 'init-child' ],
203 [ true, true, 'x-merged-parent', 'x-merged-child' ]
204 ];
205 }
206
207 /**
208 * @covers BagOStuff::changeTTL
209 */
210 public function testChangeTTL() {
211 $key = $this->cache->makeKey( self::TEST_KEY );
212 $value = 'meow';
213
214 $this->cache->add( $key, $value, 5 );
215 $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
216 $this->assertEquals( $this->cache->get( $key ), $value );
217 $this->cache->delete( $key );
218 $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
219 }
220
221 /**
222 * @covers BagOStuff::add
223 */
224 public function testAdd() {
225 $key = $this->cache->makeKey( self::TEST_KEY );
226 $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
227 }
228
229 /**
230 * @covers BagOStuff::get
231 */
232 public function testGet() {
233 $value = [ 'this' => 'is', 'a' => 'test' ];
234
235 $key = $this->cache->makeKey( self::TEST_KEY );
236 $this->cache->add( $key, $value, 5 );
237 $this->assertEquals( $this->cache->get( $key ), $value );
238 }
239
240 /**
241 * @covers BagOStuff::get
242 * @covers BagOStuff::set
243 * @covers BagOStuff::getWithSetCallback
244 */
245 public function testGetWithSetCallback() {
246 $key = $this->cache->makeKey( self::TEST_KEY );
247 $value = $this->cache->getWithSetCallback(
248 $key,
249 30,
250 function () {
251 return 'hello kitty';
252 }
253 );
254
255 $this->assertEquals( 'hello kitty', $value );
256 $this->assertEquals( $value, $this->cache->get( $key ) );
257 }
258
259 /**
260 * @covers BagOStuff::incr
261 */
262 public function testIncr() {
263 $key = $this->cache->makeKey( self::TEST_KEY );
264 $this->cache->add( $key, 0, 5 );
265 $this->cache->incr( $key );
266 $expectedValue = 1;
267 $actualValue = $this->cache->get( $key );
268 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
269 }
270
271 /**
272 * @covers BagOStuff::incrWithInit
273 */
274 public function testIncrWithInit() {
275 $key = $this->cache->makeKey( self::TEST_KEY );
276 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
277 $this->assertEquals( 3, $val, "Correct init value" );
278
279 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
280 $this->assertEquals( 4, $val, "Correct init value" );
281 }
282
283 /**
284 * @covers BagOStuff::getMulti
285 */
286 public function testGetMulti() {
287 $value1 = [ 'this' => 'is', 'a' => 'test' ];
288 $value2 = [ 'this' => 'is', 'another' => 'test' ];
289 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
290 $value4 = [ 'another test where chars in key will be encoded' ];
291
292 $key1 = $this->cache->makeKey( 'test-1' );
293 $key2 = $this->cache->makeKey( 'test-2' );
294 // internally, MemcachedBagOStuffs will encode to will-%25-encode
295 $key3 = $this->cache->makeKey( 'will-%-encode' );
296 $key4 = $this->cache->makeKey(
297 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
298 );
299
300 // cleanup
301 $this->cache->delete( $key1 );
302 $this->cache->delete( $key2 );
303 $this->cache->delete( $key3 );
304 $this->cache->delete( $key4 );
305
306 $this->cache->add( $key1, $value1, 5 );
307 $this->cache->add( $key2, $value2, 5 );
308 $this->cache->add( $key3, $value3, 5 );
309 $this->cache->add( $key4, $value4, 5 );
310
311 $this->assertEquals(
312 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
313 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
314 );
315
316 // cleanup
317 $this->cache->delete( $key1 );
318 $this->cache->delete( $key2 );
319 $this->cache->delete( $key3 );
320 $this->cache->delete( $key4 );
321 }
322
323 /**
324 * @covers BagOStuff::setMulti
325 * @covers BagOStuff::deleteMulti
326 */
327 public function testSetDeleteMulti() {
328 $map = [
329 $this->cache->makeKey( 'test-1' ) => 'Siberian',
330 $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
331 $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
332 $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
333 $this->cache->makeKey( 'test-5' ) => 4,
334 $this->cache->makeKey( 'test-6' ) => 'ever'
335 ];
336
337 $this->cache->setMulti( $map, 5 );
338 $this->assertEquals(
339 $map,
340 $this->cache->getMulti( array_keys( $map ) )
341 );
342
343 $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ), 5 ) );
344
345 $this->assertEquals(
346 [],
347 $this->cache->getMulti( array_keys( $map ) )
348 );
349 }
350
351 /**
352 * @covers BagOStuff::getScopedLock
353 */
354 public function testGetScopedLock() {
355 $key = $this->cache->makeKey( self::TEST_KEY );
356 $value1 = $this->cache->getScopedLock( $key, 0 );
357 $value2 = $this->cache->getScopedLock( $key, 0 );
358
359 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
360 $this->assertNull( $value2, 'Duplicate call returned no lock' );
361
362 unset( $value1 );
363
364 $value3 = $this->cache->getScopedLock( $key, 0 );
365 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
366 unset( $value3 );
367
368 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
369 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
370
371 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
372 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
373 }
374
375 /**
376 * @covers BagOStuff::__construct
377 * @covers BagOStuff::trackDuplicateKeys
378 */
379 public function testReportDupes() {
380 $logger = $this->createMock( Psr\Log\NullLogger::class );
381 $logger->expects( $this->once() )
382 ->method( 'warning' )
383 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
384 'key' => 'foo',
385 'count' => 2,
386 ] );
387
388 $cache = new HashBagOStuff( [
389 'reportDupes' => true,
390 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
391 'logger' => $logger,
392 ] );
393 $cache->get( 'foo' );
394 $cache->get( 'bar' );
395 $cache->get( 'foo' );
396
397 DeferredUpdates::doUpdates();
398 }
399
400 /**
401 * @covers BagOStuff::lock()
402 * @covers BagOStuff::unlock()
403 */
404 public function testLocking() {
405 $key = 'test';
406 $this->assertTrue( $this->cache->lock( $key ) );
407 $this->assertFalse( $this->cache->lock( $key ) );
408 $this->assertTrue( $this->cache->unlock( $key ) );
409
410 $key2 = 'test2';
411 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
412 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
413 $this->assertTrue( $this->cache->unlock( $key2 ) );
414 $this->assertTrue( $this->cache->unlock( $key2 ) );
415 }
416 }