[search] Fix method call on null value
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
1 <?php
2
3 class WANObjectCacheTest extends MediaWikiTestCase {
4 /** @var WANObjectCache */
5 private $cache;
6 /**@var BagOStuff */
7 private $internalCache;
8
9 protected function setUp() {
10 parent::setUp();
11
12 if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13 $name = $this->getCliArg( 'use-wanobjectcache' );
14
15 $this->cache = ObjectCache::getWANInstance( $name );
16 } else {
17 $this->cache = new WANObjectCache( array(
18 'cache' => new HashBagOStuff(),
19 'pool' => 'testcache-hash',
20 'relayer' => new EventRelayerNull( array() )
21 ) );
22 }
23
24 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
25 $this->internalCache = $wanCache->cache;
26 }
27
28 /**
29 * @dataProvider provider_testSetAndGet
30 * @covers WANObjectCache::set()
31 * @covers WANObjectCache::get()
32 * @param mixed $value
33 * @param integer $ttl
34 */
35 public function testSetAndGet( $value, $ttl ) {
36 $key = wfRandomString();
37 $this->cache->set( $key, $value, $ttl );
38
39 $curTTL = null;
40 $this->assertEquals( $value, $this->cache->get( $key, $curTTL ) );
41 if ( is_infinite( $ttl ) || $ttl == 0 ) {
42 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
43 } else {
44 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
45 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
46 }
47 }
48
49 public static function provider_testSetAndGet() {
50 return array(
51 array( 14141, 3 ),
52 array( 3535.666, 3 ),
53 array( array(), 3 ),
54 array( null, 3 ),
55 array( '0', 3 ),
56 array( (object)array( 'meow' ), 3 ),
57 array( INF, 3 ),
58 array( '', 3 ),
59 array( 'pizzacat', INF ),
60 );
61 }
62
63 public function testGetNotExists() {
64 $key = wfRandomString();
65 $curTTL = null;
66 $value = $this->cache->get( $key, $curTTL );
67
68 $this->assertFalse( $value, "Non-existing key has false value" );
69 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
70 }
71
72 public function testSetOver() {
73 $key = wfRandomString();
74 for ( $i = 0; $i < 3; ++$i ) {
75 $value = wfRandomString();
76 $this->cache->set( $key, $value, 3 );
77
78 $this->assertEquals( $this->cache->get( $key ), $value );
79 }
80 }
81
82 public function testStaleSet() {
83 $key = wfRandomString();
84 $value = wfRandomString();
85 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
86
87 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
88 }
89
90 /**
91 * @covers WANObjectCache::getWithSetCallback()
92 */
93 public function testGetWithSetCallback() {
94 $cache = $this->cache;
95
96 $key = wfRandomString();
97 $value = wfRandomString();
98 $cKey1 = wfRandomString();
99 $cKey2 = wfRandomString();
100
101 $wasSet = 0;
102 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
103 ++$wasSet;
104 $ttl = 20; // override with another value
105 return $value;
106 };
107
108 $wasSet = 0;
109 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
110 $this->assertEquals( $value, $v, "Value returned" );
111 $this->assertEquals( 1, $wasSet, "Value regenerated" );
112
113 $curTTL = null;
114 $cache->get( $key, $curTTL );
115 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
116 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
117
118 $wasSet = 0;
119 $v = $cache->getWithSetCallback( $key, 30, $func, array(
120 'lowTTL' => 0,
121 'lockTSE' => 5,
122 ) );
123 $this->assertEquals( $value, $v, "Value returned" );
124 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
125
126 $priorTime = microtime( true );
127 usleep( 1 );
128 $wasSet = 0;
129 $v = $cache->getWithSetCallback( $key, 30, $func,
130 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
131 $this->assertEquals( $value, $v, "Value returned" );
132 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
133 $t1 = $cache->getCheckKeyTime( $cKey1 );
134 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
135 $t2 = $cache->getCheckKeyTime( $cKey2 );
136 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
137
138 $priorTime = microtime( true );
139 $wasSet = 0;
140 $v = $cache->getWithSetCallback( $key, 30, $func,
141 array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
142 $this->assertEquals( $value, $v, "Value returned" );
143 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
144 $t1 = $cache->getCheckKeyTime( $cKey1 );
145 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
146 $t2 = $cache->getCheckKeyTime( $cKey2 );
147 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
148
149 $curTTL = null;
150 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
151 $this->assertEquals( $value, $v, "Value returned" );
152 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
153
154 $wasSet = 0;
155 $key = wfRandomString();
156 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
157 $this->assertEquals( $value, $v, "Value returned" );
158 $cache->delete( $key );
159 $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
160 $this->assertEquals( $value, $v, "Value still returned after deleted" );
161 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
162 }
163
164 /**
165 * @covers WANObjectCache::getWithSetCallback()
166 */
167 public function testLockTSE() {
168 $cache = $this->cache;
169 $key = wfRandomString();
170 $value = wfRandomString();
171
172 $calls = 0;
173 $func = function() use ( &$calls, $value ) {
174 ++$calls;
175 return $value;
176 };
177
178 $cache->delete( $key );
179 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
180 $this->assertEquals( $value, $ret );
181 $this->assertEquals( 1, $calls, 'Value was populated' );
182
183 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
184 $this->internalCache->lock( $key, 0 );
185 $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
186 $this->assertEquals( $value, $ret );
187 $this->assertEquals( 1, $calls, 'Callback was not used' );
188 }
189
190 /**
191 * @covers WANObjectCache::getMulti()
192 */
193 public function testGetMulti() {
194 $cache = $this->cache;
195
196 $value1 = array( 'this' => 'is', 'a' => 'test' );
197 $value2 = array( 'this' => 'is', 'another' => 'test' );
198
199 $key1 = wfRandomString();
200 $key2 = wfRandomString();
201 $key3 = wfRandomString();
202
203 $cache->set( $key1, $value1, 5 );
204 $cache->set( $key2, $value2, 10 );
205
206 $curTTLs = array();
207 $this->assertEquals(
208 array( $key1 => $value1, $key2 => $value2 ),
209 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
210 );
211
212 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
213 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
214 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
215
216 $cKey1 = wfRandomString();
217 $cKey2 = wfRandomString();
218 $curTTLs = array();
219 $this->assertEquals(
220 array( $key1 => $value1, $key2 => $value2 ),
221 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
222 'Result array populated'
223 );
224
225 $priorTime = microtime( true );
226 usleep( 1 );
227 $curTTLs = array();
228 $this->assertEquals(
229 array( $key1 => $value1, $key2 => $value2 ),
230 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
231 "Result array populated even with new check keys"
232 );
233 $t1 = $cache->getCheckKeyTime( $cKey1 );
234 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
235 $t2 = $cache->getCheckKeyTime( $cKey2 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
237 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
238 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
239 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
240
241 usleep( 1 );
242 $curTTLs = array();
243 $this->assertEquals(
244 array( $key1 => $value1, $key2 => $value2 ),
245 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
246 "Result array still populated even with new check keys"
247 );
248 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
249 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
250 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
251 }
252
253 /**
254 * @covers WANObjectCache::delete()
255 */
256 public function testDelete() {
257 $key = wfRandomString();
258 $value = wfRandomString();
259 $this->cache->set( $key, $value );
260
261 $curTTL = null;
262 $v = $this->cache->get( $key, $curTTL );
263 $this->assertEquals( $value, $v, "Key was created with value" );
264 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
265
266 $this->cache->delete( $key );
267
268 $curTTL = null;
269 $v = $this->cache->get( $key, $curTTL );
270 $this->assertFalse( $v, "Deleted key has false value" );
271 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
272
273 $this->cache->set( $key, $value . 'more' );
274 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
275 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
276 }
277
278 /**
279 * @covers WANObjectCache::touchCheckKey()
280 * @covers WANObjectCache::resetCheckKey()
281 * @covers WANObjectCache::getCheckKeyTime()
282 */
283 public function testTouchKeys() {
284 $key = wfRandomString();
285
286 $priorTime = microtime( true );
287 usleep( 100 );
288 $t0 = $this->cache->getCheckKeyTime( $key );
289 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
290
291 $priorTime = microtime( true );
292 usleep( 100 );
293 $this->cache->touchCheckKey( $key );
294 $t1 = $this->cache->getCheckKeyTime( $key );
295 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
296
297 $t2 = $this->cache->getCheckKeyTime( $key );
298 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
299
300 usleep( 100 );
301 $this->cache->touchCheckKey( $key );
302 $t3 = $this->cache->getCheckKeyTime( $key );
303 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
304
305 $t4 = $this->cache->getCheckKeyTime( $key );
306 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
307
308 usleep( 100 );
309 $this->cache->resetCheckKey( $key );
310 $t5 = $this->cache->getCheckKeyTime( $key );
311 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
312
313 $t6 = $this->cache->getCheckKeyTime( $key );
314 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
315 }
316 }