Merge "Remove AutoLoader::loadClass()"
[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( [
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( [] )
21 ] );
22 }
23
24 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
25 $this->internalCache = $wanCache->cache;
26 }
27
28 /**
29 * @dataProvider provideSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
32 * @param mixed $value
33 * @param integer $ttl
34 */
35 public function testSetAndGet( $value, $ttl ) {
36 $curTTL = null;
37 $asOf = null;
38 $key = wfRandomString();
39
40 $this->cache->get( $key, $curTTL, [], $asOf );
41 $this->assertNull( $curTTL, "Current TTL is null" );
42 $this->assertNull( $asOf, "Current as-of-time is infinite" );
43
44 $t = microtime( true );
45 $this->cache->set( $key, $value, $ttl );
46
47 $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
48 if ( is_infinite( $ttl ) || $ttl == 0 ) {
49 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
50 } else {
51 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
52 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
53 }
54 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
55 $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
56 }
57
58 public static function provideSetAndGet() {
59 return [
60 [ 14141, 3 ],
61 [ 3535.666, 3 ],
62 [ [], 3 ],
63 [ null, 3 ],
64 [ '0', 3 ],
65 [ (object)[ 'meow' ], 3 ],
66 [ INF, 3 ],
67 [ '', 3 ],
68 [ 'pizzacat', INF ],
69 ];
70 }
71
72 /**
73 * @covers WANObjectCache::get()
74 */
75 public function testGetNotExists() {
76 $key = wfRandomString();
77 $curTTL = null;
78 $value = $this->cache->get( $key, $curTTL );
79
80 $this->assertFalse( $value, "Non-existing key has false value" );
81 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
82 }
83
84 /**
85 * @covers WANObjectCache::set()
86 */
87 public function testSetOver() {
88 $key = wfRandomString();
89 for ( $i = 0; $i < 3; ++$i ) {
90 $value = wfRandomString();
91 $this->cache->set( $key, $value, 3 );
92
93 $this->assertEquals( $this->cache->get( $key ), $value );
94 }
95 }
96
97 /**
98 * @covers WANObjectCache::set()
99 */
100 public function testStaleSet() {
101 $key = wfRandomString();
102 $value = wfRandomString();
103 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
104
105 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
106 }
107
108 /**
109 * @dataProvider getWithSetCallback_provider
110 * @covers WANObjectCache::getWithSetCallback()
111 * @covers WANObjectCache::doGetWithSetCallback()
112 * @param array $extOpts
113 * @param bool $versioned
114 */
115 public function testGetWithSetCallback( array $extOpts, $versioned ) {
116 $cache = $this->cache;
117
118 $key = wfRandomString();
119 $value = wfRandomString();
120 $cKey1 = wfRandomString();
121 $cKey2 = wfRandomString();
122
123 $wasSet = 0;
124 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
125 ++$wasSet;
126 $ttl = 20; // override with another value
127 return $value;
128 };
129
130 $wasSet = 0;
131 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
132 $this->assertEquals( $value, $v, "Value returned" );
133 $this->assertEquals( 1, $wasSet, "Value regenerated" );
134
135 $curTTL = null;
136 $cache->get( $key, $curTTL );
137 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
138 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
139
140 $wasSet = 0;
141 $v = $cache->getWithSetCallback( $key, 30, $func, [
142 'lowTTL' => 0,
143 'lockTSE' => 5,
144 ] + $extOpts );
145 $this->assertEquals( $value, $v, "Value returned" );
146 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
147
148 $priorTime = microtime( true );
149 usleep( 1 );
150 $wasSet = 0;
151 $v = $cache->getWithSetCallback(
152 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
153 );
154 $this->assertEquals( $value, $v, "Value returned" );
155 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
156 $t1 = $cache->getCheckKeyTime( $cKey1 );
157 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
158 $t2 = $cache->getCheckKeyTime( $cKey2 );
159 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
160
161 $priorTime = microtime( true );
162 $wasSet = 0;
163 $v = $cache->getWithSetCallback(
164 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
165 );
166 $this->assertEquals( $value, $v, "Value returned" );
167 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
168 $t1 = $cache->getCheckKeyTime( $cKey1 );
169 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
170 $t2 = $cache->getCheckKeyTime( $cKey2 );
171 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
172
173 $curTTL = null;
174 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
175 if ( $versioned ) {
176 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
177 } else {
178 $this->assertEquals( $value, $v, "Value returned" );
179 }
180 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
181
182 $wasSet = 0;
183 $key = wfRandomString();
184 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
185 $this->assertEquals( $value, $v, "Value returned" );
186 $cache->delete( $key );
187 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
188 $this->assertEquals( $value, $v, "Value still returned after deleted" );
189 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
190 }
191
192 public static function getWithSetCallback_provider() {
193 return [
194 [ [], false ],
195 [ [ 'version' => 1 ], true ]
196 ];
197 }
198
199 /**
200 * @covers WANObjectCache::getWithSetCallback()
201 * @covers WANObjectCache::doGetWithSetCallback()
202 */
203 public function testLockTSE() {
204 $cache = $this->cache;
205 $key = wfRandomString();
206 $value = wfRandomString();
207
208 $calls = 0;
209 $func = function() use ( &$calls, $value ) {
210 ++$calls;
211 return $value;
212 };
213
214 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
215 $this->assertEquals( $value, $ret );
216 $this->assertEquals( 1, $calls, 'Value was populated' );
217
218 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
219 $this->internalCache->lock( $key, 0 );
220
221 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
222 $ret = $cache->getWithSetCallback( $key, 30, $func,
223 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
224 $this->assertEquals( $value, $ret, 'Old value used' );
225 $this->assertEquals( 1, $calls, 'Callback was not used' );
226
227 $cache->delete( $key );
228 $ret = $cache->getWithSetCallback( $key, 30, $func,
229 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
230 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
231 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
232
233 $ret = $cache->getWithSetCallback( $key, 30, $func,
234 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
235 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
236 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
237 }
238
239 /**
240 * @covers WANObjectCache::getWithSetCallback()
241 * @covers WANObjectCache::doGetWithSetCallback()
242 */
243 public function testLockTSESlow() {
244 $cache = $this->cache;
245 $key = wfRandomString();
246 $value = wfRandomString();
247
248 $calls = 0;
249 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
250 ++$calls;
251 $setOpts['since'] = microtime( true ) - 10;
252 return $value;
253 };
254
255 // Value should be marked as stale due to snapshot lag
256 $curTTL = null;
257 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
258 $this->assertEquals( $value, $ret );
259 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
260 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
261 $this->assertEquals( 1, $calls, 'Value was generated' );
262
263 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
264 $this->internalCache->lock( $key, 0 );
265 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
266 $this->assertEquals( $value, $ret );
267 $this->assertEquals( 1, $calls, 'Callback was not used' );
268 }
269
270 /**
271 * @covers WANObjectCache::getWithSetCallback()
272 * @covers WANObjectCache::doGetWithSetCallback()
273 */
274 public function testBusyValue() {
275 $cache = $this->cache;
276 $key = wfRandomString();
277 $value = wfRandomString();
278 $busyValue = wfRandomString();
279
280 $calls = 0;
281 $func = function() use ( &$calls, $value ) {
282 ++$calls;
283 return $value;
284 };
285
286 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
287 $this->assertEquals( $value, $ret );
288 $this->assertEquals( 1, $calls, 'Value was populated' );
289
290 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
291 $this->internalCache->lock( $key, 0 );
292
293 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
294 $ret = $cache->getWithSetCallback( $key, 30, $func,
295 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
296 $this->assertEquals( $value, $ret, 'Callback used' );
297 $this->assertEquals( 2, $calls, 'Callback used' );
298
299 $ret = $cache->getWithSetCallback( $key, 30, $func,
300 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
301 $this->assertEquals( $value, $ret, 'Old value used' );
302 $this->assertEquals( 2, $calls, 'Callback was not used' );
303
304 $cache->delete( $key ); // no value at all anymore and still locked
305 $ret = $cache->getWithSetCallback( $key, 30, $func,
306 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
307 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
308 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
309
310 $this->internalCache->unlock( $key );
311 $ret = $cache->getWithSetCallback( $key, 30, $func,
312 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
313 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
314 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
315
316 $this->internalCache->lock( $key, 0 );
317 $ret = $cache->getWithSetCallback( $key, 30, $func,
318 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
319 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
320 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
321 }
322
323 /**
324 * @covers WANObjectCache::getMulti()
325 */
326 public function testGetMulti() {
327 $cache = $this->cache;
328
329 $value1 = [ 'this' => 'is', 'a' => 'test' ];
330 $value2 = [ 'this' => 'is', 'another' => 'test' ];
331
332 $key1 = wfRandomString();
333 $key2 = wfRandomString();
334 $key3 = wfRandomString();
335
336 $cache->set( $key1, $value1, 5 );
337 $cache->set( $key2, $value2, 10 );
338
339 $curTTLs = [];
340 $this->assertEquals(
341 [ $key1 => $value1, $key2 => $value2 ],
342 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
343 'Result array populated'
344 );
345
346 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
347 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
348 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
349
350 $cKey1 = wfRandomString();
351 $cKey2 = wfRandomString();
352
353 $priorTime = microtime( true );
354 usleep( 1 );
355 $curTTLs = [];
356 $this->assertEquals(
357 [ $key1 => $value1, $key2 => $value2 ],
358 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
359 "Result array populated even with new check keys"
360 );
361 $t1 = $cache->getCheckKeyTime( $cKey1 );
362 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
363 $t2 = $cache->getCheckKeyTime( $cKey2 );
364 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
365 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
366 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
367 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
368
369 usleep( 1 );
370 $curTTLs = [];
371 $this->assertEquals(
372 [ $key1 => $value1, $key2 => $value2 ],
373 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
374 "Result array still populated even with new check keys"
375 );
376 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
377 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
378 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
379 }
380
381 /**
382 * @covers WANObjectCache::getMulti()
383 * @covers WANObjectCache::processCheckKeys()
384 */
385 public function testGetMultiCheckKeys() {
386 $cache = $this->cache;
387
388 $checkAll = wfRandomString();
389 $check1 = wfRandomString();
390 $check2 = wfRandomString();
391 $check3 = wfRandomString();
392 $value1 = wfRandomString();
393 $value2 = wfRandomString();
394
395 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
396 // several seconds during the test to assert the behaviour.
397 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
398 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
399 }
400 usleep( 100 );
401
402 $cache->set( 'key1', $value1, 10 );
403 $cache->set( 'key2', $value2, 10 );
404
405 $curTTLs = [];
406 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
407 'key1' => $check1,
408 $checkAll,
409 'key2' => $check2,
410 'key3' => $check3,
411 ] );
412 $this->assertEquals(
413 [ 'key1' => $value1, 'key2' => $value2 ],
414 $result,
415 'Initial values'
416 );
417 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
418 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
419 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
420 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
421
422 $cache->touchCheckKey( $check1 );
423
424 $curTTLs = [];
425 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
426 'key1' => $check1,
427 $checkAll,
428 'key2' => $check2,
429 'key3' => $check3,
430 ] );
431 $this->assertEquals(
432 [ 'key1' => $value1, 'key2' => $value2 ],
433 $result,
434 'key1 expired by check1, but value still provided'
435 );
436 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
437 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
438
439 $cache->touchCheckKey( $checkAll );
440
441 $curTTLs = [];
442 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
443 'key1' => $check1,
444 $checkAll,
445 'key2' => $check2,
446 'key3' => $check3,
447 ] );
448 $this->assertEquals(
449 [ 'key1' => $value1, 'key2' => $value2 ],
450 $result,
451 'All keys expired by checkAll, but value still provided'
452 );
453 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
454 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
455 }
456
457 /**
458 * @covers WANObjectCache::get()
459 * @covers WANObjectCache::processCheckKeys()
460 */
461 public function testCheckKeyInitHoldoff() {
462 $cache = $this->cache;
463
464 for ( $i = 0; $i < 500; ++$i ) {
465 $key = wfRandomString();
466 $checkKey = wfRandomString();
467 // miss, set, hit
468 $cache->get( $key, $curTTL, [ $checkKey ] );
469 $cache->set( $key, 'val', 10 );
470 $curTTL = null;
471 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
472
473 $this->assertEquals( 'val', $v );
474 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
475 }
476
477 for ( $i = 0; $i < 500; ++$i ) {
478 $key = wfRandomString();
479 $checkKey = wfRandomString();
480 // set, hit
481 $cache->set( $key, 'val', 10 );
482 $curTTL = null;
483 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
484
485 $this->assertEquals( 'val', $v );
486 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
487 }
488 }
489
490 /**
491 * @covers WANObjectCache::delete()
492 */
493 public function testDelete() {
494 $key = wfRandomString();
495 $value = wfRandomString();
496 $this->cache->set( $key, $value );
497
498 $curTTL = null;
499 $v = $this->cache->get( $key, $curTTL );
500 $this->assertEquals( $value, $v, "Key was created with value" );
501 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
502
503 $this->cache->delete( $key );
504
505 $curTTL = null;
506 $v = $this->cache->get( $key, $curTTL );
507 $this->assertFalse( $v, "Deleted key has false value" );
508 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
509
510 $this->cache->set( $key, $value . 'more' );
511 $v = $this->cache->get( $key, $curTTL );
512 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
513 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
514
515 $this->cache->set( $key, $value );
516 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
517
518 $curTTL = null;
519 $v = $this->cache->get( $key, $curTTL );
520 $this->assertFalse( $v, "Deleted key has false value" );
521 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
522
523 $this->cache->set( $key, $value );
524 $v = $this->cache->get( $key, $curTTL );
525 $this->assertEquals( $value, $v, "Key was created with value" );
526 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
527 }
528
529 /**
530 * @dataProvider getWithSetCallback_versions_provider
531 * @param array $extOpts
532 * @param $versioned
533 */
534 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
535 $cache = $this->cache;
536
537 $key = wfRandomString();
538 $value = wfRandomString();
539
540 $wasSet = 0;
541 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
542 ++$wasSet;
543 return $value;
544 };
545
546 // Set the main key (version N if versioned)
547 $wasSet = 0;
548 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
549 $this->assertEquals( $value, $v, "Value returned" );
550 $this->assertEquals( 1, $wasSet, "Value regenerated" );
551 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
552 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
553 // Set the key for version N+1 (if versioned)
554 if ( $versioned ) {
555 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
556
557 $wasSet = 0;
558 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
559 $this->assertEquals( $value, $v, "Value returned" );
560 $this->assertEquals( 1, $wasSet, "Value regenerated" );
561
562 $wasSet = 0;
563 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
564 $this->assertEquals( $value, $v, "Value returned" );
565 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
566 }
567
568 $wasSet = 0;
569 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
570 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
571
572 $wasSet = 0;
573 $cache->delete( $key );
574 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
575 $this->assertEquals( $value, $v, "Value returned" );
576 $this->assertEquals( 1, $wasSet, "Value regenerated" );
577
578 if ( $versioned ) {
579 $wasSet = 0;
580 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
581 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
582 $this->assertEquals( $value, $v, "Value returned" );
583 $this->assertEquals( 1, $wasSet, "Value regenerated" );
584 }
585 }
586
587 public static function getWithSetCallback_versions_provider() {
588 return [
589 [ [], false ],
590 [ [ 'version' => 1 ], true ]
591 ];
592 }
593
594 /**
595 * @covers WANObjectCache::touchCheckKey()
596 * @covers WANObjectCache::resetCheckKey()
597 * @covers WANObjectCache::getCheckKeyTime()
598 */
599 public function testTouchKeys() {
600 $key = wfRandomString();
601
602 $priorTime = microtime( true );
603 usleep( 100 );
604 $t0 = $this->cache->getCheckKeyTime( $key );
605 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
606
607 $priorTime = microtime( true );
608 usleep( 100 );
609 $this->cache->touchCheckKey( $key );
610 $t1 = $this->cache->getCheckKeyTime( $key );
611 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
612
613 $t2 = $this->cache->getCheckKeyTime( $key );
614 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
615
616 usleep( 100 );
617 $this->cache->touchCheckKey( $key );
618 $t3 = $this->cache->getCheckKeyTime( $key );
619 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
620
621 $t4 = $this->cache->getCheckKeyTime( $key );
622 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
623
624 usleep( 100 );
625 $this->cache->resetCheckKey( $key );
626 $t5 = $this->cache->getCheckKeyTime( $key );
627 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
628
629 $t6 = $this->cache->getCheckKeyTime( $key );
630 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
631 }
632
633 /**
634 * @covers WANObjectCache::getMulti()
635 */
636 public function testGetWithSeveralCheckKeys() {
637 $key = wfRandomString();
638 $tKey1 = wfRandomString();
639 $tKey2 = wfRandomString();
640 $value = 'meow';
641
642 // Two check keys are newer (given hold-off) than $key, another is older
643 $this->internalCache->set(
644 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
645 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
646 );
647 $this->internalCache->set(
648 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
649 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
650 );
651 $this->internalCache->set(
652 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
653 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
654 );
655 $this->cache->set( $key, $value, 30 );
656
657 $curTTL = null;
658 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
659 $this->assertEquals( $value, $v, "Value matches" );
660 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
661 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
662 }
663
664 /**
665 * @covers WANObjectCache::set()
666 */
667 public function testSetWithLag() {
668 $value = 1;
669
670 $key = wfRandomString();
671 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
672 $this->cache->set( $key, $value, 30, $opts );
673 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
674
675 $key = wfRandomString();
676 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
677 $this->cache->set( $key, $value, 30, $opts );
678 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
679
680 $key = wfRandomString();
681 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
682 $this->cache->set( $key, $value, 30, $opts );
683 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
684 }
685
686 /**
687 * @covers WANObjectCache::set()
688 */
689 public function testWritePending() {
690 $value = 1;
691
692 $key = wfRandomString();
693 $opts = [ 'pending' => true ];
694 $this->cache->set( $key, $value, 30, $opts );
695 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
696 }
697 }