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