Merge "deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fname...
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @author Matthias Mullie <mmullie@wikimedia.org>
8 * @group BagOStuff
9 * @covers BagOStuff
10 */
11 class BagOStuffTest extends MediaWikiTestCase {
12 /** @var BagOStuff */
13 private $cache;
14
15 const TEST_KEY = 'test';
16
17 protected function setUp() {
18 parent::setUp();
19
20 // type defined through parameter
21 if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
22 global $wgObjectCaches;
23
24 $id = $this->getCliArg( 'use-bagostuff' );
25 $this->cache = ObjectCache::newFromParams( $wgObjectCaches[$id] );
26 } else {
27 // no type defined - use simple hash
28 $this->cache = new HashBagOStuff;
29 }
30
31 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
32 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
33 }
34
35 /**
36 * @covers MediumSpecificBagOStuff::makeGlobalKey
37 * @covers MediumSpecificBagOStuff::makeKeyInternal
38 */
39 public function testMakeKey() {
40 $cache = new HashBagOStuff();
41
42 $localKey = $cache->makeKey( 'first', 'second', 'third' );
43 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
44
45 $this->assertStringMatchesFormat(
46 '%Sfirst%Ssecond%Sthird%S',
47 $localKey,
48 'Local key interpolates parameters'
49 );
50
51 $this->assertStringMatchesFormat(
52 'global%Sfirst%Ssecond%Sthird%S',
53 $globalKey,
54 'Global key interpolates parameters and contains global prefix'
55 );
56
57 $this->assertNotEquals(
58 $localKey,
59 $globalKey,
60 'Local key and global key with same parameters should not be equal'
61 );
62
63 $this->assertNotEquals(
64 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
65 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
66 );
67 }
68
69 /**
70 * @covers MediumSpecificBagOStuff::merge
71 * @covers MediumSpecificBagOStuff::mergeViaCas
72 */
73 public function testMerge() {
74 $key = $this->cache->makeKey( self::TEST_KEY );
75
76 $calls = 0;
77 $casRace = false; // emulate a race
78 $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
79 ++$calls;
80 if ( $casRace ) {
81 // Uses CAS instead?
82 $cache->set( $key, 'conflict', 5 );
83 }
84
85 return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
86 };
87
88 // merge on non-existing value
89 $merged = $this->cache->merge( $key, $callback, 5 );
90 $this->assertTrue( $merged );
91 $this->assertEquals( 'merged', $this->cache->get( $key ) );
92
93 // merge on existing value
94 $merged = $this->cache->merge( $key, $callback, 5 );
95 $this->assertTrue( $merged );
96 $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
97
98 $calls = 0;
99 $casRace = true;
100 $this->assertFalse(
101 $this->cache->merge( $key, $callback, 5, 1 ),
102 'Non-blocking merge (CAS)'
103 );
104
105 if ( $this->cache instanceof MultiWriteBagOStuff ) {
106 $wrapper = TestingAccessWrapper::newFromObject( $this->cache );
107 $this->assertEquals( count( $wrapper->caches ), $calls );
108 } else {
109 $this->assertEquals( 1, $calls );
110 }
111 }
112
113 /**
114 * @covers MediumSpecificBagOStuff::changeTTL
115 */
116 public function testChangeTTLRenew() {
117 $now = microtime( true ); // need real time
118 $this->cache->setMockTime( $now );
119
120 $key = $this->cache->makeKey( self::TEST_KEY );
121 $value = 'meow';
122
123 $this->cache->add( $key, $value, 60 );
124 $this->assertEquals( $value, $this->cache->get( $key ) );
125 $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
126 $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
127 $this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
128 $this->assertEquals( $this->cache->get( $key ), $value );
129
130 $this->cache->delete( $key );
131 $this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
132 }
133
134 /**
135 * @covers MediumSpecificBagOStuff::changeTTL
136 */
137 public function testChangeTTLExpireRel() {
138 $now = microtime( true ); // need real time
139 $this->cache->setMockTime( $now );
140
141 $key = $this->cache->makeKey( self::TEST_KEY );
142 $value = 'meow';
143
144 $this->cache->add( $key, $value, 5 );
145 $this->assertTrue( $this->cache->changeTTL( $key, -3600 ) );
146 $this->assertFalse( $this->cache->get( $key ) );
147 }
148
149 /**
150 * @covers MediumSpecificBagOStuff::changeTTL
151 */
152 public function testChangeTTLExpireAbs() {
153 $now = microtime( true ); // need real time
154 $this->cache->setMockTime( $now );
155
156 $key = $this->cache->makeKey( self::TEST_KEY );
157 $value = 'meow';
158
159 $this->cache->add( $key, $value, 5 );
160 $this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );
161 $this->assertFalse( $this->cache->get( $key ) );
162 }
163
164 /**
165 * @covers MediumSpecificBagOStuff::changeTTLMulti
166 */
167 public function testChangeTTLMulti() {
168 $now = 1563892142;
169 $this->cache->setMockTime( $now );
170
171 $key1 = $this->cache->makeKey( 'test-key1' );
172 $key2 = $this->cache->makeKey( 'test-key2' );
173 $key3 = $this->cache->makeKey( 'test-key3' );
174 $key4 = $this->cache->makeKey( 'test-key4' );
175
176 // cleanup
177 $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
178
179 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
180 $this->assertFalse( $ok, "No keys found" );
181 $this->assertFalse( $this->cache->get( $key1 ) );
182 $this->assertFalse( $this->cache->get( $key2 ) );
183 $this->assertFalse( $this->cache->get( $key3 ) );
184
185 $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
186 $this->assertTrue( $ok, "setMulti() succeeded" );
187 $this->assertEquals(
188 3,
189 count( $this->cache->getMulti( [ $key1, $key2, $key3 ] ) ),
190 "setMulti() succeeded via getMulti() check"
191 );
192
193 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
194 $this->assertTrue( $ok, "TTL bumped for all keys" );
195 $this->assertEquals( 1, $this->cache->get( $key1 ) );
196 $this->assertEquals( 2, $this->cache->get( $key2 ) );
197 $this->assertEquals( 3, $this->cache->get( $key3 ) );
198
199 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
200 $this->assertFalse( $ok, "One key missing" );
201 $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
202
203 $now = microtime( true ); // real time
204 $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
205 $this->assertTrue( $ok, "setMulti() succeeded" );
206
207 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
208 $this->assertTrue( $ok, "Expiry set for all keys" );
209 $this->assertEquals( 1, $this->cache->get( $key1 ), "Key still live" );
210
211 $this->assertEquals( 2, $this->cache->incr( $key1 ) );
212 $this->assertEquals( 3, $this->cache->incr( $key2 ) );
213 $this->assertEquals( 4, $this->cache->incr( $key3 ) );
214
215 // cleanup
216 $this->cache->deleteMulti( [ $key1, $key2, $key3, $key4 ] );
217 }
218
219 /**
220 * @covers MediumSpecificBagOStuff::add
221 */
222 public function testAdd() {
223 $key = $this->cache->makeKey( self::TEST_KEY );
224 $this->assertFalse( $this->cache->get( $key ) );
225 $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
226 $this->assertFalse( $this->cache->add( $key, 'test', 5 ) );
227 }
228
229 /**
230 * @covers MediumSpecificBagOStuff::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 MediumSpecificBagOStuff::get
242 * @covers MediumSpecificBagOStuff::set
243 * @covers MediumSpecificBagOStuff::getWithSetCallback
244 */
245 public function testGetWithSetCallback() {
246 $now = 1563892142;
247 $cache = new HashBagOStuff( [] );
248 $cache->setMockTime( $now );
249 $key = $cache->makeKey( self::TEST_KEY );
250
251 $this->assertFalse( $cache->get( $key ), "No value" );
252
253 $value = $cache->getWithSetCallback(
254 $key,
255 30,
256 function ( &$ttl ) {
257 $ttl = 10;
258
259 return 'hello kitty';
260 }
261 );
262
263 $this->assertEquals( 'hello kitty', $value );
264 $this->assertEquals( $value, $cache->get( $key ), "Value set" );
265
266 $now += 11;
267
268 $this->assertFalse( $cache->get( $key ), "Value expired" );
269 }
270
271 /**
272 * @covers MediumSpecificBagOStuff::incr
273 */
274 public function testIncr() {
275 $key = $this->cache->makeKey( self::TEST_KEY );
276 $this->cache->add( $key, 0, 5 );
277 $this->cache->incr( $key );
278 $expectedValue = 1;
279 $actualValue = $this->cache->get( $key );
280 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
281 }
282
283 /**
284 * @covers MediumSpecificBagOStuff::incrWithInit
285 */
286 public function testIncrWithInit() {
287 $key = $this->cache->makeKey( self::TEST_KEY );
288 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
289 $this->assertEquals( 3, $val, "Correct init value" );
290
291 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
292 $this->assertEquals( 4, $val, "Correct init value" );
293 }
294
295 /**
296 * @covers MediumSpecificBagOStuff::getMulti
297 */
298 public function testGetMulti() {
299 $value1 = [ 'this' => 'is', 'a' => 'test' ];
300 $value2 = [ 'this' => 'is', 'another' => 'test' ];
301 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
302 $value4 = [ 'another test where chars in key will be encoded' ];
303
304 $key1 = $this->cache->makeKey( 'test-1' );
305 $key2 = $this->cache->makeKey( 'test-2' );
306 // internally, MemcachedBagOStuffs will encode to will-%25-encode
307 $key3 = $this->cache->makeKey( 'will-%-encode' );
308 $key4 = $this->cache->makeKey(
309 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
310 );
311
312 // cleanup
313 $this->cache->delete( $key1 );
314 $this->cache->delete( $key2 );
315 $this->cache->delete( $key3 );
316 $this->cache->delete( $key4 );
317
318 $this->cache->add( $key1, $value1, 5 );
319 $this->cache->add( $key2, $value2, 5 );
320 $this->cache->add( $key3, $value3, 5 );
321 $this->cache->add( $key4, $value4, 5 );
322
323 $this->assertEquals(
324 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
325 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
326 );
327
328 // cleanup
329 $this->cache->delete( $key1 );
330 $this->cache->delete( $key2 );
331 $this->cache->delete( $key3 );
332 $this->cache->delete( $key4 );
333 }
334
335 /**
336 * @covers MediumSpecificBagOStuff::setMulti
337 * @covers MediumSpecificBagOStuff::deleteMulti
338 */
339 public function testSetDeleteMulti() {
340 $map = [
341 $this->cache->makeKey( 'test-1' ) => 'Siberian',
342 $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
343 $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
344 $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
345 $this->cache->makeKey( 'test-5' ) => 4,
346 $this->cache->makeKey( 'test-6' ) => 'ever'
347 ];
348
349 $this->assertTrue( $this->cache->setMulti( $map ) );
350 $this->assertEquals(
351 $map,
352 $this->cache->getMulti( array_keys( $map ) )
353 );
354
355 $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ) ) );
356
357 $this->assertEquals(
358 [],
359 $this->cache->getMulti( array_keys( $map ), BagOStuff::READ_LATEST )
360 );
361 $this->assertEquals(
362 [],
363 $this->cache->getMulti( array_keys( $map ) )
364 );
365 }
366
367 /**
368 * @covers MediumSpecificBagOStuff::get
369 * @covers MediumSpecificBagOStuff::getMulti
370 * @covers MediumSpecificBagOStuff::merge
371 * @covers MediumSpecificBagOStuff::delete
372 */
373 public function testSetSegmentable() {
374 $key = $this->cache->makeKey( self::TEST_KEY );
375 $tiny = 418;
376 $small = wfRandomString( 32 );
377 // 64 * 8 * 32768 = 16777216 bytes
378 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
379
380 $callback = function ( $cache, $key, $oldValue ) {
381 return $oldValue . '!';
382 };
383
384 $cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
385 foreach ( $cases as $case => $value ) {
386 $this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
387 $this->assertEquals( $value, $this->cache->get( $key ), "get $case" );
388 $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key], "get $case" );
389
390 $this->assertTrue(
391 $this->cache->merge( $key, $callback, 5, 1, BagOStuff::WRITE_ALLOW_SEGMENTS ),
392 "merge $case"
393 );
394 $this->assertEquals(
395 "$value!",
396 $this->cache->get( $key ),
397 "merged $case"
398 );
399 $this->assertEquals(
400 "$value!",
401 $this->cache->getMulti( [ $key ] )[$key],
402 "merged $case"
403 );
404
405 $this->assertTrue( $this->cache->deleteMulti( [ $key ] ), "delete $case" );
406 $this->assertFalse( $this->cache->get( $key ), "deleted $case" );
407 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "deletd $case" );
408
409 $this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
410 $this->assertEquals( "@$value", $this->cache->get( $key ), "get $case" );
411 $this->assertTrue(
412 $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ),
413 "prune $case"
414 );
415 $this->assertFalse( $this->cache->get( $key ), "pruned $case" );
416 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ), "pruned $case" );
417 }
418
419 $this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
420
421 $this->assertEquals( 666, $this->cache->get( $key ) );
422 $this->assertEquals( 667, $this->cache->incr( $key ) );
423 $this->assertEquals( 667, $this->cache->get( $key ) );
424
425 $this->assertEquals( 664, $this->cache->decr( $key, 3 ) );
426 $this->assertEquals( 664, $this->cache->get( $key ) );
427
428 $this->assertTrue( $this->cache->delete( $key ) );
429 $this->assertFalse( $this->cache->get( $key ) );
430 }
431
432 /**
433 * @covers MediumSpecificBagOStuff::getScopedLock
434 */
435 public function testGetScopedLock() {
436 $key = $this->cache->makeKey( self::TEST_KEY );
437 $value1 = $this->cache->getScopedLock( $key, 0 );
438 $value2 = $this->cache->getScopedLock( $key, 0 );
439
440 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
441 $this->assertNull( $value2, 'Duplicate call returned no lock' );
442
443 unset( $value1 );
444
445 $value3 = $this->cache->getScopedLock( $key, 0 );
446 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
447 unset( $value3 );
448
449 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
450 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
451
452 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
453 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
454 }
455
456 /**
457 * @covers MediumSpecificBagOStuff::__construct
458 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
459 */
460 public function testReportDupes() {
461 $logger = $this->createMock( Psr\Log\NullLogger::class );
462 $logger->expects( $this->once() )
463 ->method( 'warning' )
464 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
465 'key' => 'foo',
466 'count' => 2,
467 ] );
468
469 $cache = new HashBagOStuff( [
470 'reportDupes' => true,
471 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
472 'logger' => $logger,
473 ] );
474 $cache->get( 'foo' );
475 $cache->get( 'bar' );
476 $cache->get( 'foo' );
477
478 DeferredUpdates::doUpdates();
479 }
480
481 /**
482 * @covers MediumSpecificBagOStuff::lock()
483 * @covers MediumSpecificBagOStuff::unlock()
484 */
485 public function testLocking() {
486 $key = 'test';
487 $this->assertTrue( $this->cache->lock( $key ) );
488 $this->assertFalse( $this->cache->lock( $key ) );
489 $this->assertTrue( $this->cache->unlock( $key ) );
490
491 $key2 = 'test2';
492 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
493 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
494 $this->assertTrue( $this->cache->unlock( $key2 ) );
495 $this->assertTrue( $this->cache->unlock( $key2 ) );
496 }
497
498 public function tearDown() {
499 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
500 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
501
502 parent::tearDown();
503 }
504 }