objectcache: Fix flaky unit test
[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(
115 'lowTTL' => 0,
116 'lockTSE' => 5,
117 ) );
118 $this->assertEquals( $v, $value );
119 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
120
121 $priorTime = microtime( true );
122 usleep( 1 );
123 $wasSet = 0;
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' );
131
132 $priorTime = microtime( true );
133 $wasSet = 0;
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' );
141
142 $curTTL = null;
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" );
146 }
147
148 /**
149 * @covers WANObjectCache::getMulti()
150 */
151 public function testGetMulti() {
152 $cache = $this->cache;
153
154 $value1 = array( 'this' => 'is', 'a' => 'test' );
155 $value2 = array( 'this' => 'is', 'another' => 'test' );
156
157 $key1 = wfRandomString();
158 $key2 = wfRandomString();
159 $key3 = wfRandomString();
160
161 $cache->set( $key1, $value1, 5 );
162 $cache->set( $key2, $value2, 10 );
163
164 $curTTLs = array();
165 $this->assertEquals(
166 array( $key1 => $value1, $key2 => $value2 ),
167 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
168 );
169
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" );
173
174 $cKey1 = wfRandomString();
175 $cKey2 = wfRandomString();
176 $curTTLs = array();
177 $this->assertEquals(
178 array( $key1 => $value1, $key2 => $value2 ),
179 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
180 'Result array populated'
181 );
182
183 $priorTime = microtime( true );
184 usleep( 1 );
185 $curTTLs = array();
186 $this->assertEquals(
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"
190 );
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' );
198
199 usleep( 1 );
200 $curTTLs = array();
201 $this->assertEquals(
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"
205 );
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' );
209 }
210
211 /**
212 * @covers WANObjectCache::delete()
213 */
214 public function testDelete() {
215 $key = wfRandomString();
216 $value = wfRandomString();
217 $this->cache->set( $key, $value );
218
219 $curTTL = null;
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" );
223
224 $this->cache->delete( $key );
225
226 $curTTL = null;
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" );
230
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" );
234 }
235
236 /**
237 * @covers WANObjectCache::touchCheckKey()
238 * @covers WANObjectCache::resetCheckKey()
239 * @covers WANObjectCache::getCheckKeyTime()
240 */
241 public function testTouchKeys() {
242 $key = wfRandomString();
243
244 $priorTime = microtime( true );
245 usleep( 100 );
246 $t0 = $this->cache->getCheckKeyTime( $key );
247 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
248
249 $priorTime = microtime( true );
250 usleep( 100 );
251 $this->cache->touchCheckKey( $key );
252 $t1 = $this->cache->getCheckKeyTime( $key );
253 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
254
255 $t2 = $this->cache->getCheckKeyTime( $key );
256 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
257
258 usleep( 100 );
259 $this->cache->touchCheckKey( $key );
260 $t3 = $this->cache->getCheckKeyTime( $key );
261 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
262
263 $t4 = $this->cache->getCheckKeyTime( $key );
264 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
265
266 usleep( 100 );
267 $this->cache->resetCheckKey( $key );
268 $t5 = $this->cache->getCheckKeyTime( $key );
269 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
270
271 $t6 = $this->cache->getCheckKeyTime( $key );
272 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
273 }
274 }