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