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:
40 'sectionsByDB' => array(),
41 'sectionLoads' => array(),
42 'serverTemplate' => array(),
45 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
46 $result = LBFactory
::getLBFactoryClass( $config );
48 $this->assertEquals( $expected, $result );
51 public function getLBFactoryClassProvider() {
53 # Format: new class, old class
54 array( 'LBFactorySimple', 'LBFactory_Simple' ),
55 array( 'LBFactorySingle', 'LBFactory_Single' ),
56 array( 'LBFactoryMulti', 'LBFactory_Multi' ),
57 array( 'LBFactoryFake', 'LBFactory_Fake' ),
61 public function testLBFactorySimpleServer() {
62 $this->setMwGlobals( 'wgDBservers', false );
64 $factory = new LBFactorySimple( array() );
65 $lb = $factory->getMainLB();
67 $dbw = $lb->getConnection( DB_MASTER
);
68 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
70 $dbr = $lb->getConnection( DB_SLAVE
);
71 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
77 public function testLBFactorySimpleServers() {
78 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
80 $this->setMwGlobals( 'wgDBservers', array(
82 'host' => $wgDBserver,
83 'dbname' => $wgDBname,
85 'password' => $wgDBpassword,
88 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
90 array( // emulated slave
91 'host' => $wgDBserver,
92 'dbname' => $wgDBname,
94 'password' => $wgDBpassword,
97 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
101 $factory = new LBFactorySimple( array( 'loadMonitorClass' => 'LoadMonitorNull' ) );
102 $lb = $factory->getMainLB();
104 $dbw = $lb->getConnection( DB_MASTER
);
105 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
107 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
109 $dbr = $lb->getConnection( DB_SLAVE
);
110 $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as slave' );
112 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
114 $factory->shutdown();
118 public function testLBFactoryMulti() {
119 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
121 $factory = new LBFactoryMulti( array(
122 'sectionsByDB' => array(),
123 'sectionLoads' => array(
129 'serverTemplate' => array(
130 'dbname' => $wgDBname,
132 'password' => $wgDBpassword,
134 'flags' => DBO_DEFAULT
136 'hostsByName' => array(
137 'test-db1' => $wgDBserver,
138 'test-db2' => $wgDBserver
140 'loadMonitorClass' => 'LoadMonitorNull'
142 $lb = $factory->getMainLB();
144 $dbw = $lb->getConnection( DB_MASTER
);
145 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
147 $dbr = $lb->getConnection( DB_SLAVE
);
148 $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as slave' );
150 $factory->shutdown();
154 public function testChronologyProtector() {
155 // (a) First HTTP request
156 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
158 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
159 ->disableOriginalConstructor()
161 $mockDB->expects( $this->any() )
162 ->method( 'doneWrites' )->will( $this->returnValue( true ) );
163 $mockDB->expects( $this->any() )
164 ->method( 'getMasterPos' )->will( $this->returnValue( $mPos ) );
166 $lb = $this->getMockBuilder( 'LoadBalancer' )
167 ->disableOriginalConstructor()
169 $lb->expects( $this->any() )
170 ->method( 'getConnection' )->will( $this->returnValue( $mockDB ) );
171 $lb->expects( $this->any() )
172 ->method( 'getServerCount' )->will( $this->returnValue( 2 ) );
173 $lb->expects( $this->any() )
174 ->method( 'parentInfo' )->will( $this->returnValue( array( 'id' => "main-DEFAULT" ) ) );
175 $lb->expects( $this->any() )
176 ->method( 'getAnyOpenConnection' )->will( $this->returnValue( $mockDB ) );
178 $bag = new HashBagOStuff();
179 $cp = new ChronologyProtector(
183 'agent' => "Totally-Not-FireFox"
187 $mockDB->expects( $this->exactly( 2 ) )->method( 'doneWrites' );
189 // Nothing to wait for
192 $cp->shutdownLB( $lb );
195 // (b) Second HTTP request
196 $cp = new ChronologyProtector(
200 'agent' => "Totally-Not-FireFox"
204 $lb->expects( $this->once() )
205 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
210 $cp->shutdownLB( $lb );