3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 private $internalCache;
9 protected function setUp() {
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
15 $this->cache
= ObjectCache
::getWANInstance( $name );
17 $this->cache
= new WANObjectCache( array(
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( array() )
24 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
25 $this->internalCache
= $wanCache->cache
;
29 * @dataProvider provider_testSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
35 public function testSetAndGet( $value, $ttl ) {
36 $key = wfRandomString();
37 $this->cache
->set( $key, $value, $ttl );
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" );
44 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
45 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
49 public static function provider_testSetAndGet() {
56 array( (object)array( 'meow' ), 3 ),
59 array( 'pizzacat', INF
),
63 public function testGetNotExists() {
64 $key = wfRandomString();
66 $value = $this->cache
->get( $key, $curTTL );
68 $this->assertFalse( $value, "Non-existing key has false value" );
69 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
72 public function testSetOver() {
73 $key = wfRandomString();
74 for ( $i = 0; $i < 3; ++
$i ) {
75 $value = wfRandomString();
76 $this->cache
->set( $key, $value, 3 );
78 $this->assertEquals( $this->cache
->get( $key ), $value );
82 public function testStaleSet() {
83 $key = wfRandomString();
84 $value = wfRandomString();
85 $this->cache
->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
87 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
91 * @covers WANObjectCache::getWithSetCallback()
93 public function testGetWithSetCallback() {
94 $cache = $this->cache
;
96 $key = wfRandomString();
97 $value = wfRandomString();
98 $cKey1 = wfRandomString();
99 $cKey2 = wfRandomString();
102 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
104 $ttl = 20; // override with another value
109 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
110 $this->assertEquals( $value, $v, "Value returned" );
111 $this->assertEquals( 1, $wasSet, "Value regenerated" );
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)' );
119 $v = $cache->getWithSetCallback( $key, 30, $func, array(
123 $this->assertEquals( $value, $v, "Value returned" );
124 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
126 $priorTime = microtime( true );
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' );
138 $priorTime = microtime( true );
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' );
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" );
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" );
165 * @covers WANObjectCache::getWithSetCallback()
167 public function testLockTSE() {
168 $cache = $this->cache
;
169 $key = wfRandomString();
170 $value = wfRandomString();
173 $func = function() use ( &$calls, $value ) {
178 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
179 $this->assertEquals( $value, $ret );
180 $this->assertEquals( 1, $calls, 'Value was populated' );
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' );
190 * @covers WANObjectCache::getWithSetCallback()
192 public function testLockTSESlow() {
193 $cache = $this->cache
;
194 $key = wfRandomString();
195 $value = wfRandomString();
198 $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
200 $setOpts['since'] = microtime( true ) - 10;
204 // Value should be marked as stale due to snapshot lag
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' );
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' );
220 * @covers WANObjectCache::getMulti()
222 public function testGetMulti() {
223 $cache = $this->cache
;
225 $value1 = array( 'this' => 'is', 'a' => 'test' );
226 $value2 = array( 'this' => 'is', 'another' => 'test' );
228 $key1 = wfRandomString();
229 $key2 = wfRandomString();
230 $key3 = wfRandomString();
232 $cache->set( $key1, $value1, 5 );
233 $cache->set( $key2, $value2, 10 );
237 array( $key1 => $value1, $key2 => $value2 ),
238 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
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" );
245 $cKey1 = wfRandomString();
246 $cKey2 = wfRandomString();
249 array( $key1 => $value1, $key2 => $value2 ),
250 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
251 'Result array populated'
254 $priorTime = microtime( true );
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"
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' );
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"
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' );
283 * @covers WANObjectCache::delete()
285 public function testDelete() {
286 $key = wfRandomString();
287 $value = wfRandomString();
288 $this->cache
->set( $key, $value );
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" );
295 $this->cache
->delete( $key );
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" );
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" );
308 * @covers WANObjectCache::touchCheckKey()
309 * @covers WANObjectCache::resetCheckKey()
310 * @covers WANObjectCache::getCheckKeyTime()
312 public function testTouchKeys() {
313 $key = wfRandomString();
315 $priorTime = microtime( true );
317 $t0 = $this->cache
->getCheckKeyTime( $key );
318 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
320 $priorTime = microtime( true );
322 $this->cache
->touchCheckKey( $key );
323 $t1 = $this->cache
->getCheckKeyTime( $key );
324 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
326 $t2 = $this->cache
->getCheckKeyTime( $key );
327 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
330 $this->cache
->touchCheckKey( $key );
331 $t3 = $this->cache
->getCheckKeyTime( $key );
332 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
334 $t4 = $this->cache
->getCheckKeyTime( $key );
335 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
338 $this->cache
->resetCheckKey( $key );
339 $t5 = $this->cache
->getCheckKeyTime( $key );
340 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
342 $t6 = $this->cache
->getCheckKeyTime( $key );
343 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
346 public function testSetWithLag() {
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." );
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." );
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." );
365 public function testWritePending() {
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." );