Add process cache support to WANObjectCache
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / WANObjectCacheTest.php
1 <?php
2
3 class WANObjectCacheTest extends MediaWikiTestCase {
4 /** @var WANObjectCache */
5 private $cache;
6
7 protected function setUp() {
8 parent::setUp();
9
10 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
11 $name = $this->getCliArg( 'use-wanobjectcache' );
12
13 $this->cache = ObjectCache::getWANInstance( $name );
14 } else {
15 $this->cache = new WANObjectCache( array(
16 'cache' => new HashBagOStuff(),
17 'pool' => 'testcache-hash',
18 'relayer' => new EventRelayerNull( array() )
19 ) );
20 }
21
22 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
23 $this->internalCache = $wanCache->cache;
24 }
25
26 /**
27 * @dataProvider provider_testSetAndGet
28 * @covers WANObjectCache::set()
29 * @covers WANObjectCache::get()
30 * @param mixed $value
31 * @param integer $ttl
32 */
33 public function testSetAndGet( $value, $ttl ) {
34 $key = wfRandomString();
35 $this->cache->set( $key, $value, $ttl );
36
37 $curTTL = null;
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" );
41 } else {
42 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
43 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
44 }
45 }
46
47 public static function provider_testSetAndGet() {
48 return array(
49 array( 14141, 3 ),
50 array( 3535.666, 3 ),
51 array( array(), 3 ),
52 array( null, 3 ),
53 array( '0', 3 ),
54 array( (object)array( 'meow' ), 3 ),
55 array( INF, 3 ),
56 array( '', 3 ),
57 array( 'pizzacat', INF ),
58 );
59 }
60
61 public function testGetNotExists() {
62 $key = wfRandomString();
63 $curTTL = null;
64 $value = $this->cache->get( $key, $curTTL );
65
66 $this->assertFalse( $value, "Non-existing key has false value" );
67 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
68 }
69
70 public function testSetOver() {
71 $key = wfRandomString();
72 for ( $i = 0; $i < 3; ++$i ) {
73 $value = wfRandomString();
74 $this->cache->set( $key, $value, 3 );
75
76 $this->assertEquals( $this->cache->get( $key ), $value );
77 }
78 }
79
80 public function testStaleSet() {
81 $key = wfRandomString();
82 $value = wfRandomString();
83 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
84
85 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
86 }
87
88 /**
89 * @covers WANObjectCache::getWithSetCallback()
90 */
91 public function testGetWithSetCallback() {
92 $cache = $this->cache;
93
94 $key = wfRandomString();
95 $value = wfRandomString();
96 $cKey1 = wfRandomString();
97 $cKey2 = wfRandomString();
98
99 $wasSet = 0;
100 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
101 ++$wasSet;
102 $ttl = 20; // override with another value
103 return $value;
104 };
105
106 $wasSet = 0;
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" );
110
111 $curTTL = null;
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)' );
115
116 $wasSet = 0;
117 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array(
118 'lowTTL' => 0,
119 'lockTSE' => 5,
120 ) );
121 $this->assertEquals( $value, $v, "Value returned" );
122 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
123
124 $priorTime = microtime( true );
125 usleep( 1 );
126 $wasSet = 0;
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' );
134
135 $priorTime = microtime( true );
136 $wasSet = 0;
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' );
144
145 $curTTL = null;
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" );
149
150 $wasSet = 0;
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" );
158 }
159
160 /**
161 * @covers WANObjectCache::getWithSetCallback()
162 */
163 public function testLockTSE() {
164 $cache = $this->cache;
165 $key = wfRandomString();
166 $value = wfRandomString();
167
168 $calls = 0;
169 $func = function() use ( &$calls, $value ) {
170 ++$calls;
171 return $value;
172 };
173
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' );
178
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' );
184 }
185
186 /**
187 * @covers WANObjectCache::getMulti()
188 */
189 public function testGetMulti() {
190 $cache = $this->cache;
191
192 $value1 = array( 'this' => 'is', 'a' => 'test' );
193 $value2 = array( 'this' => 'is', 'another' => 'test' );
194
195 $key1 = wfRandomString();
196 $key2 = wfRandomString();
197 $key3 = wfRandomString();
198
199 $cache->set( $key1, $value1, 5 );
200 $cache->set( $key2, $value2, 10 );
201
202 $curTTLs = array();
203 $this->assertEquals(
204 array( $key1 => $value1, $key2 => $value2 ),
205 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
206 );
207
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" );
211
212 $cKey1 = wfRandomString();
213 $cKey2 = wfRandomString();
214 $curTTLs = array();
215 $this->assertEquals(
216 array( $key1 => $value1, $key2 => $value2 ),
217 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
218 'Result array populated'
219 );
220
221 $priorTime = microtime( true );
222 usleep( 1 );
223 $curTTLs = array();
224 $this->assertEquals(
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"
228 );
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' );
236
237 usleep( 1 );
238 $curTTLs = array();
239 $this->assertEquals(
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"
243 );
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' );
247 }
248
249 /**
250 * @covers WANObjectCache::delete()
251 */
252 public function testDelete() {
253 $key = wfRandomString();
254 $value = wfRandomString();
255 $this->cache->set( $key, $value );
256
257 $curTTL = null;
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" );
261
262 $this->cache->delete( $key );
263
264 $curTTL = null;
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" );
268
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" );
272 }
273
274 /**
275 * @covers WANObjectCache::touchCheckKey()
276 * @covers WANObjectCache::resetCheckKey()
277 * @covers WANObjectCache::getCheckKeyTime()
278 */
279 public function testTouchKeys() {
280 $key = wfRandomString();
281
282 $priorTime = microtime( true );
283 usleep( 100 );
284 $t0 = $this->cache->getCheckKeyTime( $key );
285 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
286
287 $priorTime = microtime( true );
288 usleep( 100 );
289 $this->cache->touchCheckKey( $key );
290 $t1 = $this->cache->getCheckKeyTime( $key );
291 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
292
293 $t2 = $this->cache->getCheckKeyTime( $key );
294 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
295
296 usleep( 100 );
297 $this->cache->touchCheckKey( $key );
298 $t3 = $this->cache->getCheckKeyTime( $key );
299 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
300
301 $t4 = $this->cache->getCheckKeyTime( $key );
302 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
303
304 usleep( 100 );
305 $this->cache->resetCheckKey( $key );
306 $t5 = $this->cache->getCheckKeyTime( $key );
307 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
308
309 $t6 = $this->cache->getCheckKeyTime( $key );
310 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
311 }
312 }