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