3 class WANObjectCacheTest
extends MediaWikiTestCase
{
4 /** @var WANObjectCache */
7 protected function setUp() {
10 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
11 $name = $this->getCliArg( 'use-wanobjectcache' );
13 $this->cache
= ObjectCache
::getWANInstance( $name );
15 $this->cache
= new WANObjectCache( array(
16 'cache' => new HashBagOStuff(),
17 'pool' => 'testcache-hash',
18 'relayer' => new EventRelayerNull( array() )
22 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
23 $this->internalCache
= $wanCache->cache
;
27 * @dataProvider provider_testSetAndGet
28 * @covers WANObjectCache::set()
29 * @covers WANObjectCache::get()
33 public function testSetAndGet( $value, $ttl ) {
34 $key = wfRandomString();
35 $this->cache
->set( $key, $value, $ttl );
38 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL ) );
39 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
40 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
42 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
43 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
47 public static function provider_testSetAndGet() {
54 array( (object)array( 'meow' ), 3 ),
57 array( 'pizzacat', INF
),
61 public function testGetNotExists() {
62 $key = wfRandomString();
64 $value = $this->cache
->get( $key, $curTTL );
66 $this->assertFalse( $value, "Non-existing key has false value" );
67 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
70 public function testSetOver() {
71 $key = wfRandomString();
72 for ( $i = 0; $i < 3; ++
$i ) {
73 $value = wfRandomString();
74 $this->cache
->set( $key, $value, 3 );
76 $this->assertEquals( $this->cache
->get( $key ), $value );
80 public function testStaleSet() {
81 $key = wfRandomString();
82 $value = wfRandomString();
83 $this->cache
->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
85 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
89 * @covers WANObjectCache::getWithSetCallback()
91 public function testGetWithSetCallback() {
92 $cache = $this->cache
;
94 $key = wfRandomString();
95 $value = wfRandomString();
96 $cKey1 = wfRandomString();
97 $cKey2 = wfRandomString();
100 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
102 $ttl = 20; // override with another value
107 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
108 $this->assertEquals( $value, $v, "Value returned" );
109 $this->assertEquals( 1, $wasSet, "Value regenerated" );
112 $cache->get( $key, $curTTL );
113 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
114 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
117 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array(
121 $this->assertEquals( $value, $v, "Value returned" );
122 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
124 $priorTime = microtime( true );
127 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
128 $this->assertEquals( $value, $v, "Value returned" );
129 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
130 $t1 = $cache->getCheckKeyTime( $cKey1 );
131 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
132 $t2 = $cache->getCheckKeyTime( $cKey2 );
133 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
135 $priorTime = microtime( true );
137 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
138 $this->assertEquals( $value, $v, "Value returned" );
139 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
140 $t1 = $cache->getCheckKeyTime( $cKey1 );
141 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
142 $t2 = $cache->getCheckKeyTime( $cKey2 );
143 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
146 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
147 $this->assertEquals( $value, $v, "Value returned" );
148 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
151 $key = wfRandomString();
152 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'pcTTL' => 5 ) );
153 $this->assertEquals( $value, $v, "Value returned" );
154 $cache->delete( $key );
155 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'pcTTL' => 5 ) );
156 $this->assertEquals( $value, $v, "Value still returned after deleted" );
157 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
161 * @covers WANObjectCache::getWithSetCallback()
163 public function testLockTSE() {
164 $cache = $this->cache
;
165 $key = wfRandomString();
166 $value = wfRandomString();
169 $func = function() use ( &$calls, $value ) {
174 $cache->delete( $key );
175 $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
176 $this->assertEquals( $value, $ret );
177 $this->assertEquals( 1, $calls, 'Value was populated' );
179 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
180 $this->internalCache
->lock( $key, 0 );
181 $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
182 $this->assertEquals( $value, $ret );
183 $this->assertEquals( 1, $calls, 'Callback was not used' );
187 * @covers WANObjectCache::getMulti()
189 public function testGetMulti() {
190 $cache = $this->cache
;
192 $value1 = array( 'this' => 'is', 'a' => 'test' );
193 $value2 = array( 'this' => 'is', 'another' => 'test' );
195 $key1 = wfRandomString();
196 $key2 = wfRandomString();
197 $key3 = wfRandomString();
199 $cache->set( $key1, $value1, 5 );
200 $cache->set( $key2, $value2, 10 );
204 array( $key1 => $value1, $key2 => $value2 ),
205 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
208 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
209 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
210 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
212 $cKey1 = wfRandomString();
213 $cKey2 = wfRandomString();
216 array( $key1 => $value1, $key2 => $value2 ),
217 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
218 'Result array populated'
221 $priorTime = microtime( true );
225 array( $key1 => $value1, $key2 => $value2 ),
226 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
227 "Result array populated even with new check keys"
229 $t1 = $cache->getCheckKeyTime( $cKey1 );
230 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
231 $t2 = $cache->getCheckKeyTime( $cKey2 );
232 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
233 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
234 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
235 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
240 array( $key1 => $value1, $key2 => $value2 ),
241 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
242 "Result array still populated even with new check keys"
244 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
245 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
246 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
250 * @covers WANObjectCache::delete()
252 public function testDelete() {
253 $key = wfRandomString();
254 $value = wfRandomString();
255 $this->cache
->set( $key, $value );
258 $v = $this->cache
->get( $key, $curTTL );
259 $this->assertEquals( $value, $v, "Key was created with value" );
260 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
262 $this->cache
->delete( $key );
265 $v = $this->cache
->get( $key, $curTTL );
266 $this->assertFalse( $v, "Deleted key has false value" );
267 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
269 $this->cache
->set( $key, $value . 'more' );
270 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
271 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
275 * @covers WANObjectCache::touchCheckKey()
276 * @covers WANObjectCache::resetCheckKey()
277 * @covers WANObjectCache::getCheckKeyTime()
279 public function testTouchKeys() {
280 $key = wfRandomString();
282 $priorTime = microtime( true );
284 $t0 = $this->cache
->getCheckKeyTime( $key );
285 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
287 $priorTime = microtime( true );
289 $this->cache
->touchCheckKey( $key );
290 $t1 = $this->cache
->getCheckKeyTime( $key );
291 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
293 $t2 = $this->cache
->getCheckKeyTime( $key );
294 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
297 $this->cache
->touchCheckKey( $key );
298 $t3 = $this->cache
->getCheckKeyTime( $key );
299 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
301 $t4 = $this->cache
->getCheckKeyTime( $key );
302 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
305 $this->cache
->resetCheckKey( $key );
306 $t5 = $this->cache
->getCheckKeyTime( $key );
307 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
309 $t6 = $this->cache
->getCheckKeyTime( $key );
310 $this->assertEquals( $t5, $t6, 'Check key time did not change' );