3 use Wikimedia\Rdbms\ChronologyProtector
;
6 * Holds tests for LBFactory abstract MediaWiki class.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
25 * @author Antoine Musso
26 * @copyright © 2013 Antoine Musso
27 * @copyright © 2013 Wikimedia Foundation Inc.
29 class LBFactoryTest
extends MediaWikiTestCase
{
32 * @dataProvider getLBFactoryClassProvider
34 public function testGetLBFactoryClass( $expected, $deprecated ) {
35 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
36 ->disableOriginalConstructor()
40 'class' => $deprecated,
41 'connection' => $mockDB,
42 # Various other parameters required:
45 'serverTemplate' => [],
48 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
49 $result = MWLBFactory
::getLBFactoryClass( $config );
51 $this->assertEquals( $expected, $result );
54 public function getLBFactoryClassProvider() {
56 # Format: new class, old class
57 [ 'LBFactorySimple', 'LBFactory_Simple' ],
58 [ 'LBFactorySingle', 'LBFactory_Single' ],
59 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
63 public function testLBFactorySimpleServer() {
64 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
68 'host' => $wgDBserver,
69 'dbname' => $wgDBname,
71 'password' => $wgDBpassword,
73 'dbDirectory' => $wgSQLiteDataDir,
75 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
79 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
80 $lb = $factory->getMainLB();
82 $dbw = $lb->getConnection( DB_MASTER
);
83 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
85 $dbr = $lb->getConnection( DB_SLAVE
);
86 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
92 public function testLBFactorySimpleServers() {
93 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
97 'host' => $wgDBserver,
98 'dbname' => $wgDBname,
100 'password' => $wgDBpassword,
102 'dbDirectory' => $wgSQLiteDataDir,
104 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
107 'host' => $wgDBserver,
108 'dbname' => $wgDBname,
110 'password' => $wgDBpassword,
112 'dbDirectory' => $wgSQLiteDataDir,
114 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
118 $factory = new LBFactorySimple( [
119 'servers' => $servers,
120 'loadMonitorClass' => 'LoadMonitorNull'
122 $lb = $factory->getMainLB();
124 $dbw = $lb->getConnection( DB_MASTER
);
125 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
127 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
128 $dbw->getLBInfo( 'clusterMasterHost' ),
129 'cluster master set' );
131 $dbr = $lb->getConnection( DB_SLAVE
);
132 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
134 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
135 $dbr->getLBInfo( 'clusterMasterHost' ),
136 'cluster master set' );
138 $factory->shutdown();
142 public function testLBFactoryMulti() {
143 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
145 $factory = new LBFactoryMulti( [
146 'sectionsByDB' => [],
153 'serverTemplate' => [
154 'dbname' => $wgDBname,
156 'password' => $wgDBpassword,
158 'dbDirectory' => $wgSQLiteDataDir,
159 'flags' => DBO_DEFAULT
162 'test-db1' => $wgDBserver,
163 'test-db2' => $wgDBserver
165 'loadMonitorClass' => 'LoadMonitorNull'
167 $lb = $factory->getMainLB();
169 $dbw = $lb->getConnection( DB_MASTER
);
170 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
172 $dbr = $lb->getConnection( DB_SLAVE
);
173 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
175 $factory->shutdown();
179 public function testChronologyProtector() {
180 // (a) First HTTP request
181 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
183 $now = microtime( true );
184 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
185 ->disableOriginalConstructor()
187 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
188 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
189 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
191 $lb = $this->getMockBuilder( 'LoadBalancer' )
192 ->disableOriginalConstructor()
194 $lb->method( 'getConnection' )->willReturn( $mockDB );
195 $lb->method( 'getServerCount' )->willReturn( 2 );
196 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
197 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
198 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
199 function () use ( $mockDB ) {
201 $p |
= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
202 $p |
= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
207 $lb->method( 'getMasterPos' )->willReturn( $mPos );
209 $bag = new HashBagOStuff();
210 $cp = new ChronologyProtector(
214 'agent' => "Totally-Not-FireFox"
218 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
219 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
221 // Nothing to wait for
224 $cp->shutdownLB( $lb );
227 // (b) Second HTTP request
228 $cp = new ChronologyProtector(
232 'agent' => "Totally-Not-FireFox"
236 $lb->expects( $this->once() )
237 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
242 $cp->shutdownLB( $lb );
246 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
247 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir;
249 return new LBFactoryMulti( $baseOverride +
[
250 'sectionsByDB' => [],
256 'serverTemplate' => $serverOverride +
[
257 'dbname' => $wgDBname,
259 'password' => $wgDBpassword,
261 'dbDirectory' => $wgSQLiteDataDir,
262 'flags' => DBO_DEFAULT
265 'test-db1' => $wgDBserver,
267 'loadMonitorClass' => 'LoadMonitorNull',
268 'localDomain' => wfWikiID()
272 public function testNiceDomains() {
273 global $wgDBname, $wgDBtype;
275 if ( $wgDBtype === 'sqlite' ) {
276 $tmpDir = $this->getNewTempDirectory();
277 $dbPath = "$tmpDir/unit_test_db.sqlite";
278 file_put_contents( $dbPath, '' );
279 $tempFsFile = new TempFSFile( $dbPath );
280 $tempFsFile->autocollect();
285 $factory = $this->newLBFactoryMulti(
287 [ 'dbFilePath' => $dbPath ]
289 $lb = $factory->getMainLB();
291 if ( $wgDBtype !== 'sqlite' ) {
292 $db = $lb->getConnectionRef( DB_MASTER
);
300 /** @var Database $db */
301 $db = $lb->getConnection( DB_MASTER
, [], '' );
302 $lb->reuseConnection( $db ); // don't care
310 $this->quoteTable( $db, 'page' ),
311 $db->tableName( 'page' ),
312 "Correct full table name"
316 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
317 $db->tableName( "$wgDBname.page" ),
318 "Correct full table name"
322 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
323 $db->tableName( 'nice_db.page' ),
324 "Correct full table name"
327 $factory->setDomainPrefix( 'my_' );
333 $this->quoteTable( $db, 'my_page' ),
334 $db->tableName( 'page' ),
335 "Correct full table name"
338 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
339 $db->tableName( 'other_nice_db.page' ),
340 "Correct full table name"
343 $factory->closeAll();
347 public function testTrickyDomain() {
350 if ( $wgDBtype === 'sqlite' ) {
351 $tmpDir = $this->getNewTempDirectory();
352 $dbPath = "$tmpDir/unit_test_db.sqlite";
353 file_put_contents( $dbPath, '' );
354 $tempFsFile = new TempFSFile( $dbPath );
355 $tempFsFile->autocollect();
360 $dbname = 'unittest-domain';
361 $factory = $this->newLBFactoryMulti(
362 [ 'localDomain' => $dbname ],
363 [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ]
365 $lb = $factory->getMainLB();
366 /** @var Database $db */
367 $db = $lb->getConnection( DB_MASTER
, [], '' );
368 $lb->reuseConnection( $db ); // don't care
376 $this->quoteTable( $db, 'page' ),
377 $db->tableName( 'page' ),
378 "Correct full table name"
382 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
383 $db->tableName( "$dbname.page" ),
384 "Correct full table name"
388 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
389 $db->tableName( 'nice_db.page' ),
390 "Correct full table name"
393 $factory->setDomainPrefix( 'my_' );
396 $this->quoteTable( $db, 'my_page' ),
397 $db->tableName( 'page' ),
398 "Correct full table name"
401 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
402 $db->tableName( 'other_nice_db.page' ),
403 "Correct full table name"
406 \MediaWiki\
suppressWarnings();
407 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
408 \MediaWiki\restoreWarnings
();
411 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
412 $db->tableName( 'garbage-db.page' ),
413 "Correct full table name"
416 $factory->closeAll();
420 private function quoteTable( Database
$db, $table ) {
421 if ( $db->getType() === 'sqlite' ) {
424 return $db->addIdentifierQuotes( $table );