Merge "Improve MIME detection in FileBackend"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
1 <?php
2
3 class WANObjectCacheTest extends MediaWikiTestCase {
4 /** @var WANObjectCache */
5 private $cache;
6 /**@var BagOStuff */
7 private $internalCache;
8
9 protected function setUp() {
10 parent::setUp();
11
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
14
15 $this->cache = ObjectCache::getWANInstance( $name );
16 } else {
17 $this->cache = new WANObjectCache( array(
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( array() )
21 ) );
22 }
23
24 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
25 $this->internalCache = $wanCache->cache;
26 }
27
28 /**
29 * @dataProvider provider_testSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
32 * @param mixed $value
33 * @param integer $ttl
34 */
35 public function testSetAndGet( $value, $ttl ) {
36 $key = wfRandomString();
37 $this->cache->set( $key, $value, $ttl );
38
39 $curTTL = null;
40 $this->assertEquals( $value, $this->cache->get( $key, $curTTL ) );
41 if ( is_infinite( $ttl ) || $ttl == 0 ) {
42 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
43 } else {
44 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
45 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
46 }
47 }
48
49 public static function provider_testSetAndGet() {
50 return array(
51 array( 14141, 3 ),
52 array( 3535.666, 3 ),
53 array( array(), 3 ),
54 array( null, 3 ),
55 array( '0', 3 ),
56 array( (object)array( 'meow' ), 3 ),
57 array( INF, 3 ),
58 array( '', 3 ),
59 array( 'pizzacat', INF ),
60 );
61 }
62
63 public function testGetNotExists() {
64 $key = wfRandomString();
65 $curTTL = null;
66 $value = $this->cache->get( $key, $curTTL );
67
68 $this->assertFalse( $value, "Non-existing key has false value" );
69 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
70 }
71
72 public function testSetOver() {
73 $key = wfRandomString();
74 for ( $i = 0; $i < 3; ++$i ) {
75 $value = wfRandomString();
76 $this->cache->set( $key, $value, 3 );
77
78 $this->assertEquals( $this->cache->get( $key ), $value );
79 }
80 }
81
82 public function testStaleSet() {
83 $key = wfRandomString();
84 $value = wfRandomString();
85 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
86
87 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
88 }
89
90 /**
91 * @covers WANObjectCache::getWithSetCallback()
92 */
93 public function testGetWithSetCallback() {
94 $cache = $this->cache;
95
96 $key = wfRandomString();
97 $value = wfRandomString();
98 $cKey1 = wfRandomString();
99 $cKey2 = wfRandomString();
100
101 $wasSet = 0;
102 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
103 ++$wasSet;
104 $ttl = 20; // override with another value
105 return $value;
106 };
107
108 $wasSet = 0;
109 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
110 $this->assertEquals( $value, $v, "Value returned" );
111 $this->assertEquals( 1, $wasSet, "Value regenerated" );
112
113 $curTTL = null;
114 $cache->get( $key, $curTTL );
115 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
116 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
117
118 $wasSet = 0;
119 $v = $cache->getWithSetCallback( $key, 30, $func, array(
120 'lowTTL' => 0,
121 'lockTSE' => 5,
122 ) );
123 $this->assertEquals( $value, $v, "Value returned" );
124 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
125
126 $priorTime = microtime( true );
127 usleep( 1 );
128 $wasSet = 0;
129 $v = $cache->getWithSetCallback( $key, 30, $func,
130 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
131 $this->assertEquals( $value, $v, "Value returned" );
132 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
133 $t1 = $cache->getCheckKeyTime( $cKey1 );
134 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
135 $t2 = $cache->getCheckKeyTime( $cKey2 );
136 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
137
138 $priorTime = microtime( true );
139 $wasSet = 0;
140 $v = $cache->getWithSetCallback( $key, 30, $func,
141 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
142 $this->assertEquals( $value, $v, "Value returned" );
143 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
144 $t1 = $cache->getCheckKeyTime( $cKey1 );
145 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
146 $t2 = $cache->getCheckKeyTime( $cKey2 );
147 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
148
149 $curTTL = null;
150 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
151 $this->assertEquals( $value, $v, "Value returned" );
152 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
153
154 $wasSet = 0;
155 $key = wfRandomString();
156 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
157 $this->assertEquals( $value, $v, "Value returned" );
158 $cache->delete( $key );
159 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
160 $this->assertEquals( $value, $v, "Value still returned after deleted" );
161 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
162 }
163
164 /**
165 * @covers WANObjectCache::getWithSetCallback()
166 */
167 public function testLockTSE() {
168 $cache = $this->cache;
169 $key = wfRandomString();
170 $value = wfRandomString();
171
172 $calls = 0;
173 $func = function() use ( &$calls, $value ) {
174 ++$calls;
175 return $value;
176 };
177
178 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
179 $this->assertEquals( $value, $ret );
180 $this->assertEquals( 1, $calls, 'Value was populated' );
181
182 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
183 $this->internalCache->lock( $key, 0 );
184 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
185 $this->assertEquals( $value, $ret );
186 $this->assertEquals( 1, $calls, 'Callback was not used' );
187 }
188
189 /**
190 * @covers WANObjectCache::getWithSetCallback()
191 */
192 public function testLockTSESlow() {
193 $cache = $this->cache;
194 $key = wfRandomString();
195 $value = wfRandomString();
196
197 $calls = 0;
198 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
199 ++$calls;
200 $setOpts['since'] = microtime( true ) - 10;
201 return $value;
202 };
203
204 // Value should be marked as stale due to snapshot lag
205 $curTTL = null;
206 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
207 $this->assertEquals( $value, $ret );
208 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
209 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
210 $this->assertEquals( 1, $calls, 'Value was generated' );
211
212 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
213 $this->internalCache->lock( $key, 0 );
214 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
215 $this->assertEquals( $value, $ret );
216 $this->assertEquals( 1, $calls, 'Callback was not used' );
217 }
218
219 /**
220 * @covers WANObjectCache::getMulti()
221 */
222 public function testGetMulti() {
223 $cache = $this->cache;
224
225 $value1 = array( 'this' => 'is', 'a' => 'test' );
226 $value2 = array( 'this' => 'is', 'another' => 'test' );
227
228 $key1 = wfRandomString();
229 $key2 = wfRandomString();
230 $key3 = wfRandomString();
231
232 $cache->set( $key1, $value1, 5 );
233 $cache->set( $key2, $value2, 10 );
234
235 $curTTLs = array();
236 $this->assertEquals(
237 array( $key1 => $value1, $key2 => $value2 ),
238 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
239 );
240
241 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
242 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
243 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
244
245 $cKey1 = wfRandomString();
246 $cKey2 = wfRandomString();
247 $curTTLs = array();
248 $this->assertEquals(
249 array( $key1 => $value1, $key2 => $value2 ),
250 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
251 'Result array populated'
252 );
253
254 $priorTime = microtime( true );
255 usleep( 1 );
256 $curTTLs = array();
257 $this->assertEquals(
258 array( $key1 => $value1, $key2 => $value2 ),
259 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
260 "Result array populated even with new check keys"
261 );
262 $t1 = $cache->getCheckKeyTime( $cKey1 );
263 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
264 $t2 = $cache->getCheckKeyTime( $cKey2 );
265 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
266 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
267 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
268 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
269
270 usleep( 1 );
271 $curTTLs = array();
272 $this->assertEquals(
273 array( $key1 => $value1, $key2 => $value2 ),
274 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
275 "Result array still populated even with new check keys"
276 );
277 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
278 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
279 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
280 }
281
282 /**
283 * @covers WANObjectCache::delete()
284 */
285 public function testDelete() {
286 $key = wfRandomString();
287 $value = wfRandomString();
288 $this->cache->set( $key, $value );
289
290 $curTTL = null;
291 $v = $this->cache->get( $key, $curTTL );
292 $this->assertEquals( $value, $v, "Key was created with value" );
293 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
294
295 $this->cache->delete( $key );
296
297 $curTTL = null;
298 $v = $this->cache->get( $key, $curTTL );
299 $this->assertFalse( $v, "Deleted key has false value" );
300 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
301
302 $this->cache->set( $key, $value . 'more' );
303 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
304 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
305 }
306
307 /**
308 * @covers WANObjectCache::touchCheckKey()
309 * @covers WANObjectCache::resetCheckKey()
310 * @covers WANObjectCache::getCheckKeyTime()
311 */
312 public function testTouchKeys() {
313 $key = wfRandomString();
314
315 $priorTime = microtime( true );
316 usleep( 100 );
317 $t0 = $this->cache->getCheckKeyTime( $key );
318 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
319
320 $priorTime = microtime( true );
321 usleep( 100 );
322 $this->cache->touchCheckKey( $key );
323 $t1 = $this->cache->getCheckKeyTime( $key );
324 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
325
326 $t2 = $this->cache->getCheckKeyTime( $key );
327 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
328
329 usleep( 100 );
330 $this->cache->touchCheckKey( $key );
331 $t3 = $this->cache->getCheckKeyTime( $key );
332 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
333
334 $t4 = $this->cache->getCheckKeyTime( $key );
335 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
336
337 usleep( 100 );
338 $this->cache->resetCheckKey( $key );
339 $t5 = $this->cache->getCheckKeyTime( $key );
340 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
341
342 $t6 = $this->cache->getCheckKeyTime( $key );
343 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
344 }
345
346 public function testSetWithLag() {
347 $value = 1;
348
349 $key = wfRandomString();
350 $opts = array( 'lag' => 300, 'since' => microtime( true ) );
351 $this->cache->set( $key, $value, 30, $opts );
352 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
353
354 $key = wfRandomString();
355 $opts = array( 'lag' => 0, 'since' => microtime( true ) - 300 );
356 $this->cache->set( $key, $value, 30, $opts );
357 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
358
359 $key = wfRandomString();
360 $opts = array( 'lag' => 5, 'since' => microtime( true ) - 5 );
361 $this->cache->set( $key, $value, 30, $opts );
362 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
363 }
364
365 public function testWritePending() {
366 $value = 1;
367
368 $key = wfRandomString();
369 $opts = array( 'pending' => true );
370 $this->cache->set( $key, $value, 30, $opts );
371 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
372 }
373 }