742b73773399f8d81541683d6b293f835926e176
[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
23 /**
24 * @dataProvider provider_testSetAndGet
25 * @covers WANObjectCache::set()
26 * @covers WANObjectCache::get()
27 * @param mixed $value
28 * @param integer $ttl
29 */
30 public function testSetAndGet( $value, $ttl ) {
31 $key = wfRandomString();
32 $this->cache->set( $key, $value, $ttl );
33
34 $curTTL = null;
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" );
38 } else {
39 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
40 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
41 }
42 }
43
44 public static function provider_testSetAndGet() {
45 return array(
46 array( 14141, 3 ),
47 array( 3535.666, 3 ),
48 array( array(), 3 ),
49 array( null, 3 ),
50 array( '0', 3 ),
51 array( (object)array( 'meow' ), 3 ),
52 array( INF, 3 ),
53 array( '', 3 ),
54 array( 'pizzacat', INF ),
55 );
56 }
57
58 public function testGetNotExists() {
59 $key = wfRandomString();
60 $curTTL = null;
61 $value = $this->cache->get( $key, $curTTL );
62
63 $this->assertFalse( $value, "Non-existing key has false value" );
64 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
65 }
66
67 public function testSetOver() {
68 $key = wfRandomString();
69 for ( $i = 0; $i < 3; ++$i ) {
70 $value = wfRandomString();
71 $this->cache->set( $key, $value, 3 );
72
73 $this->assertEquals( $this->cache->get( $key ), $value );
74 }
75 }
76
77 public function testStaleSet() {
78 $key = wfRandomString();
79 $value = wfRandomString();
80 $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
81
82 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
83 }
84
85 /**
86 * @covers WANObjectCache::getWithSetCallback()
87 */
88 public function testGetWithSetCallback() {
89 $cache = $this->cache;
90
91 $key = wfRandomString();
92 $value = wfRandomString();
93 $cKey1 = wfRandomString();
94 $cKey2 = wfRandomString();
95
96 $wasSet = 0;
97 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
98 ++$wasSet;
99 $ttl = 20; // override with another value
100 return $value;
101 };
102
103 $wasSet = 0;
104 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
105 $this->assertEquals( $v, $value );
106 $this->assertEquals( 1, $wasSet, "Value regenerated" );
107
108 $curTTL = null;
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)' );
112
113 $wasSet = 0;
114 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
115 $this->assertEquals( $v, $value );
116 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
117
118 $priorTime = microtime( true );
119 usleep( 1 );
120 $wasSet = 0;
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' );
128
129 $priorTime = microtime( true );
130 $wasSet = 0;
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' );
138
139 $curTTL = null;
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" );
143 }
144
145 /**
146 * @covers WANObjectCache::getMulti()
147 */
148 public function testGetMulti() {
149 $cache = $this->cache;
150
151 $value1 = array( 'this' => 'is', 'a' => 'test' );
152 $value2 = array( 'this' => 'is', 'another' => 'test' );
153
154 $key1 = wfRandomString();
155 $key2 = wfRandomString();
156 $key3 = wfRandomString();
157
158 $cache->set( $key1, $value1, 5 );
159 $cache->set( $key2, $value2, 10 );
160
161 $curTTLs = array();
162 $this->assertEquals(
163 array( $key1 => $value1, $key2 => $value2 ),
164 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
165 );
166
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" );
170
171 $cKey1 = wfRandomString();
172 $cKey2 = wfRandomString();
173 $curTTLs = array();
174 $this->assertEquals(
175 array( $key1 => $value1, $key2 => $value2 ),
176 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
177 'Result array populated'
178 );
179
180 $priorTime = microtime( true );
181 usleep( 1 );
182 $curTTLs = array();
183 $this->assertEquals(
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"
187 );
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' );
195
196 usleep( 1 );
197 $curTTLs = array();
198 $this->assertEquals(
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"
202 );
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' );
206 }
207
208 /**
209 * @covers WANObjectCache::delete()
210 */
211 public function testDelete() {
212 $key = wfRandomString();
213 $value = wfRandomString();
214 $this->cache->set( $key, $value );
215
216 $curTTL = null;
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" );
220
221 $this->cache->delete( $key );
222
223 $curTTL = null;
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" );
227
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" );
231 }
232
233 /**
234 * @covers WANObjectCache::touchCheckKey()
235 * @covers WANObjectCache::resetCheckKey()
236 * @covers WANObjectCache::getCheckKeyTime()
237 */
238 public function testTouchKeys() {
239 $key = wfRandomString();
240
241 $priorTime = microtime( true );
242 usleep( 1 );
243 $t0 = $this->cache->getCheckKeyTime( $key );
244 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
245
246 $priorTime = microtime( true );
247 usleep( 1 );
248 $this->cache->touchCheckKey( $key );
249 $t1 = $this->cache->getCheckKeyTime( $key );
250 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
251
252 $t2 = $this->cache->getCheckKeyTime( $key );
253 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
254
255 usleep( 1 );
256 $this->cache->touchCheckKey( $key );
257 $t3 = $this->cache->getCheckKeyTime( $key );
258 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
259
260 $t4 = $this->cache->getCheckKeyTime( $key );
261 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
262
263 usleep( 1 );
264 $this->cache->resetCheckKey( $key );
265 $t5 = $this->cache->getCheckKeyTime( $key );
266 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
267
268 $t6 = $this->cache->getCheckKeyTime( $key );
269 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
270 }
271 }