Fix DatabaseSqlite IDEA warnings
[mediawiki.git] / tests / phpunit / includes / db / LBFactoryTest.php
blobaed2d83a7d7f5b35f1e39bd5af2da0c865e0b954
1 <?php
2 /**
3 * Holds tests for LBFactory abstract MediaWiki class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @group Database
21 * @file
22 * @author Antoine Musso
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Wikimedia Foundation Inc.
26 class LBFactoryTest extends MediaWikiTestCase {
28 /**
29 * @dataProvider getLBFactoryClassProvider
31 public function testGetLBFactoryClass( $expected, $deprecated ) {
32 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
33 ->disableOriginalConstructor()
34 ->getMock();
36 $config = [
37 'class' => $deprecated,
38 'connection' => $mockDB,
39 # Various other parameters required:
40 'sectionsByDB' => [],
41 'sectionLoads' => [],
42 'serverTemplate' => [],
45 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
46 $result = MWLBFactory::getLBFactoryClass( $config );
48 $this->assertEquals( $expected, $result );
51 public function getLBFactoryClassProvider() {
52 return [
53 # Format: new class, old class
54 [ 'LBFactorySimple', 'LBFactory_Simple' ],
55 [ 'LBFactorySingle', 'LBFactory_Single' ],
56 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
60 public function testLBFactorySimpleServer() {
61 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
63 $servers = [
65 'host' => $wgDBserver,
66 'dbname' => $wgDBname,
67 'user' => $wgDBuser,
68 'password' => $wgDBpassword,
69 'type' => $wgDBtype,
70 'load' => 0,
71 'flags' => DBO_TRX // REPEATABLE-READ for consistency
75 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
76 $lb = $factory->getMainLB();
78 $dbw = $lb->getConnection( DB_MASTER );
79 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
81 $dbr = $lb->getConnection( DB_SLAVE );
82 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
84 $factory->shutdown();
85 $lb->closeAll();
88 public function testLBFactorySimpleServers() {
89 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
91 $servers = [
92 [ // master
93 'host' => $wgDBserver,
94 'dbname' => $wgDBname,
95 'user' => $wgDBuser,
96 'password' => $wgDBpassword,
97 'type' => $wgDBtype,
98 'load' => 0,
99 'flags' => DBO_TRX // REPEATABLE-READ for consistency
101 [ // emulated slave
102 'host' => $wgDBserver,
103 'dbname' => $wgDBname,
104 'user' => $wgDBuser,
105 'password' => $wgDBpassword,
106 'type' => $wgDBtype,
107 'load' => 100,
108 'flags' => DBO_TRX // REPEATABLE-READ for consistency
112 $factory = new LBFactorySimple( [
113 'servers' => $servers,
114 'loadMonitorClass' => 'LoadMonitorNull'
115 ] );
116 $lb = $factory->getMainLB();
118 $dbw = $lb->getConnection( DB_MASTER );
119 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
120 $this->assertEquals(
121 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
123 $dbr = $lb->getConnection( DB_SLAVE );
124 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
125 $this->assertEquals(
126 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
128 $factory->shutdown();
129 $lb->closeAll();
132 public function testLBFactoryMulti() {
133 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
135 $factory = new LBFactoryMulti( [
136 'sectionsByDB' => [],
137 'sectionLoads' => [
138 'DEFAULT' => [
139 'test-db1' => 0,
140 'test-db2' => 100,
143 'serverTemplate' => [
144 'dbname' => $wgDBname,
145 'user' => $wgDBuser,
146 'password' => $wgDBpassword,
147 'type' => $wgDBtype,
148 'flags' => DBO_DEFAULT
150 'hostsByName' => [
151 'test-db1' => $wgDBserver,
152 'test-db2' => $wgDBserver
154 'loadMonitorClass' => 'LoadMonitorNull'
155 ] );
156 $lb = $factory->getMainLB();
158 $dbw = $lb->getConnection( DB_MASTER );
159 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
161 $dbr = $lb->getConnection( DB_SLAVE );
162 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
164 $factory->shutdown();
165 $lb->closeAll();
168 public function testChronologyProtector() {
169 // (a) First HTTP request
170 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
172 $now = microtime( true );
173 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
174 ->disableOriginalConstructor()
175 ->getMock();
176 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
177 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
178 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
180 $lb = $this->getMockBuilder( 'LoadBalancer' )
181 ->disableOriginalConstructor()
182 ->getMock();
183 $lb->method( 'getConnection' )->willReturn( $mockDB );
184 $lb->method( 'getServerCount' )->willReturn( 2 );
185 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
186 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
187 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
188 function () use ( $mockDB ) {
189 $p = 0;
190 $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
191 $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
193 return (bool)$p;
195 ) );
196 $lb->method( 'getMasterPos' )->willReturn( $mPos );
198 $bag = new HashBagOStuff();
199 $cp = new ChronologyProtector(
200 $bag,
202 'ip' => '127.0.0.1',
203 'agent' => "Totally-Not-FireFox"
207 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
208 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
210 // Nothing to wait for
211 $cp->initLB( $lb );
212 // Record in stash
213 $cp->shutdownLB( $lb );
214 $cp->shutdown();
216 // (b) Second HTTP request
217 $cp = new ChronologyProtector(
218 $bag,
220 'ip' => '127.0.0.1',
221 'agent' => "Totally-Not-FireFox"
225 $lb->expects( $this->once() )
226 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
228 // Wait
229 $cp->initLB( $lb );
230 // Record in stash
231 $cp->shutdownLB( $lb );
232 $cp->shutdown();
235 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
236 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
238 return new LBFactoryMulti( $baseOverride + [
239 'sectionsByDB' => [],
240 'sectionLoads' => [
241 'DEFAULT' => [
242 'test-db1' => 1,
245 'serverTemplate' => $serverOverride + [
246 'dbname' => $wgDBname,
247 'user' => $wgDBuser,
248 'password' => $wgDBpassword,
249 'type' => $wgDBtype,
250 'flags' => DBO_DEFAULT
252 'hostsByName' => [
253 'test-db1' => $wgDBserver,
255 'loadMonitorClass' => 'LoadMonitorNull',
256 'localDomain' => wfWikiID()
257 ] );
260 public function testNiceDomains() {
261 global $wgDBname;
263 $factory = $this->newLBFactoryMulti();
264 $lb = $factory->getMainLB();
266 $db = $lb->getConnectionRef( DB_MASTER );
267 $this->assertEquals(
268 $wgDBname,
269 $db->getDomainID()
271 unset( $db );
273 /** @var Database $db */
274 $db = $lb->getConnection( DB_MASTER, [], '' );
275 $lb->reuseConnection( $db ); // don't care
277 $this->assertEquals(
279 $db->getDomainID()
282 $this->assertEquals(
283 $db->addIdentifierQuotes( 'page' ),
284 $db->tableName( 'page' ),
285 "Correct full table name"
288 $this->assertEquals(
289 $db->addIdentifierQuotes( $wgDBname ) . '.' . $db->addIdentifierQuotes( 'page' ),
290 $db->tableName( "$wgDBname.page" ),
291 "Correct full table name"
294 $this->assertEquals(
295 $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
296 $db->tableName( 'nice_db.page' ),
297 "Correct full table name"
300 $factory->setDomainPrefix( 'my_' );
301 $this->assertEquals(
303 $db->getDomainID()
305 $this->assertEquals(
306 $db->addIdentifierQuotes( 'my_page' ),
307 $db->tableName( 'page' ),
308 "Correct full table name"
310 $this->assertEquals(
311 $db->addIdentifierQuotes( 'other_nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
312 $db->tableName( 'other_nice_db.page' ),
313 "Correct full table name"
316 $factory->closeAll();
317 $factory->destroy();
320 public function testTrickyDomain() {
321 $dbname = 'unittest-domain';
322 $factory = $this->newLBFactoryMulti(
323 [ 'localDomain' => $dbname ], [ 'dbname' => $dbname ] );
324 $lb = $factory->getMainLB();
325 /** @var Database $db */
326 $db = $lb->getConnection( DB_MASTER, [], '' );
327 $lb->reuseConnection( $db ); // don't care
329 $this->assertEquals(
331 $db->getDomainID()
334 $this->assertEquals(
335 $db->addIdentifierQuotes( 'page' ),
336 $db->tableName( 'page' ),
337 "Correct full table name"
340 $this->assertEquals(
341 $db->addIdentifierQuotes( $dbname ) . '.' . $db->addIdentifierQuotes( 'page' ),
342 $db->tableName( "$dbname.page" ),
343 "Correct full table name"
346 $this->assertEquals(
347 $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
348 $db->tableName( 'nice_db.page' ),
349 "Correct full table name"
352 $factory->setDomainPrefix( 'my_' );
354 $this->assertEquals(
355 $db->addIdentifierQuotes( 'my_page' ),
356 $db->tableName( 'page' ),
357 "Correct full table name"
359 $this->assertEquals(
360 $db->addIdentifierQuotes( 'other_nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
361 $db->tableName( 'other_nice_db.page' ),
362 "Correct full table name"
365 \MediaWiki\suppressWarnings();
366 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
367 \MediaWiki\restoreWarnings();
369 $this->assertEquals(
370 $db->addIdentifierQuotes( 'garbage-db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
371 $db->tableName( 'garbage-db.page' ),
372 "Correct full table name"
375 $factory->closeAll();
376 $factory->destroy();