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( $v, $value );
109 $this->assertEquals( 1, $wasSet, "Value regenerated" );
112 $v = $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( $v, $value );
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( $v, $value );
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( $v, $value );
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( $v, $value );
148 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
152 * @covers WANObjectCache::getWithSetCallback()
154 public function testLockTSE() {
155 $cache = $this->cache
;
156 $key = wfRandomString();
157 $value = wfRandomString();
160 $func = function() use ( &$calls, $value ) {
165 $cache->delete( $key );
166 $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
167 $this->assertEquals( $value, $ret );
168 $this->assertEquals( 1, $calls, 'Value was populated' );
170 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
171 $this->internalCache
->lock( $key, 0 );
172 $ret = $cache->getWithSetCallback( $key, 30, $func, array(), array( 'lockTSE' => 5 ) );
173 $this->assertEquals( $value, $ret );
174 $this->assertEquals( 1, $calls, 'Callback was not used' );
178 * @covers WANObjectCache::getMulti()
180 public function testGetMulti() {
181 $cache = $this->cache
;
183 $value1 = array( 'this' => 'is', 'a' => 'test' );
184 $value2 = array( 'this' => 'is', 'another' => 'test' );
186 $key1 = wfRandomString();
187 $key2 = wfRandomString();
188 $key3 = wfRandomString();
190 $cache->set( $key1, $value1, 5 );
191 $cache->set( $key2, $value2, 10 );
195 array( $key1 => $value1, $key2 => $value2 ),
196 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
199 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
200 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
201 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
203 $cKey1 = wfRandomString();
204 $cKey2 = wfRandomString();
207 array( $key1 => $value1, $key2 => $value2 ),
208 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
209 'Result array populated'
212 $priorTime = microtime( true );
216 array( $key1 => $value1, $key2 => $value2 ),
217 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
218 "Result array populated even with new check keys"
220 $t1 = $cache->getCheckKeyTime( $cKey1 );
221 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
222 $t2 = $cache->getCheckKeyTime( $cKey2 );
223 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
224 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
225 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
226 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
231 array( $key1 => $value1, $key2 => $value2 ),
232 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
233 "Result array still populated even with new check keys"
235 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
236 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
237 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
241 * @covers WANObjectCache::delete()
243 public function testDelete() {
244 $key = wfRandomString();
245 $value = wfRandomString();
246 $this->cache
->set( $key, $value );
249 $v = $this->cache
->get( $key, $curTTL );
250 $this->assertEquals( $value, $v, "Key was created with value" );
251 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
253 $this->cache
->delete( $key );
256 $v = $this->cache
->get( $key, $curTTL );
257 $this->assertFalse( $v, "Deleted key has false value" );
258 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
260 $this->cache
->set( $key, $value . 'more' );
261 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
262 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
266 * @covers WANObjectCache::touchCheckKey()
267 * @covers WANObjectCache::resetCheckKey()
268 * @covers WANObjectCache::getCheckKeyTime()
270 public function testTouchKeys() {
271 $key = wfRandomString();
273 $priorTime = microtime( true );
275 $t0 = $this->cache
->getCheckKeyTime( $key );
276 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
278 $priorTime = microtime( true );
280 $this->cache
->touchCheckKey( $key );
281 $t1 = $this->cache
->getCheckKeyTime( $key );
282 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
284 $t2 = $this->cache
->getCheckKeyTime( $key );
285 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
288 $this->cache
->touchCheckKey( $key );
289 $t3 = $this->cache
->getCheckKeyTime( $key );
290 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
292 $t4 = $this->cache
->getCheckKeyTime( $key );
293 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
296 $this->cache
->resetCheckKey( $key );
297 $t5 = $this->cache
->getCheckKeyTime( $key );
298 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
300 $t6 = $this->cache
->getCheckKeyTime( $key );
301 $this->assertEquals( $t5, $t6, 'Check key time did not change' );