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