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(
118 $this->assertEquals( $v, $value );
119 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
121 $priorTime = microtime( true );
124 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
125 $this->assertEquals( $v, $value );
126 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
127 $t1 = $cache->getCheckKeyTime( $cKey1 );
128 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
129 $t2 = $cache->getCheckKeyTime( $cKey2 );
130 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
132 $priorTime = microtime( true );
134 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
135 $this->assertEquals( $v, $value );
136 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
137 $t1 = $cache->getCheckKeyTime( $cKey1 );
138 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
139 $t2 = $cache->getCheckKeyTime( $cKey2 );
140 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
143 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
144 $this->assertEquals( $v, $value );
145 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
149 * @covers WANObjectCache::getMulti()
151 public function testGetMulti() {
152 $cache = $this->cache
;
154 $value1 = array( 'this' => 'is', 'a' => 'test' );
155 $value2 = array( 'this' => 'is', 'another' => 'test' );
157 $key1 = wfRandomString();
158 $key2 = wfRandomString();
159 $key3 = wfRandomString();
161 $cache->set( $key1, $value1, 5 );
162 $cache->set( $key2, $value2, 10 );
166 array( $key1 => $value1, $key2 => $value2 ),
167 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
170 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
171 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
172 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
174 $cKey1 = wfRandomString();
175 $cKey2 = wfRandomString();
178 array( $key1 => $value1, $key2 => $value2 ),
179 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
180 'Result array populated'
183 $priorTime = microtime( true );
187 array( $key1 => $value1, $key2 => $value2 ),
188 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
189 "Result array populated even with new check keys"
191 $t1 = $cache->getCheckKeyTime( $cKey1 );
192 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
193 $t2 = $cache->getCheckKeyTime( $cKey2 );
194 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
195 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
196 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
197 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
202 array( $key1 => $value1, $key2 => $value2 ),
203 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
204 "Result array still populated even with new check keys"
206 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
207 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
208 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
212 * @covers WANObjectCache::delete()
214 public function testDelete() {
215 $key = wfRandomString();
216 $value = wfRandomString();
217 $this->cache
->set( $key, $value );
220 $v = $this->cache
->get( $key, $curTTL );
221 $this->assertEquals( $value, $v, "Key was created with value" );
222 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
224 $this->cache
->delete( $key );
227 $v = $this->cache
->get( $key, $curTTL );
228 $this->assertFalse( $v, "Deleted key has false value" );
229 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
231 $this->cache
->set( $key, $value . 'more' );
232 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
233 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
237 * @covers WANObjectCache::touchCheckKey()
238 * @covers WANObjectCache::resetCheckKey()
239 * @covers WANObjectCache::getCheckKeyTime()
241 public function testTouchKeys() {
242 $key = wfRandomString();
244 $priorTime = microtime( true );
246 $t0 = $this->cache
->getCheckKeyTime( $key );
247 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
249 $priorTime = microtime( true );
251 $this->cache
->touchCheckKey( $key );
252 $t1 = $this->cache
->getCheckKeyTime( $key );
253 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
255 $t2 = $this->cache
->getCheckKeyTime( $key );
256 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
259 $this->cache
->touchCheckKey( $key );
260 $t3 = $this->cache
->getCheckKeyTime( $key );
261 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
263 $t4 = $this->cache
->getCheckKeyTime( $key );
264 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
267 $this->cache
->resetCheckKey( $key );
268 $t5 = $this->cache
->getCheckKeyTime( $key );
269 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
271 $t6 = $this->cache
->getCheckKeyTime( $key );
272 $this->assertEquals( $t5, $t6, 'Check key time did not change' );