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