Merge "Prepare for REL1_33 cut, labelling master as 1.34-alpha"
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / NameTableStoreTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use BagOStuff;
6 use EmptyBagOStuff;
7 use HashBagOStuff;
8 use MediaWiki\Storage\NameTableAccessException;
9 use MediaWiki\Storage\NameTableStore;
10 use MediaWikiTestCase;
11 use Psr\Log\NullLogger;
12 use WANObjectCache;
13 use Wikimedia\Rdbms\Database;
14 use Wikimedia\Rdbms\LoadBalancer;
15 use Wikimedia\TestingAccessWrapper;
16
17 /**
18 * @author Addshore
19 * @group Database
20 * @covers \MediaWiki\Storage\NameTableStore
21 */
22 class NameTableStoreTest extends MediaWikiTestCase {
23
24 public function setUp() {
25 $this->tablesUsed[] = 'slot_roles';
26 parent::setUp();
27 }
28
29 protected function addCoreDBData() {
30 // The default implementation causes the slot_roles to already have content. Skip that.
31 }
32
33 private function populateTable( $values ) {
34 $insertValues = [];
35 foreach ( $values as $name ) {
36 $insertValues[] = [ 'role_name' => $name ];
37 }
38 $this->db->insert( 'slot_roles', $insertValues );
39 }
40
41 private function getHashWANObjectCache( $cacheBag ) {
42 return new WANObjectCache( [ 'cache' => $cacheBag ] );
43 }
44
45 /**
46 * @param $db
47 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
48 */
49 private function getMockLoadBalancer( $db ) {
50 $mock = $this->getMockBuilder( LoadBalancer::class )
51 ->disableOriginalConstructor()
52 ->getMock();
53 $mock->expects( $this->any() )
54 ->method( 'getConnection' )
55 ->willReturn( $db );
56 return $mock;
57 }
58
59 private function getCallCheckingDb( $insertCalls, $selectCalls ) {
60 $mock = $this->getMockBuilder( Database::class )
61 ->disableOriginalConstructor()
62 ->getMock();
63 $mock->expects( $this->exactly( $insertCalls ) )
64 ->method( 'insert' )
65 ->willReturnCallback( function () {
66 return call_user_func_array( [ $this->db, 'insert' ], func_get_args() );
67 } );
68 $mock->expects( $this->exactly( $selectCalls ) )
69 ->method( 'select' )
70 ->willReturnCallback( function () {
71 return call_user_func_array( [ $this->db, 'select' ], func_get_args() );
72 } );
73 $mock->expects( $this->exactly( $insertCalls ) )
74 ->method( 'affectedRows' )
75 ->willReturnCallback( function () {
76 return call_user_func_array( [ $this->db, 'affectedRows' ], func_get_args() );
77 } );
78 $mock->expects( $this->any() )
79 ->method( 'insertId' )
80 ->willReturnCallback( function () {
81 return call_user_func_array( [ $this->db, 'insertId' ], func_get_args() );
82 } );
83 return $mock;
84 }
85
86 private function getNameTableSqlStore(
87 BagOStuff $cacheBag,
88 $insertCalls,
89 $selectCalls,
90 $normalizationCallback = null,
91 $insertCallback = null
92 ) {
93 return new NameTableStore(
94 $this->getMockLoadBalancer( $this->getCallCheckingDb( $insertCalls, $selectCalls ) ),
95 $this->getHashWANObjectCache( $cacheBag ),
96 new NullLogger(),
97 'slot_roles', 'role_id', 'role_name',
98 $normalizationCallback,
99 false,
100 $insertCallback
101 );
102 }
103
104 public function provideGetAndAcquireId() {
105 return [
106 'no wancache, empty table' =>
107 [ new EmptyBagOStuff(), true, 1, [], 'foo', 1 ],
108 'no wancache, one matching value' =>
109 [ new EmptyBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
110 'no wancache, one not matching value' =>
111 [ new EmptyBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
112 'no wancache, multiple, one matching value' =>
113 [ new EmptyBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
114 'no wancache, multiple, no matching value' =>
115 [ new EmptyBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
116 'wancache, empty table' =>
117 [ new HashBagOStuff(), true, 1, [], 'foo', 1 ],
118 'wancache, one matching value' =>
119 [ new HashBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ],
120 'wancache, one not matching value' =>
121 [ new HashBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ],
122 'wancache, multiple, one matching value' =>
123 [ new HashBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ],
124 'wancache, multiple, no matching value' =>
125 [ new HashBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ],
126 ];
127 }
128
129 /**
130 * @dataProvider provideGetAndAcquireId
131 * @param BagOStuff $cacheBag to use in the WANObjectCache service
132 * @param bool $needsInsert Does the value we are testing need to be inserted?
133 * @param int $selectCalls Number of times the select DB method will be called
134 * @param string[] $existingValues to be added to the db table
135 * @param string $name name to acquire
136 * @param int $expectedId the id we expect the name to have
137 */
138 public function testGetAndAcquireId(
139 $cacheBag,
140 $needsInsert,
141 $selectCalls,
142 $existingValues,
143 $name,
144 $expectedId
145 ) {
146 // Make sure the table is empty!
147 $this->truncateTable( 'slot_roles' );
148
149 $this->populateTable( $existingValues );
150 $store = $this->getNameTableSqlStore( $cacheBag, (int)$needsInsert, $selectCalls );
151
152 // Some names will not initially exist
153 try {
154 $result = $store->getId( $name );
155 $this->assertSame( $expectedId, $result );
156 } catch ( NameTableAccessException $e ) {
157 if ( $needsInsert ) {
158 $this->assertTrue( true ); // Expected exception
159 } else {
160 $this->fail( 'Did not expect an exception, but got one: ' . $e->getMessage() );
161 }
162 }
163
164 // All names should return their id here
165 $this->assertSame( $expectedId, $store->acquireId( $name ) );
166
167 // acquireId inserted these names, so now everything should exist with getId
168 $this->assertSame( $expectedId, $store->getId( $name ) );
169
170 // calling getId again will also still work, and not result in more selects
171 $this->assertSame( $expectedId, $store->getId( $name ) );
172 }
173
174 public function provideTestGetAndAcquireIdNameNormalization() {
175 yield [ 'A', 'a', 'strtolower' ];
176 yield [ 'b', 'B', 'strtoupper' ];
177 yield [
178 'X',
179 'X',
180 function ( $name ) {
181 return $name;
182 }
183 ];
184 yield [ 'ZZ', 'ZZ-a', __CLASS__ . '::appendDashAToString' ];
185 }
186
187 public static function appendDashAToString( $string ) {
188 return $string . '-a';
189 }
190
191 /**
192 * @dataProvider provideTestGetAndAcquireIdNameNormalization
193 */
194 public function testGetAndAcquireIdNameNormalization(
195 $nameIn,
196 $nameOut,
197 $normalizationCallback
198 ) {
199 $store = $this->getNameTableSqlStore(
200 new EmptyBagOStuff(),
201 1,
202 1,
203 $normalizationCallback
204 );
205 $acquiredId = $store->acquireId( $nameIn );
206 $this->assertSame( $nameOut, $store->getName( $acquiredId ) );
207 }
208
209 public function provideGetName() {
210 return [
211 [ new HashBagOStuff(), 3, 2 ],
212 [ new EmptyBagOStuff(), 3, 3 ],
213 ];
214 }
215
216 /**
217 * @dataProvider provideGetName
218 */
219 public function testGetName( BagOStuff $cacheBag, $insertCalls, $selectCalls ) {
220 $now = microtime( true );
221 $cacheBag->setMockTime( $now );
222 // Check for operations to in-memory cache (IMC) and persistent cache (PC)
223 $store = $this->getNameTableSqlStore( $cacheBag, $insertCalls, $selectCalls );
224
225 // Get 1 ID and make sure getName returns correctly
226 $fooId = $store->acquireId( 'foo' ); // regen PC, set IMC, update IMC, tombstone PC
227 $now += 0.01;
228 $this->assertSame( 'foo', $store->getName( $fooId ) ); // use IMC
229 $now += 0.01;
230
231 // Get another ID and make sure getName returns correctly
232 $barId = $store->acquireId( 'bar' ); // update IMC, tombstone PC
233 $now += 0.01;
234 $this->assertSame( 'bar', $store->getName( $barId ) ); // use IMC
235 $now += 0.01;
236
237 // Blitz the cache and make sure it still returns
238 TestingAccessWrapper::newFromObject( $store )->tableCache = null; // clear IMC
239 $this->assertSame( 'foo', $store->getName( $fooId ) ); // regen interim PC, set IMC
240 $this->assertSame( 'bar', $store->getName( $barId ) ); // use IMC
241
242 // Blitz the cache again and get another ID and make sure getName returns correctly
243 TestingAccessWrapper::newFromObject( $store )->tableCache = null; // clear IMC
244 $bazId = $store->acquireId( 'baz' ); // set IMC using interim PC, update IMC, tombstone PC
245 $now += 0.01;
246 $this->assertSame( 'baz', $store->getName( $bazId ) ); // uses IMC
247 $this->assertSame( 'baz', $store->getName( $bazId ) ); // uses IMC
248 }
249
250 public function testGetName_masterFallback() {
251 $store = $this->getNameTableSqlStore( new EmptyBagOStuff(), 1, 2 );
252
253 // Insert a new name
254 $fooId = $store->acquireId( 'foo' );
255
256 // Empty the process cache, getCachedTable() will now return this empty array
257 TestingAccessWrapper::newFromObject( $store )->tableCache = [];
258
259 // getName should fallback to master, which is why we assert 2 selectCalls above
260 $this->assertSame( 'foo', $store->getName( $fooId ) );
261 }
262
263 public function testGetMap_empty() {
264 $this->populateTable( [] );
265 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
266 $table = $store->getMap();
267 $this->assertSame( [], $table );
268 }
269
270 public function testGetMap_twoValues() {
271 $this->populateTable( [ 'foo', 'bar' ] );
272 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 );
273
274 // We are using a cache, so 2 calls should only result in 1 select on the db
275 $store->getMap();
276 $table = $store->getMap();
277
278 $expected = [ 1 => 'foo', 2 => 'bar' ];
279 $this->assertSame( $expected, $table );
280 // Make sure the table returned is the same as the cached table
281 $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $store )->tableCache );
282 }
283
284 public function testReloadMap() {
285 $this->populateTable( [ 'foo' ] );
286 $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 2 );
287
288 // force load
289 $this->assertCount( 1, $store->getMap() );
290
291 // add more stuff to the table, so the cache gets out of sync
292 $this->populateTable( [ 'bar' ] );
293
294 $expected = [ 1 => 'foo', 2 => 'bar' ];
295 $this->assertSame( $expected, $store->reloadMap() );
296 $this->assertSame( $expected, $store->getMap() );
297 }
298
299 public function testCacheRaceCondition() {
300 $wanHashBag = new HashBagOStuff();
301 $store1 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
302 $store2 = $this->getNameTableSqlStore( $wanHashBag, 1, 0 );
303 $store3 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 );
304
305 // Cache the current table in the instances we will use
306 // This simulates multiple requests running simultaneously
307 $store1->getMap();
308 $store2->getMap();
309 $store3->getMap();
310
311 // Store 2 separate names using different instances
312 $fooId = $store1->acquireId( 'foo' );
313 $barId = $store2->acquireId( 'bar' );
314
315 // Each of these instances should be aware of what they have inserted
316 $this->assertSame( $fooId, $store1->acquireId( 'foo' ) );
317 $this->assertSame( $barId, $store2->acquireId( 'bar' ) );
318
319 // A new store should be able to get both of these new Ids
320 // Note: before there was a race condition here where acquireId( 'bar' ) would update the
321 // cache with data missing the 'foo' key that it was not aware of
322 $store4 = $this->getNameTableSqlStore( $wanHashBag, 0, 1 );
323 $this->assertSame( $fooId, $store4->getId( 'foo' ) );
324 $this->assertSame( $barId, $store4->getId( 'bar' ) );
325
326 // If a store with old cached data tries to acquire these we will get the same ids.
327 $this->assertSame( $fooId, $store3->acquireId( 'foo' ) );
328 $this->assertSame( $barId, $store3->acquireId( 'bar' ) );
329 }
330
331 public function testGetAndAcquireIdInsertCallback() {
332 // FIXME: fails under postgres
333 $this->markTestSkippedIfDbType( 'postgres' );
334
335 $store = $this->getNameTableSqlStore(
336 new EmptyBagOStuff(),
337 1,
338 1,
339 null,
340 function ( $insertFields ) {
341 $insertFields['role_id'] = 7251;
342 return $insertFields;
343 }
344 );
345 $this->assertSame( 7251, $store->acquireId( 'A' ) );
346 }
347
348 }