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 $cache->delete( $key );
179 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
180 $this->assertEquals( $value, $ret );
181 $this->assertEquals( 1, $calls, 'Value was populated' );
183 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
184 $this->internalCache
->lock( $key, 0 );
185 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
186 $this->assertEquals( $value, $ret );
187 $this->assertEquals( 1, $calls, 'Callback was not used' );
191 * @covers WANObjectCache::getMulti()
193 public function testGetMulti() {
194 $cache = $this->cache
;
196 $value1 = array( 'this' => 'is', 'a' => 'test' );
197 $value2 = array( 'this' => 'is', 'another' => 'test' );
199 $key1 = wfRandomString();
200 $key2 = wfRandomString();
201 $key3 = wfRandomString();
203 $cache->set( $key1, $value1, 5 );
204 $cache->set( $key2, $value2, 10 );
208 array( $key1 => $value1, $key2 => $value2 ),
209 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
212 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
213 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
214 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
216 $cKey1 = wfRandomString();
217 $cKey2 = wfRandomString();
220 array( $key1 => $value1, $key2 => $value2 ),
221 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
222 'Result array populated'
225 $priorTime = microtime( true );
229 array( $key1 => $value1, $key2 => $value2 ),
230 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
231 "Result array populated even with new check keys"
233 $t1 = $cache->getCheckKeyTime( $cKey1 );
234 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
235 $t2 = $cache->getCheckKeyTime( $cKey2 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
237 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
238 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
239 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
244 array( $key1 => $value1, $key2 => $value2 ),
245 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
246 "Result array still populated even with new check keys"
248 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
249 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
250 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
254 * @covers WANObjectCache::delete()
256 public function testDelete() {
257 $key = wfRandomString();
258 $value = wfRandomString();
259 $this->cache
->set( $key, $value );
262 $v = $this->cache
->get( $key, $curTTL );
263 $this->assertEquals( $value, $v, "Key was created with value" );
264 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
266 $this->cache
->delete( $key );
269 $v = $this->cache
->get( $key, $curTTL );
270 $this->assertFalse( $v, "Deleted key has false value" );
271 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
273 $this->cache
->set( $key, $value . 'more' );
274 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
275 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
279 * @covers WANObjectCache::touchCheckKey()
280 * @covers WANObjectCache::resetCheckKey()
281 * @covers WANObjectCache::getCheckKeyTime()
283 public function testTouchKeys() {
284 $key = wfRandomString();
286 $priorTime = microtime( true );
288 $t0 = $this->cache
->getCheckKeyTime( $key );
289 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
291 $priorTime = microtime( true );
293 $this->cache
->touchCheckKey( $key );
294 $t1 = $this->cache
->getCheckKeyTime( $key );
295 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
297 $t2 = $this->cache
->getCheckKeyTime( $key );
298 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
301 $this->cache
->touchCheckKey( $key );
302 $t3 = $this->cache
->getCheckKeyTime( $key );
303 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
305 $t4 = $this->cache
->getCheckKeyTime( $key );
306 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
309 $this->cache
->resetCheckKey( $key );
310 $t5 = $this->cache
->getCheckKeyTime( $key );
311 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
313 $t6 = $this->cache
->getCheckKeyTime( $key );
314 $this->assertEquals( $t5, $t6, 'Check key time did not change' );