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
22 * @author Antoine Musso
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Wikimedia Foundation Inc.
26 class LBFactoryTest
extends MediaWikiTestCase
{
29 * @dataProvider getLBFactoryClassProvider
31 public function testGetLBFactoryClass( $expected, $deprecated ) {
32 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
33 ->disableOriginalConstructor()
37 'class' => $deprecated,
38 'connection' => $mockDB,
39 # Various other parameters required:
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() {
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;
65 'host' => $wgDBserver,
66 'dbname' => $wgDBname,
68 'password' => $wgDBpassword,
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' );
88 public function testLBFactorySimpleServers() {
89 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
93 'host' => $wgDBserver,
94 'dbname' => $wgDBname,
96 'password' => $wgDBpassword,
99 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
102 'host' => $wgDBserver,
103 'dbname' => $wgDBname,
105 'password' => $wgDBpassword,
108 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
112 $factory = new LBFactorySimple( [
113 'servers' => $servers,
114 'loadMonitorClass' => 'LoadMonitorNull'
116 $lb = $factory->getMainLB();
118 $dbw = $lb->getConnection( DB_MASTER
);
119 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
121 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
123 $dbr = $lb->getConnection( DB_SLAVE
);
124 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
126 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
128 $factory->shutdown();
132 public function testLBFactoryMulti() {
133 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
135 $factory = new LBFactoryMulti( [
136 'sectionsByDB' => [],
143 'serverTemplate' => [
144 'dbname' => $wgDBname,
146 'password' => $wgDBpassword,
148 'flags' => DBO_DEFAULT
151 'test-db1' => $wgDBserver,
152 'test-db2' => $wgDBserver
154 'loadMonitorClass' => 'LoadMonitorNull'
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();
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()
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()
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 ) {
190 $p |
= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
191 $p |
= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
196 $lb->method( 'getMasterPos' )->willReturn( $mPos );
198 $bag = new HashBagOStuff();
199 $cp = new ChronologyProtector(
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
213 $cp->shutdownLB( $lb );
216 // (b) Second HTTP request
217 $cp = new ChronologyProtector(
221 'agent' => "Totally-Not-FireFox"
225 $lb->expects( $this->once() )
226 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
231 $cp->shutdownLB( $lb );
235 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
236 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
238 return new LBFactoryMulti( $baseOverride +
[
239 'sectionsByDB' => [],
245 'serverTemplate' => $serverOverride +
[
246 'dbname' => $wgDBname,
248 'password' => $wgDBpassword,
250 'flags' => DBO_DEFAULT
253 'test-db1' => $wgDBserver,
255 'loadMonitorClass' => 'LoadMonitorNull',
256 'localDomain' => wfWikiID()
260 public function testNiceDomains() {
263 $factory = $this->newLBFactoryMulti();
264 $lb = $factory->getMainLB();
266 $db = $lb->getConnectionRef( DB_MASTER
);
273 /** @var Database $db */
274 $db = $lb->getConnection( DB_MASTER
, [], '' );
275 $lb->reuseConnection( $db ); // don't care
283 $db->addIdentifierQuotes( 'page' ),
284 $db->tableName( 'page' ),
285 "Correct full table name"
289 $db->addIdentifierQuotes( $wgDBname ) . '.' . $db->addIdentifierQuotes( 'page' ),
290 $db->tableName( "$wgDBname.page" ),
291 "Correct full table name"
295 $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
296 $db->tableName( 'nice_db.page' ),
297 "Correct full table name"
300 $factory->setDomainPrefix( 'my_' );
306 $db->addIdentifierQuotes( 'my_page' ),
307 $db->tableName( 'page' ),
308 "Correct full table name"
311 $db->addIdentifierQuotes( 'other_nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
312 $db->tableName( 'other_nice_db.page' ),
313 "Correct full table name"
316 $factory->closeAll();
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
335 $db->addIdentifierQuotes( 'page' ),
336 $db->tableName( 'page' ),
337 "Correct full table name"
341 $db->addIdentifierQuotes( $dbname ) . '.' . $db->addIdentifierQuotes( 'page' ),
342 $db->tableName( "$dbname.page" ),
343 "Correct full table name"
347 $db->addIdentifierQuotes( 'nice_db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
348 $db->tableName( 'nice_db.page' ),
349 "Correct full table name"
352 $factory->setDomainPrefix( 'my_' );
355 $db->addIdentifierQuotes( 'my_page' ),
356 $db->tableName( 'page' ),
357 "Correct full table name"
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
();
370 $db->addIdentifierQuotes( 'garbage-db' ) . '.' . $db->addIdentifierQuotes( 'page' ),
371 $db->tableName( 'garbage-db.page' ),
372 "Correct full table name"
375 $factory->closeAll();