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