WANObjectCache: Change getWithSetCallback() signature to key/ttl/callback/opts
[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( $v, $value );
109 $this->assertEquals( 1, $wasSet, "Value regenerated" );
110
111 $curTTL = null;
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)' );
115
116 $wasSet = 0;
117 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array(
118 'lowTTL' => 0,
119 'lockTSE' => 5,
120 ) );
121 $this->assertEquals( $v, $value );
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( $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' );
134
135 $priorTime = microtime( true );
136 $wasSet = 0;
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' );
144
145 $curTTL = null;
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" );
149 }
150
151 /**
152 * @covers WANObjectCache::getWithSetCallback()
153 */
154 public function testLockTSE() {
155 $cache = $this->cache;
156 $key = wfRandomString();
157 $value = wfRandomString();
158
159 $calls = 0;
160 $func = function() use ( &$calls, $value ) {
161 ++$calls;
162 return $value;
163 };
164
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' );
169
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' );
175 }
176
177 /**
178 * @covers WANObjectCache::getMulti()
179 */
180 public function testGetMulti() {
181 $cache = $this->cache;
182
183 $value1 = array( 'this' => 'is', 'a' => 'test' );
184 $value2 = array( 'this' => 'is', 'another' => 'test' );
185
186 $key1 = wfRandomString();
187 $key2 = wfRandomString();
188 $key3 = wfRandomString();
189
190 $cache->set( $key1, $value1, 5 );
191 $cache->set( $key2, $value2, 10 );
192
193 $curTTLs = array();
194 $this->assertEquals(
195 array( $key1 => $value1, $key2 => $value2 ),
196 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
197 );
198
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" );
202
203 $cKey1 = wfRandomString();
204 $cKey2 = wfRandomString();
205 $curTTLs = array();
206 $this->assertEquals(
207 array( $key1 => $value1, $key2 => $value2 ),
208 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
209 'Result array populated'
210 );
211
212 $priorTime = microtime( true );
213 usleep( 1 );
214 $curTTLs = array();
215 $this->assertEquals(
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"
219 );
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' );
227
228 usleep( 1 );
229 $curTTLs = array();
230 $this->assertEquals(
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"
234 );
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' );
238 }
239
240 /**
241 * @covers WANObjectCache::delete()
242 */
243 public function testDelete() {
244 $key = wfRandomString();
245 $value = wfRandomString();
246 $this->cache->set( $key, $value );
247
248 $curTTL = null;
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" );
252
253 $this->cache->delete( $key );
254
255 $curTTL = null;
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" );
259
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" );
263 }
264
265 /**
266 * @covers WANObjectCache::touchCheckKey()
267 * @covers WANObjectCache::resetCheckKey()
268 * @covers WANObjectCache::getCheckKeyTime()
269 */
270 public function testTouchKeys() {
271 $key = wfRandomString();
272
273 $priorTime = microtime( true );
274 usleep( 100 );
275 $t0 = $this->cache->getCheckKeyTime( $key );
276 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
277
278 $priorTime = microtime( true );
279 usleep( 100 );
280 $this->cache->touchCheckKey( $key );
281 $t1 = $this->cache->getCheckKeyTime( $key );
282 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
283
284 $t2 = $this->cache->getCheckKeyTime( $key );
285 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
286
287 usleep( 100 );
288 $this->cache->touchCheckKey( $key );
289 $t3 = $this->cache->getCheckKeyTime( $key );
290 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
291
292 $t4 = $this->cache->getCheckKeyTime( $key );
293 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
294
295 usleep( 100 );
296 $this->cache->resetCheckKey( $key );
297 $t5 = $this->cache->getCheckKeyTime( $key );
298 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
299
300 $t6 = $this->cache->getCheckKeyTime( $key );
301 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
302 }
303 }