Add @covers tags to objectcache tests
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
1 <?php
2
3 use Wikimedia\ScopedCallback;
4
5 /**
6 * @author Matthias Mullie <mmullie@wikimedia.org>
7 * @group BagOStuff
8 */
9 class BagOStuffTest extends MediaWikiTestCase {
10 /** @var BagOStuff */
11 private $cache;
12
13 protected function setUp() {
14 parent::setUp();
15
16 // type defined through parameter
17 if ( $this->getCliArg( 'use-bagostuff' ) ) {
18 $name = $this->getCliArg( 'use-bagostuff' );
19
20 $this->cache = ObjectCache::newFromId( $name );
21 } else {
22 // no type defined - use simple hash
23 $this->cache = new HashBagOStuff;
24 }
25
26 $this->cache->delete( wfMemcKey( 'test' ) );
27 }
28
29 /**
30 * @covers BagOStuff::makeGlobalKey
31 * @covers BagOStuff::makeKeyInternal
32 */
33 public function testMakeKey() {
34 $cache = ObjectCache::newFromId( 'hash' );
35
36 $localKey = $cache->makeKey( 'first', 'second', 'third' );
37 $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
38
39 $this->assertStringMatchesFormat(
40 '%Sfirst%Ssecond%Sthird%S',
41 $localKey,
42 'Local key interpolates parameters'
43 );
44
45 $this->assertStringMatchesFormat(
46 'global%Sfirst%Ssecond%Sthird%S',
47 $globalKey,
48 'Global key interpolates parameters and contains global prefix'
49 );
50
51 $this->assertNotEquals(
52 $localKey,
53 $globalKey,
54 'Local key and global key with same parameters should not be equal'
55 );
56
57 $this->assertNotEquals(
58 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
59 $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
60 );
61 }
62
63 /**
64 * @covers BagOStuff::merge
65 * @covers BagOStuff::mergeViaLock
66 */
67 public function testMerge() {
68 $key = wfMemcKey( 'test' );
69
70 $usleep = 0;
71
72 /**
73 * Callback method: append "merged" to whatever is in cache.
74 *
75 * @param BagOStuff $cache
76 * @param string $key
77 * @param int $existingValue
78 * @use int $usleep
79 * @return int
80 */
81 $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
82 // let's pretend this is an expensive callback to test concurrent merge attempts
83 usleep( $usleep );
84
85 if ( $existingValue === false ) {
86 return 'merged';
87 }
88
89 return $existingValue . 'merged';
90 };
91
92 // merge on non-existing value
93 $merged = $this->cache->merge( $key, $callback, 0 );
94 $this->assertTrue( $merged );
95 $this->assertEquals( $this->cache->get( $key ), 'merged' );
96
97 // merge on existing value
98 $merged = $this->cache->merge( $key, $callback, 0 );
99 $this->assertTrue( $merged );
100 $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
101
102 /*
103 * Test concurrent merges by forking this process, if:
104 * - not manually called with --use-bagostuff
105 * - pcntl_fork is supported by the system
106 * - cache type will correctly support calls over forks
107 */
108 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
109 $fork &= function_exists( 'pcntl_fork' );
110 $fork &= !$this->cache instanceof HashBagOStuff;
111 $fork &= !$this->cache instanceof EmptyBagOStuff;
112 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
113 if ( $fork ) {
114 // callback should take awhile now so that we can test concurrent merge attempts
115 $pid = pcntl_fork();
116 if ( $pid == -1 ) {
117 // can't fork, ignore this test...
118 } elseif ( $pid ) {
119 // wait a little, making sure that the child process is calling merge
120 usleep( 3000 );
121
122 // attempt a merge - this should fail
123 $merged = $this->cache->merge( $key, $callback, 0, 1 );
124
125 // merge has failed because child process was merging (and we only attempted once)
126 $this->assertFalse( $merged );
127
128 // make sure the child's merge is completed and verify
129 usleep( 3000 );
130 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
131 } else {
132 $this->cache->merge( $key, $callback, 0, 1 );
133
134 // Note: I'm not even going to check if the merge worked, I'll
135 // compare values in the parent process to test if this merge worked.
136 // I'm just going to exit this child process, since I don't want the
137 // child to output any test results (would be rather confusing to
138 // have test output twice)
139 exit;
140 }
141 }
142 }
143
144 /**
145 * @covers BagOStuff::changeTTL
146 */
147 public function testChangeTTL() {
148 $key = wfMemcKey( 'test' );
149 $value = 'meow';
150
151 $this->cache->add( $key, $value );
152 $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
153 $this->assertEquals( $this->cache->get( $key ), $value );
154 $this->cache->delete( $key );
155 $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
156 }
157
158 /**
159 * @covers BagOStuff::add
160 */
161 public function testAdd() {
162 $key = wfMemcKey( 'test' );
163 $this->assertTrue( $this->cache->add( $key, 'test' ) );
164 }
165
166 /**
167 * @covers BagOStuff::get
168 */
169 public function testGet() {
170 $value = [ 'this' => 'is', 'a' => 'test' ];
171
172 $key = wfMemcKey( 'test' );
173 $this->cache->add( $key, $value );
174 $this->assertEquals( $this->cache->get( $key ), $value );
175 }
176
177 /**
178 * @covers BagOStuff::getWithSetCallback
179 */
180 public function testGetWithSetCallback() {
181 $key = wfMemcKey( 'test' );
182 $value = $this->cache->getWithSetCallback(
183 $key,
184 30,
185 function () {
186 return 'hello kitty';
187 }
188 );
189
190 $this->assertEquals( 'hello kitty', $value );
191 $this->assertEquals( $value, $this->cache->get( $key ) );
192 }
193
194 /**
195 * @covers BagOStuff::incr
196 */
197 public function testIncr() {
198 $key = wfMemcKey( 'test' );
199 $this->cache->add( $key, 0 );
200 $this->cache->incr( $key );
201 $expectedValue = 1;
202 $actualValue = $this->cache->get( $key );
203 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
204 }
205
206 /**
207 * @covers BagOStuff::incrWithInit
208 */
209 public function testIncrWithInit() {
210 $key = wfMemcKey( 'test' );
211 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
212 $this->assertEquals( 3, $val, "Correct init value" );
213
214 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
215 $this->assertEquals( 4, $val, "Correct init value" );
216 }
217
218 /**
219 * @covers BagOStuff::getMulti
220 */
221 public function testGetMulti() {
222 $value1 = [ 'this' => 'is', 'a' => 'test' ];
223 $value2 = [ 'this' => 'is', 'another' => 'test' ];
224 $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
225 $value4 = [ 'another test where chars in key will be encoded' ];
226
227 $key1 = wfMemcKey( 'test1' );
228 $key2 = wfMemcKey( 'test2' );
229 // internally, MemcachedBagOStuffs will encode to will-%25-encode
230 $key3 = wfMemcKey( 'will-%-encode' );
231 $key4 = wfMemcKey(
232 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
233 );
234
235 $this->cache->add( $key1, $value1 );
236 $this->cache->add( $key2, $value2 );
237 $this->cache->add( $key3, $value3 );
238 $this->cache->add( $key4, $value4 );
239
240 $this->assertEquals(
241 [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
242 $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
243 );
244
245 // cleanup
246 $this->cache->delete( $key1 );
247 $this->cache->delete( $key2 );
248 $this->cache->delete( $key3 );
249 $this->cache->delete( $key4 );
250 }
251
252 /**
253 * @covers BagOStuff::getScopedLock
254 */
255 public function testGetScopedLock() {
256 $key = wfMemcKey( 'test' );
257 $value1 = $this->cache->getScopedLock( $key, 0 );
258 $value2 = $this->cache->getScopedLock( $key, 0 );
259
260 $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
261 $this->assertNull( $value2, 'Duplicate call returned no lock' );
262
263 unset( $value1 );
264
265 $value3 = $this->cache->getScopedLock( $key, 0 );
266 $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
267 unset( $value3 );
268
269 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
270 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
271
272 $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
273 $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
274 }
275
276 /**
277 * @covers BagOStuff::__construct
278 * @covers BagOStuff::trackDuplicateKeys
279 */
280 public function testReportDupes() {
281 $logger = $this->createMock( Psr\Log\NullLogger::class );
282 $logger->expects( $this->once() )
283 ->method( 'warning' )
284 ->with( 'Duplicate get(): "{key}" fetched {count} times', [
285 'key' => 'foo',
286 'count' => 2,
287 ] );
288
289 $cache = new HashBagOStuff( [
290 'reportDupes' => true,
291 'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
292 'logger' => $logger,
293 ] );
294 $cache->get( 'foo' );
295 $cache->get( 'bar' );
296 $cache->get( 'foo' );
297
298 DeferredUpdates::doUpdates();
299 }
300 }