4a56fc580fc1c0c8247140a21788a09c682a1085
[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 testChangeTTL() {
116 $now = 1563892142;
117 $this->cache->setMockTime( $now );
118
119 $key = $this->cache->makeKey( self::TEST_KEY );
120 $value = 'meow';
121
122 $this->cache->add( $key, $value, 5 );
123 $this->assertEquals( $value, $this->cache->get( $key ) );
124 $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
125 $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
126 $this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
127 $this->assertEquals( $this->cache->get( $key ), $value );
128 $this->cache->delete( $key );
129 $this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
130
131 $this->cache->add( $key, $value, 5 );
132 $this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );
133 $this->assertFalse( $this->cache->get( $key ) );
134 }
135
136 /**
137 * @covers MediumSpecificBagOStuff::changeTTLMulti
138 */
139 public function testChangeTTLMulti() {
140 $now = 1563892142;
141 $this->cache->setMockTime( $now );
142
143 $key1 = $this->cache->makeKey( 'test-key1' );
144 $key2 = $this->cache->makeKey( 'test-key2' );
145 $key3 = $this->cache->makeKey( 'test-key3' );
146 $key4 = $this->cache->makeKey( 'test-key4' );
147
148 // cleanup
149 $this->cache->delete( $key1 );
150 $this->cache->delete( $key2 );
151 $this->cache->delete( $key3 );
152 $this->cache->delete( $key4 );
153
154 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
155 $this->assertFalse( $ok, "No keys found" );
156 $this->assertFalse( $this->cache->get( $key1 ) );
157 $this->assertFalse( $this->cache->get( $key2 ) );
158 $this->assertFalse( $this->cache->get( $key3 ) );
159
160 $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
161
162 $this->assertTrue( $ok, "setMulti() succeeded" );
163 $this->assertEquals(
164 3,
165 count( $this->cache->getMulti( [ $key1, $key2, $key3 ] ) ),
166 "setMulti() succeeded via getMulti() check"
167 );
168
169 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
170 $this->assertTrue( $ok, "TTL bumped for all keys" );
171 $this->assertEquals( 1, $this->cache->get( $key1 ) );
172 $this->assertEquals( 2, $this->cache->get( $key2 ) );
173 $this->assertEquals( 3, $this->cache->get( $key3 ) );
174
175 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], $now + 86400 );
176 $this->assertTrue( $ok, "Expiry set for all keys" );
177
178 $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
179 $this->assertFalse( $ok, "One key missing" );
180
181 $this->assertEquals( 2, $this->cache->incr( $key1 ) );
182 $this->assertEquals( 3, $this->cache->incr( $key2 ) );
183 $this->assertEquals( 4, $this->cache->incr( $key3 ) );
184
185 // cleanup
186 $this->cache->delete( $key1 );
187 $this->cache->delete( $key2 );
188 $this->cache->delete( $key3 );
189 $this->cache->delete( $key4 );
190 }
191
192 /**
193 * @covers MediumSpecificBagOStuff::add
194 */
195 public function testAdd() {
196 $key = $this->cache->makeKey( self::TEST_KEY );
197 $this->assertFalse( $this->cache->get( $key ) );
198 $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
199 $this->assertFalse( $this->cache->add( $key, 'test', 5 ) );
200 }
201
202 /**
203 * @covers MediumSpecificBagOStuff::get
204 */
205 public function testGet() {
206 $value = [ 'this' => 'is', 'a' => 'test' ];
207
208 $key = $this->cache->makeKey( self::TEST_KEY );
209 $this->cache->add( $key, $value, 5 );
210 $this->assertEquals( $this->cache->get( $key ), $value );
211 }
212
213 /**
214 * @covers MediumSpecificBagOStuff::get
215 * @covers MediumSpecificBagOStuff::set
216 * @covers MediumSpecificBagOStuff::getWithSetCallback
217 */
218 public function testGetWithSetCallback() {
219 $now = 1563892142;
220 $this->cache->setMockTime( $now );
221 $key = $this->cache->makeKey( self::TEST_KEY );
222
223 $this->assertFalse( $this->cache->get( $key ), "No value" );
224
225 $value = $this->cache->getWithSetCallback(
226 $key,
227 30,
228 function ( &$ttl ) {
229 $ttl = 10;
230
231 return 'hello kitty';
232 }
233 );
234
235 $this->assertEquals( 'hello kitty', $value );
236 $this->assertEquals( $value, $this->cache->get( $key ), "Value set" );
237
238 $now += 11;
239
240 $this->assertFalse( $this->cache->get( $key ), "Value expired" );
241 }
242
243 /**
244 * @covers MediumSpecificBagOStuff::incr
245 */
246 public function testIncr() {
247 $key = $this->cache->makeKey( self::TEST_KEY );
248 $this->cache->add( $key, 0, 5 );
249 $this->cache->incr( $key );
250 $expectedValue = 1;
251 $actualValue = $this->cache->get( $key );
252 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
253 }
254
255 /**
256 * @covers MediumSpecificBagOStuff::incrWithInit
257 */
258 public function testIncrWithInit() {
259 $key = $this->cache->makeKey( self::TEST_KEY );
260 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
261 $this->assertEquals( 3, $val, "Correct init value" );
262
263 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
264 $this->assertEquals( 4, $val, "Correct init value" );
265 }
266
267 /**
268 * @covers MediumSpecificBagOStuff::getMulti
269 */
270 public function testGetMulti() {
271 $value1 = [ 'this' => 'is', 'a' => 'test' ];
272 $value2 = [ 'this' => 'is', 'another' => 'test' ];
273 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
274 $value4 = [ 'another test where chars in key will be encoded' ];
275
276 $key1 = $this->cache->makeKey( 'test-1' );
277 $key2 = $this->cache->makeKey( 'test-2' );
278 // internally, MemcachedBagOStuffs will encode to will-%25-encode
279 $key3 = $this->cache->makeKey( 'will-%-encode' );
280 $key4 = $this->cache->makeKey(
281 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
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 $this->cache->add( $key1, $value1, 5 );
291 $this->cache->add( $key2, $value2, 5 );
292 $this->cache->add( $key3, $value3, 5 );
293 $this->cache->add( $key4, $value4, 5 );
294
295 $this->assertEquals(
296 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
297 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
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
307 /**
308 * @covers MediumSpecificBagOStuff::setMulti
309 * @covers MediumSpecificBagOStuff::deleteMulti
310 */
311 public function testSetDeleteMulti() {
312 $map = [
313 $this->cache->makeKey( 'test-1' ) => 'Siberian',
314 $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
315 $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
316 $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
317 $this->cache->makeKey( 'test-5' ) => 4,
318 $this->cache->makeKey( 'test-6' ) => 'ever'
319 ];
320
321 $this->assertTrue( $this->cache->setMulti( $map ) );
322 $this->assertEquals(
323 $map,
324 $this->cache->getMulti( array_keys( $map ) )
325 );
326
327 $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ) ) );
328
329 $this->assertEquals(
330 [],
331 $this->cache->getMulti( array_keys( $map ), BagOStuff::READ_LATEST )
332 );
333 $this->assertEquals(
334 [],
335 $this->cache->getMulti( array_keys( $map ) )
336 );
337 }
338
339 /**
340 * @covers MediumSpecificBagOStuff::get
341 * @covers MediumSpecificBagOStuff::getMulti
342 * @covers MediumSpecificBagOStuff::merge
343 * @covers MediumSpecificBagOStuff::delete
344 */
345 public function testSetSegmentable() {
346 $key = $this->cache->makeKey( self::TEST_KEY );
347 $tiny = 418;
348 $small = wfRandomString( 32 );
349 // 64 * 8 * 32768 = 16777216 bytes
350 $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
351
352 $callback = function ( $cache, $key, $oldValue ) {
353 return $oldValue . '!';
354 };
355
356 foreach ( [ $tiny, $small, $big ] as $value ) {
357 $this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
358 $this->assertEquals( $value, $this->cache->get( $key ) );
359 $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key] );
360
361 $this->assertTrue( $this->cache->merge( $key, $callback, 5 ) );
362 $this->assertEquals( "$value!", $this->cache->get( $key ) );
363 $this->assertEquals( "$value!", $this->cache->getMulti( [ $key ] )[$key] );
364
365 $this->assertTrue( $this->cache->deleteMulti( [ $key ] ) );
366 $this->assertFalse( $this->cache->get( $key ) );
367 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
368
369 $this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
370 $this->assertEquals( "@$value", $this->cache->get( $key ) );
371 $this->assertTrue( $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ) );
372 $this->assertFalse( $this->cache->get( $key ) );
373 $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
374 }
375
376 $this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
377
378 $this->assertEquals( 666, $this->cache->get( $key ) );
379 $this->assertEquals( 667, $this->cache->incr( $key ) );
380 $this->assertEquals( 667, $this->cache->get( $key ) );
381
382 $this->assertEquals( 664, $this->cache->decr( $key, 3 ) );
383 $this->assertEquals( 664, $this->cache->get( $key ) );
384
385 $this->assertTrue( $this->cache->delete( $key ) );
386 $this->assertFalse( $this->cache->get( $key ) );
387 }
388
389 /**
390 * @covers MediumSpecificBagOStuff::getScopedLock
391 */
392 public function testGetScopedLock() {
393 $key = $this->cache->makeKey( self::TEST_KEY );
394 $value1 = $this->cache->getScopedLock( $key, 0 );
395 $value2 = $this->cache->getScopedLock( $key, 0 );
396
397 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
398 $this->assertNull( $value2, 'Duplicate call returned no lock' );
399
400 unset( $value1 );
401
402 $value3 = $this->cache->getScopedLock( $key, 0 );
403 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
404 unset( $value3 );
405
406 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
407 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
408
409 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
410 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
411 }
412
413 /**
414 * @covers MediumSpecificBagOStuff::__construct
415 * @covers MediumSpecificBagOStuff::trackDuplicateKeys
416 */
417 public function testReportDupes() {
418 $logger = $this->createMock( Psr\Log\NullLogger::class );
419 $logger->expects( $this->once() )
420 ->method( 'warning' )
421 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
422 'key' => 'foo',
423 'count' => 2,
424 ] );
425
426 $cache = new HashBagOStuff( [
427 'reportDupes' => true,
428 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
429 'logger' => $logger,
430 ] );
431 $cache->get( 'foo' );
432 $cache->get( 'bar' );
433 $cache->get( 'foo' );
434
435 DeferredUpdates::doUpdates();
436 }
437
438 /**
439 * @covers MediumSpecificBagOStuff::lock()
440 * @covers MediumSpecificBagOStuff::unlock()
441 */
442 public function testLocking() {
443 $key = 'test';
444 $this->assertTrue( $this->cache->lock( $key ) );
445 $this->assertFalse( $this->cache->lock( $key ) );
446 $this->assertTrue( $this->cache->unlock( $key ) );
447
448 $key2 = 'test2';
449 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
450 $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
451 $this->assertTrue( $this->cache->unlock( $key2 ) );
452 $this->assertTrue( $this->cache->unlock( $key2 ) );
453 }
454
455 public function tearDown() {
456 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
457 $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
458
459 parent::tearDown();
460 }
461 }