jquery.accessKeyLabel: make modifier info public
[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', array( 'a', 'bc:', 'de' ) ),
56 $cache->makeKeyInternal( 'prefix', array( '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::add
143 */
144 public function testAdd() {
145 $key = wfMemcKey( 'test' );
146 $this->assertTrue( $this->cache->add( $key, 'test' ) );
147 }
148
149 public function testGet() {
150 $value = array( 'this' => 'is', 'a' => 'test' );
151
152 $key = wfMemcKey( 'test' );
153 $this->cache->add( $key, $value );
154 $this->assertEquals( $this->cache->get( $key ), $value );
155 }
156
157 /**
158 * @covers BagOStuff::getWithSetCallback
159 */
160 public function testGetWithSetCallback() {
161 $key = wfMemcKey( 'test' );
162 $value = $this->cache->getWithSetCallback(
163 $key,
164 30,
165 function () {
166 return 'hello kitty';
167 }
168 );
169
170 $this->assertEquals( 'hello kitty', $value );
171 $this->assertEquals( $value, $this->cache->get( $key ) );
172 }
173
174 /**
175 * @covers BagOStuff::incr
176 */
177 public function testIncr() {
178 $key = wfMemcKey( 'test' );
179 $this->cache->add( $key, 0 );
180 $this->cache->incr( $key );
181 $expectedValue = 1;
182 $actualValue = $this->cache->get( $key );
183 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
184 }
185
186 /**
187 * @covers BagOStuff::getMulti
188 */
189 public function testGetMulti() {
190 $value1 = array( 'this' => 'is', 'a' => 'test' );
191 $value2 = array( 'this' => 'is', 'another' => 'test' );
192 $value3 = array( 'testing a key that may be encoded when sent to cache backend' );
193 $value4 = array( 'another test where chars in key will be encoded' );
194
195 $key1 = wfMemcKey( 'test1' );
196 $key2 = wfMemcKey( 'test2' );
197 // internally, MemcachedBagOStuffs will encode to will-%25-encode
198 $key3 = wfMemcKey( 'will-%-encode' );
199 $key4 = wfMemcKey(
200 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
201 );
202
203 $this->cache->add( $key1, $value1 );
204 $this->cache->add( $key2, $value2 );
205 $this->cache->add( $key3, $value3 );
206 $this->cache->add( $key4, $value4 );
207
208 $this->assertEquals(
209 array( $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ),
210 $this->cache->getMulti( array( $key1, $key2, $key3, $key4 ) )
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
220 /**
221 * @covers BagOStuff::getScopedLock
222 */
223 public function testGetScopedLock() {
224 $key = wfMemcKey( 'test' );
225 $value1 = $this->cache->getScopedLock( $key, 0 );
226 $value2 = $this->cache->getScopedLock( $key, 0 );
227
228 $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
229 $this->assertNull( $value2, 'Duplicate call returned no lock' );
230
231 unset( $value1 );
232
233 $value3 = $this->cache->getScopedLock( $key, 0 );
234 $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
235 unset( $value3 );
236
237 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
238 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
239
240 $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
241 $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );
242 }
243 }