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