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 = LBFactoryMW
::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 $this->setMwGlobals( 'wgDBservers', false );
63 $factory = new LBFactorySimple( [] );
64 $lb = $factory->getMainLB();
66 $dbw = $lb->getConnection( DB_MASTER
);
67 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
69 $dbr = $lb->getConnection( DB_SLAVE
);
70 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
76 public function testLBFactorySimpleServers() {
77 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
79 $this->setMwGlobals( 'wgDBservers', [
81 'host' => $wgDBserver,
82 'dbname' => $wgDBname,
84 'password' => $wgDBpassword,
87 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
90 'host' => $wgDBserver,
91 'dbname' => $wgDBname,
93 'password' => $wgDBpassword,
96 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
100 $factory = new LBFactorySimple( [ 'loadMonitorClass' => 'LoadMonitorNull' ] );
101 $lb = $factory->getMainLB();
103 $dbw = $lb->getConnection( DB_MASTER
);
104 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
106 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
108 $dbr = $lb->getConnection( DB_SLAVE
);
109 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
111 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
113 $factory->shutdown();
117 public function testLBFactoryMulti() {
118 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
120 $factory = new LBFactoryMulti( [
121 'sectionsByDB' => [],
128 'serverTemplate' => [
129 'dbname' => $wgDBname,
131 'password' => $wgDBpassword,
133 'flags' => DBO_DEFAULT
136 'test-db1' => $wgDBserver,
137 'test-db2' => $wgDBserver
139 'loadMonitorClass' => 'LoadMonitorNull'
141 $lb = $factory->getMainLB();
143 $dbw = $lb->getConnection( DB_MASTER
);
144 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
146 $dbr = $lb->getConnection( DB_SLAVE
);
147 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
149 $factory->shutdown();
153 public function testChronologyProtector() {
154 // (a) First HTTP request
155 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
157 $now = microtime( true );
158 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
159 ->disableOriginalConstructor()
161 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
162 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
163 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
165 $lb = $this->getMockBuilder( 'LoadBalancer' )
166 ->disableOriginalConstructor()
168 $lb->method( 'getConnection' )->willReturn( $mockDB );
169 $lb->method( 'getServerCount' )->willReturn( 2 );
170 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
171 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
172 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
173 function () use ( $mockDB ) {
175 $p |
= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
176 $p |
= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
181 $lb->method( 'getMasterPos' )->willReturn( $mPos );
183 $bag = new HashBagOStuff();
184 $cp = new ChronologyProtector(
188 'agent' => "Totally-Not-FireFox"
192 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
193 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
195 // Nothing to wait for
198 $cp->shutdownLB( $lb );
201 // (b) Second HTTP request
202 $cp = new ChronologyProtector(
206 'agent' => "Totally-Not-FireFox"
210 $lb->expects( $this->once() )
211 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
216 $cp->shutdownLB( $lb );