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