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() )
24 * @dataProvider provider_testSetAndGet
25 * @covers WANObjectCache::set()
26 * @covers WANObjectCache::get()
30 public function testSetAndGet( $value, $ttl ) {
31 $key = wfRandomString();
32 $this->cache
->set( $key, $value, $ttl );
35 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL ) );
36 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
37 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
39 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
40 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
44 public static function provider_testSetAndGet() {
51 array( (object)array( 'meow' ), 3 ),
54 array( 'pizzacat', INF
),
58 public function testGetNotExists() {
59 $key = wfRandomString();
61 $value = $this->cache
->get( $key, $curTTL );
63 $this->assertFalse( $value, "Non-existing key has false value" );
64 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
67 public function testSetOver() {
68 $key = wfRandomString();
69 for ( $i = 0; $i < 3; ++
$i ) {
70 $value = wfRandomString();
71 $this->cache
->set( $key, $value, 3 );
73 $this->assertEquals( $this->cache
->get( $key ), $value );
77 public function testStaleSet() {
78 $key = wfRandomString();
79 $value = wfRandomString();
80 $this->cache
->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
82 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
86 * @covers WANObjectCache::getWithSetCallback()
88 public function testGetWithSetCallback() {
89 $cache = $this->cache
;
91 $key = wfRandomString();
92 $value = wfRandomString();
93 $cKey1 = wfRandomString();
94 $cKey2 = wfRandomString();
97 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
99 $ttl = 20; // override with another value
104 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
105 $this->assertEquals( $v, $value );
106 $this->assertEquals( 1, $wasSet, "Value regenerated" );
109 $v = $cache->get( $key, $curTTL );
110 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
111 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
114 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
115 $this->assertEquals( $v, $value );
116 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
118 $priorTime = microtime( true );
121 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
122 $this->assertEquals( $v, $value );
123 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
124 $t1 = $cache->getCheckKeyTime( $cKey1 );
125 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
126 $t2 = $cache->getCheckKeyTime( $cKey2 );
127 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
129 $priorTime = microtime( true );
131 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
132 $this->assertEquals( $v, $value );
133 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
134 $t1 = $cache->getCheckKeyTime( $cKey1 );
135 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
136 $t2 = $cache->getCheckKeyTime( $cKey2 );
137 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
140 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
141 $this->assertEquals( $v, $value );
142 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
146 * @covers WANObjectCache::getMulti()
148 public function testGetMulti() {
149 $cache = $this->cache
;
151 $value1 = array( 'this' => 'is', 'a' => 'test' );
152 $value2 = array( 'this' => 'is', 'another' => 'test' );
154 $key1 = wfRandomString();
155 $key2 = wfRandomString();
156 $key3 = wfRandomString();
158 $cache->set( $key1, $value1, 5 );
159 $cache->set( $key2, $value2, 10 );
163 array( $key1 => $value1, $key2 => $value2 ),
164 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
167 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
168 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
169 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
171 $cKey1 = wfRandomString();
172 $cKey2 = wfRandomString();
175 array( $key1 => $value1, $key2 => $value2 ),
176 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
177 'Result array populated'
180 $priorTime = microtime( true );
184 array( $key1 => $value1, $key2 => $value2 ),
185 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
186 "Result array populated even with new check keys"
188 $t1 = $cache->getCheckKeyTime( $cKey1 );
189 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
190 $t2 = $cache->getCheckKeyTime( $cKey2 );
191 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
192 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
193 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
194 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
199 array( $key1 => $value1, $key2 => $value2 ),
200 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
201 "Result array still populated even with new check keys"
203 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
204 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
205 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
209 * @covers WANObjectCache::delete()
211 public function testDelete() {
212 $key = wfRandomString();
213 $value = wfRandomString();
214 $this->cache
->set( $key, $value );
217 $v = $this->cache
->get( $key, $curTTL );
218 $this->assertEquals( $value, $v, "Key was created with value" );
219 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
221 $this->cache
->delete( $key );
224 $v = $this->cache
->get( $key, $curTTL );
225 $this->assertFalse( $v, "Deleted key has false value" );
226 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
228 $this->cache
->set( $key, $value . 'more' );
229 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
230 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
234 * @covers WANObjectCache::touchCheckKey()
235 * @covers WANObjectCache::resetCheckKey()
236 * @covers WANObjectCache::getCheckKeyTime()
238 public function testTouchKeys() {
239 $key = wfRandomString();
241 $priorTime = microtime( true );
243 $t0 = $this->cache
->getCheckKeyTime( $key );
244 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
246 $priorTime = microtime( true );
248 $this->cache
->touchCheckKey( $key );
249 $t1 = $this->cache
->getCheckKeyTime( $key );
250 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
252 $t2 = $this->cache
->getCheckKeyTime( $key );
253 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
256 $this->cache
->touchCheckKey( $key );
257 $t3 = $this->cache
->getCheckKeyTime( $key );
258 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
260 $t4 = $this->cache
->getCheckKeyTime( $key );
261 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
264 $this->cache
->resetCheckKey( $key );
265 $t5 = $this->cache
->getCheckKeyTime( $key );
266 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
268 $t6 = $this->cache
->getCheckKeyTime( $key );
269 $this->assertEquals( $t5, $t6, 'Check key time did not change' );