Merge "Add invalidateUserSessions.php maintenance script"
[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 );
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 ] ); // should use interim value
230 $this->assertEquals( $value, $ret );
231 $this->assertEquals( 2, $calls, 'Callback was used' );
232
233 $ret = $cache->getWithSetCallback( $key, 30, $func,
234 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
235 $this->assertEquals( $value, $ret );
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::getMulti()
272 */
273 public function testGetMulti() {
274 $cache = $this->cache;
275
276 $value1 = [ 'this' => 'is', 'a' => 'test' ];
277 $value2 = [ 'this' => 'is', 'another' => 'test' ];
278
279 $key1 = wfRandomString();
280 $key2 = wfRandomString();
281 $key3 = wfRandomString();
282
283 $cache->set( $key1, $value1, 5 );
284 $cache->set( $key2, $value2, 10 );
285
286 $curTTLs = [];
287 $this->assertEquals(
288 [ $key1 => $value1, $key2 => $value2 ],
289 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
290 'Result array populated'
291 );
292
293 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
294 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
295 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
296
297 $cKey1 = wfRandomString();
298 $cKey2 = wfRandomString();
299
300 $priorTime = microtime( true );
301 usleep( 1 );
302 $curTTLs = [];
303 $this->assertEquals(
304 [ $key1 => $value1, $key2 => $value2 ],
305 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
306 "Result array populated even with new check keys"
307 );
308 $t1 = $cache->getCheckKeyTime( $cKey1 );
309 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
310 $t2 = $cache->getCheckKeyTime( $cKey2 );
311 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
312 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
313 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
314 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
315
316 usleep( 1 );
317 $curTTLs = [];
318 $this->assertEquals(
319 [ $key1 => $value1, $key2 => $value2 ],
320 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
321 "Result array still populated even with new check keys"
322 );
323 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
324 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
325 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
326 }
327
328 /**
329 * @covers WANObjectCache::getMulti()
330 * @covers WANObjectCache::processCheckKeys()
331 */
332 public function testGetMultiCheckKeys() {
333 $cache = $this->cache;
334
335 $checkAll = wfRandomString();
336 $check1 = wfRandomString();
337 $check2 = wfRandomString();
338 $check3 = wfRandomString();
339 $value1 = wfRandomString();
340 $value2 = wfRandomString();
341
342 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
343 // several seconds during the test to assert the behaviour.
344 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
345 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
346 }
347 usleep( 100 );
348
349 $cache->set( 'key1', $value1, 10 );
350 $cache->set( 'key2', $value2, 10 );
351
352 $curTTLs = [];
353 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
354 'key1' => $check1,
355 $checkAll,
356 'key2' => $check2,
357 'key3' => $check3,
358 ] );
359 $this->assertEquals(
360 [ 'key1' => $value1, 'key2' => $value2 ],
361 $result,
362 'Initial values'
363 );
364 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
365 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
366 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
367 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
368
369 $cache->touchCheckKey( $check1 );
370
371 $curTTLs = [];
372 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
373 'key1' => $check1,
374 $checkAll,
375 'key2' => $check2,
376 'key3' => $check3,
377 ] );
378 $this->assertEquals(
379 [ 'key1' => $value1, 'key2' => $value2 ],
380 $result,
381 'key1 expired by check1, but value still provided'
382 );
383 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
384 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
385
386 $cache->touchCheckKey( $checkAll );
387
388 $curTTLs = [];
389 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
390 'key1' => $check1,
391 $checkAll,
392 'key2' => $check2,
393 'key3' => $check3,
394 ] );
395 $this->assertEquals(
396 [ 'key1' => $value1, 'key2' => $value2 ],
397 $result,
398 'All keys expired by checkAll, but value still provided'
399 );
400 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
401 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
402 }
403
404 /**
405 * @covers WANObjectCache::get()
406 * @covers WANObjectCache::processCheckKeys()
407 */
408 public function testCheckKeyInitHoldoff() {
409 $cache = $this->cache;
410
411 for ( $i = 0; $i < 500; ++$i ) {
412 $key = wfRandomString();
413 $checkKey = wfRandomString();
414 // miss, set, hit
415 $cache->get( $key, $curTTL, [ $checkKey ] );
416 $cache->set( $key, 'val', 10 );
417 $curTTL = null;
418 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
419
420 $this->assertEquals( 'val', $v );
421 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
422 }
423
424 for ( $i = 0; $i < 500; ++$i ) {
425 $key = wfRandomString();
426 $checkKey = wfRandomString();
427 // set, hit
428 $cache->set( $key, 'val', 10 );
429 $curTTL = null;
430 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
431
432 $this->assertEquals( 'val', $v );
433 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
434 }
435 }
436
437 /**
438 * @covers WANObjectCache::delete()
439 */
440 public function testDelete() {
441 $key = wfRandomString();
442 $value = wfRandomString();
443 $this->cache->set( $key, $value );
444
445 $curTTL = null;
446 $v = $this->cache->get( $key, $curTTL );
447 $this->assertEquals( $value, $v, "Key was created with value" );
448 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
449
450 $this->cache->delete( $key );
451
452 $curTTL = null;
453 $v = $this->cache->get( $key, $curTTL );
454 $this->assertFalse( $v, "Deleted key has false value" );
455 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
456
457 $this->cache->set( $key, $value . 'more' );
458 $v = $this->cache->get( $key, $curTTL );
459 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
460 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
461
462 $this->cache->set( $key, $value );
463 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
464
465 $curTTL = null;
466 $v = $this->cache->get( $key, $curTTL );
467 $this->assertFalse( $v, "Deleted key has false value" );
468 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
469
470 $this->cache->set( $key, $value );
471 $v = $this->cache->get( $key, $curTTL );
472 $this->assertEquals( $value, $v, "Key was created with value" );
473 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
474 }
475
476 /**
477 * @dataProvider getWithSetCallback_versions_provider
478 * @param array $extOpts
479 * @param $versioned
480 */
481 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
482 $cache = $this->cache;
483
484 $key = wfRandomString();
485 $value = wfRandomString();
486
487 $wasSet = 0;
488 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
489 ++$wasSet;
490 return $value;
491 };
492
493 // Set the main key (version N if versioned)
494 $wasSet = 0;
495 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
496 $this->assertEquals( $value, $v, "Value returned" );
497 $this->assertEquals( 1, $wasSet, "Value regenerated" );
498 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
499 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
500 // Set the key for version N+1 (if versioned)
501 if ( $versioned ) {
502 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
503
504 $wasSet = 0;
505 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
506 $this->assertEquals( $value, $v, "Value returned" );
507 $this->assertEquals( 1, $wasSet, "Value regenerated" );
508
509 $wasSet = 0;
510 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
511 $this->assertEquals( $value, $v, "Value returned" );
512 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
513 }
514
515 $wasSet = 0;
516 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
517 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
518
519 $wasSet = 0;
520 $cache->delete( $key );
521 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
522 $this->assertEquals( $value, $v, "Value returned" );
523 $this->assertEquals( 1, $wasSet, "Value regenerated" );
524
525 if ( $versioned ) {
526 $wasSet = 0;
527 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
528 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
529 $this->assertEquals( $value, $v, "Value returned" );
530 $this->assertEquals( 1, $wasSet, "Value regenerated" );
531 }
532 }
533
534 public static function getWithSetCallback_versions_provider() {
535 return [
536 [ [], false ],
537 [ [ 'version' => 1 ], true ]
538 ];
539 }
540
541 /**
542 * @covers WANObjectCache::touchCheckKey()
543 * @covers WANObjectCache::resetCheckKey()
544 * @covers WANObjectCache::getCheckKeyTime()
545 */
546 public function testTouchKeys() {
547 $key = wfRandomString();
548
549 $priorTime = microtime( true );
550 usleep( 100 );
551 $t0 = $this->cache->getCheckKeyTime( $key );
552 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
553
554 $priorTime = microtime( true );
555 usleep( 100 );
556 $this->cache->touchCheckKey( $key );
557 $t1 = $this->cache->getCheckKeyTime( $key );
558 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
559
560 $t2 = $this->cache->getCheckKeyTime( $key );
561 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
562
563 usleep( 100 );
564 $this->cache->touchCheckKey( $key );
565 $t3 = $this->cache->getCheckKeyTime( $key );
566 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
567
568 $t4 = $this->cache->getCheckKeyTime( $key );
569 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
570
571 usleep( 100 );
572 $this->cache->resetCheckKey( $key );
573 $t5 = $this->cache->getCheckKeyTime( $key );
574 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
575
576 $t6 = $this->cache->getCheckKeyTime( $key );
577 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
578 }
579
580 /**
581 * @covers WANObjectCache::getMulti()
582 */
583 public function testGetWithSeveralCheckKeys() {
584 $key = wfRandomString();
585 $tKey1 = wfRandomString();
586 $tKey2 = wfRandomString();
587 $value = 'meow';
588
589 // Two check keys are newer (given hold-off) than $key, another is older
590 $this->internalCache->set(
591 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
592 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
593 );
594 $this->internalCache->set(
595 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
596 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
597 );
598 $this->internalCache->set(
599 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
600 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
601 );
602 $this->cache->set( $key, $value, 30 );
603
604 $curTTL = null;
605 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
606 $this->assertEquals( $value, $v, "Value matches" );
607 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
608 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
609 }
610
611 /**
612 * @covers WANObjectCache::set()
613 */
614 public function testSetWithLag() {
615 $value = 1;
616
617 $key = wfRandomString();
618 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
619 $this->cache->set( $key, $value, 30, $opts );
620 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
621
622 $key = wfRandomString();
623 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
624 $this->cache->set( $key, $value, 30, $opts );
625 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
626
627 $key = wfRandomString();
628 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
629 $this->cache->set( $key, $value, 30, $opts );
630 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
631 }
632
633 /**
634 * @covers WANObjectCache::set()
635 */
636 public function testWritePending() {
637 $value = 1;
638
639 $key = wfRandomString();
640 $opts = [ 'pending' => true ];
641 $this->cache->set( $key, $value, 30, $opts );
642 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
643 }
644 }