Don't check namespace in SpecialWantedtemplates
[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 /**
78 * @covers WANObjectCache::getWithSetCallback()
79 */
80 public function testGetWithSetCallback() {
81 $cache = $this->cache;
82
83 $key = wfRandomString();
84 $value = wfRandomString();
85 $cKey1 = wfRandomString();
86 $cKey2 = wfRandomString();
87
88 $wasSet = 0;
89 $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
90 ++$wasSet;
91 $ttl = 20; // override with another value
92 return $value;
93 };
94
95 $wasSet = 0;
96 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
97 $this->assertEquals( $v, $value );
98 $this->assertEquals( 1, $wasSet, "Value regenerated" );
99
100 $curTTL = null;
101 $v = $cache->get( $key, $curTTL );
102 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
103 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
104
105 $wasSet = 0;
106 $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
107 $this->assertEquals( $v, $value );
108 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
109
110 $priorTime = microtime( true );
111 usleep( 1 );
112 $wasSet = 0;
113 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
114 $this->assertEquals( $v, $value );
115 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
116 $t1 = $cache->getCheckKeyTime( $cKey1 );
117 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
118 $t2 = $cache->getCheckKeyTime( $cKey2 );
119 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
120
121 $priorTime = microtime( true );
122 $wasSet = 0;
123 $v = $cache->getWithSetCallback( $key, $func, 30, array( $cKey1, $cKey2 ) );
124 $this->assertEquals( $v, $value );
125 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
126 $t1 = $cache->getCheckKeyTime( $cKey1 );
127 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
128 $t2 = $cache->getCheckKeyTime( $cKey2 );
129 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
130
131 $curTTL = null;
132 $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
133 $this->assertEquals( $v, $value );
134 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
135 }
136
137 /**
138 * @covers WANObjectCache::getMulti()
139 */
140 public function testGetMulti() {
141 $cache = $this->cache;
142
143 $value1 = array( 'this' => 'is', 'a' => 'test' );
144 $value2 = array( 'this' => 'is', 'another' => 'test' );
145
146 $key1 = wfRandomString();
147 $key2 = wfRandomString();
148 $key3 = wfRandomString();
149
150 $cache->set( $key1, $value1, 5 );
151 $cache->set( $key2, $value2, 10 );
152
153 $curTTLs = array();
154 $this->assertEquals(
155 array( $key1 => $value1, $key2 => $value2 ),
156 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs )
157 );
158
159 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
160 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
161 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
162
163 $cKey1 = wfRandomString();
164 $cKey2 = wfRandomString();
165 $curTTLs = array();
166 $this->assertEquals(
167 array( $key1 => $value1, $key2 => $value2 ),
168 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
169 'Result array populated'
170 );
171
172 $priorTime = microtime( true );
173 usleep( 1 );
174 $curTTLs = array();
175 $this->assertEquals(
176 array( $key1 => $value1, $key2 => $value2 ),
177 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
178 "Result array populated even with new check keys"
179 );
180 $t1 = $cache->getCheckKeyTime( $cKey1 );
181 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
182 $t2 = $cache->getCheckKeyTime( $cKey2 );
183 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
184 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
185 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
186 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
187
188 usleep( 1 );
189 $curTTLs = array();
190 $this->assertEquals(
191 array( $key1 => $value1, $key2 => $value2 ),
192 $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
193 "Result array still populated even with new check keys"
194 );
195 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
196 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
197 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
198 }
199
200 /**
201 * @covers WANObjectCache::delete()
202 */
203 public function testDelete() {
204 $key = wfRandomString();
205 $value = wfRandomString();
206 $this->cache->set( $key, $value );
207
208 $curTTL = null;
209 $v = $this->cache->get( $key, $curTTL );
210 $this->assertEquals( $value, $v, "Key was created with value" );
211 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
212
213 $this->cache->delete( $key );
214
215 $curTTL = null;
216 $v = $this->cache->get( $key, $curTTL );
217 $this->assertFalse( $v, "Deleted key has false value" );
218 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
219
220 $this->cache->set( $key, $value . 'more' );
221 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
222 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
223 }
224
225 /**
226 * @covers WANObjectCache::touchCheckKey()
227 * @covers WANObjectCache::getCheckKeyTime()
228 */
229 public function testTouchKeys() {
230 $key = wfRandomString();
231
232 $t0 = $this->cache->getCheckKeyTime( $key );
233 $this->assertFalse( $t0, 'Check key time is false' );
234
235 $priorTime = microtime( true );
236 usleep( 1 );
237 $this->cache->touchCheckKey( $key );
238 $t1 = $this->cache->getCheckKeyTime( $key );
239 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
240
241 $t2 = $this->cache->getCheckKeyTime( $key );
242 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
243
244 usleep( 1 );
245 $this->cache->touchCheckKey( $key );
246 $t3 = $this->cache->getCheckKeyTime( $key );
247 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
248
249 $t4 = $this->cache->getCheckKeyTime( $key );
250 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
251 }
252 }