2013-11-27 10:17:06 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Holds tests for LBFactory abstract MediaWiki class.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright © 2013 Antoine Musso
|
|
|
|
|
* @copyright © 2013 Wikimedia Foundation Inc.
|
|
|
|
|
*/
|
2017-12-28 08:28:45 +00:00
|
|
|
|
|
|
|
|
use Wikimedia\Rdbms\LBFactorySimple;
|
|
|
|
|
use Wikimedia\Rdbms\LBFactoryMulti;
|
|
|
|
|
use Wikimedia\Rdbms\ChronologyProtector;
|
|
|
|
|
use Wikimedia\Rdbms\MySQLMasterPos;
|
2018-01-12 21:44:12 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseDomain;
|
2017-12-28 08:28:45 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
* @covers \Wikimedia\Rdbms\LBFactorySimple
|
|
|
|
|
* @covers \Wikimedia\Rdbms\LBFactoryMulti
|
|
|
|
|
*/
|
2013-11-27 10:17:06 +00:00
|
|
|
class LBFactoryTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-28 08:28:45 +00:00
|
|
|
* @covers MWLBFactory::getLBFactoryClass
|
2013-12-29 21:00:39 +00:00
|
|
|
* @dataProvider getLBFactoryClassProvider
|
2013-11-27 10:17:06 +00:00
|
|
|
*/
|
2013-12-29 21:00:39 +00:00
|
|
|
public function testGetLBFactoryClass( $expected, $deprecated ) {
|
2016-12-31 11:52:16 +00:00
|
|
|
$mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
|
2013-12-29 21:00:39 +00:00
|
|
|
->disableOriginalConstructor()
|
2013-11-27 10:17:06 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$config = [
|
2013-12-29 21:00:39 +00:00
|
|
|
'class' => $deprecated,
|
|
|
|
|
'connection' => $mockDB,
|
|
|
|
|
# Various other parameters required:
|
2016-02-17 09:09:32 +00:00
|
|
|
'sectionsByDB' => [],
|
|
|
|
|
'sectionLoads' => [],
|
|
|
|
|
'serverTemplate' => [],
|
|
|
|
|
];
|
2013-11-27 10:17:06 +00:00
|
|
|
|
|
|
|
|
$this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
|
2016-09-30 21:06:02 +00:00
|
|
|
$result = MWLBFactory::getLBFactoryClass( $config );
|
2013-12-29 21:00:39 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
2013-11-27 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-29 21:00:39 +00:00
|
|
|
public function getLBFactoryClassProvider() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-11-27 10:17:06 +00:00
|
|
|
# Format: new class, old class
|
2017-01-26 18:27:37 +00:00
|
|
|
[ Wikimedia\Rdbms\LBFactorySimple::class, 'LBFactory_Simple' ],
|
|
|
|
|
[ Wikimedia\Rdbms\LBFactorySingle::class, 'LBFactory_Single' ],
|
|
|
|
|
[ Wikimedia\Rdbms\LBFactoryMulti::class, 'LBFactory_Multi' ],
|
|
|
|
|
[ Wikimedia\Rdbms\LBFactorySimple::class, 'LBFactorySimple' ],
|
|
|
|
|
[ Wikimedia\Rdbms\LBFactorySingle::class, 'LBFactorySingle' ],
|
|
|
|
|
[ Wikimedia\Rdbms\LBFactoryMulti::class, 'LBFactoryMulti' ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-27 10:17:06 +00:00
|
|
|
}
|
2015-09-30 23:06:47 +00:00
|
|
|
|
|
|
|
|
public function testLBFactorySimpleServer() {
|
2016-10-27 05:44:08 +00:00
|
|
|
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
|
2016-09-15 18:52:55 +00:00
|
|
|
|
|
|
|
|
$servers = [
|
|
|
|
|
[
|
2016-10-27 05:44:08 +00:00
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 0,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
2016-09-15 18:52:55 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-09-30 23:06:47 +00:00
|
|
|
|
2016-09-15 18:52:55 +00:00
|
|
|
$factory = new LBFactorySimple( [ 'servers' => $servers ] );
|
2015-09-30 23:06:47 +00:00
|
|
|
$lb = $factory->getMainLB();
|
|
|
|
|
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
|
|
|
|
|
|
2017-08-04 18:53:34 +00:00
|
|
|
$dbr = $lb->getConnection( DB_REPLICA );
|
|
|
|
|
$this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
|
2015-09-30 23:06:47 +00:00
|
|
|
|
|
|
|
|
$factory->shutdown();
|
|
|
|
|
$lb->closeAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLBFactorySimpleServers() {
|
2016-10-27 05:44:08 +00:00
|
|
|
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
|
2015-09-30 23:06:47 +00:00
|
|
|
|
2016-09-15 18:52:55 +00:00
|
|
|
$servers = [
|
2016-02-17 09:09:32 +00:00
|
|
|
[ // master
|
2016-10-27 05:44:08 +00:00
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 0,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // emulated slave
|
2016-10-27 05:44:08 +00:00
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 100,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2016-09-15 18:52:55 +00:00
|
|
|
];
|
2015-09-30 23:06:47 +00:00
|
|
|
|
2016-09-15 18:52:55 +00:00
|
|
|
$factory = new LBFactorySimple( [
|
|
|
|
|
'servers' => $servers,
|
|
|
|
|
'loadMonitorClass' => 'LoadMonitorNull'
|
|
|
|
|
] );
|
2015-09-30 23:06:47 +00:00
|
|
|
$lb = $factory->getMainLB();
|
|
|
|
|
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
|
2015-12-04 00:37:56 +00:00
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
|
|
|
|
|
$dbw->getLBInfo( 'clusterMasterHost' ),
|
|
|
|
|
'cluster master set' );
|
2015-09-30 23:06:47 +00:00
|
|
|
|
2017-08-04 18:53:34 +00:00
|
|
|
$dbr = $lb->getConnection( DB_REPLICA );
|
2016-09-05 19:19:00 +00:00
|
|
|
$this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
|
2015-12-04 00:37:56 +00:00
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
|
|
|
|
|
$dbr->getLBInfo( 'clusterMasterHost' ),
|
|
|
|
|
'cluster master set' );
|
2015-09-30 23:06:47 +00:00
|
|
|
|
|
|
|
|
$factory->shutdown();
|
|
|
|
|
$lb->closeAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLBFactoryMulti() {
|
2016-10-27 05:44:08 +00:00
|
|
|
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
|
2015-09-30 23:06:47 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$factory = new LBFactoryMulti( [
|
|
|
|
|
'sectionsByDB' => [],
|
|
|
|
|
'sectionLoads' => [
|
|
|
|
|
'DEFAULT' => [
|
2015-09-30 23:06:47 +00:00
|
|
|
'test-db1' => 0,
|
|
|
|
|
'test-db2' => 100,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'serverTemplate' => [
|
2017-02-25 21:53:36 +00:00
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
2016-10-27 05:44:08 +00:00
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
2017-02-25 21:53:36 +00:00
|
|
|
'flags' => DBO_DEFAULT
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'hostsByName' => [
|
2015-09-30 23:06:47 +00:00
|
|
|
'test-db1' => $wgDBserver,
|
|
|
|
|
'test-db2' => $wgDBserver
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-09-30 23:06:47 +00:00
|
|
|
'loadMonitorClass' => 'LoadMonitorNull'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-09-30 23:06:47 +00:00
|
|
|
$lb = $factory->getMainLB();
|
|
|
|
|
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
|
|
|
|
|
|
2017-08-04 18:53:34 +00:00
|
|
|
$dbr = $lb->getConnection( DB_REPLICA );
|
2016-09-05 19:19:00 +00:00
|
|
|
$this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
|
2015-09-30 23:06:47 +00:00
|
|
|
|
|
|
|
|
$factory->shutdown();
|
|
|
|
|
$lb->closeAll();
|
|
|
|
|
}
|
2015-10-18 19:53:40 +00:00
|
|
|
|
2017-12-28 08:28:45 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \Wikimedia\Rdbms\ChronologyProtector
|
|
|
|
|
*/
|
2015-10-18 19:53:40 +00:00
|
|
|
public function testChronologyProtector() {
|
|
|
|
|
// (a) First HTTP request
|
2017-12-08 21:36:25 +00:00
|
|
|
$m1Pos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
|
|
|
|
|
$m2Pos = new MySQLMasterPos( 'db1064-bin.002400', '794074907' );
|
2015-10-18 19:53:40 +00:00
|
|
|
|
2016-09-08 20:07:15 +00:00
|
|
|
$now = microtime( true );
|
2017-12-08 21:36:25 +00:00
|
|
|
|
|
|
|
|
// Master DB 1
|
|
|
|
|
$mockDB1 = $this->getMockBuilder( 'DatabaseMysqli' )
|
2015-10-18 19:53:40 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-12-08 21:36:25 +00:00
|
|
|
$mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
|
|
|
|
|
$mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
|
|
|
|
|
$mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
|
|
|
|
|
// Load balancer for master DB 1
|
|
|
|
|
$lb1 = $this->getMockBuilder( 'LoadBalancer' )
|
2015-10-18 19:53:40 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-12-08 21:36:25 +00:00
|
|
|
$lb1->method( 'getConnection' )->willReturn( $mockDB1 );
|
|
|
|
|
$lb1->method( 'getServerCount' )->willReturn( 2 );
|
|
|
|
|
$lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
|
|
|
|
|
$lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
|
|
|
|
|
function () use ( $mockDB1 ) {
|
2016-09-08 20:07:15 +00:00
|
|
|
$p = 0;
|
2017-12-08 21:36:25 +00:00
|
|
|
$p |= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
|
|
|
|
|
$p |= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
|
2016-09-08 20:07:15 +00:00
|
|
|
|
|
|
|
|
return (bool)$p;
|
|
|
|
|
}
|
|
|
|
|
) );
|
2017-12-08 21:36:25 +00:00
|
|
|
$lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
|
|
|
|
|
$lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
|
|
|
|
|
// Master DB 2
|
|
|
|
|
$mockDB2 = $this->getMockBuilder( 'DatabaseMysqli' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
|
|
|
|
|
$mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
|
|
|
|
|
$mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
|
|
|
|
|
// Load balancer for master DB 2
|
|
|
|
|
$lb2 = $this->getMockBuilder( 'LoadBalancer' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$lb2->method( 'getConnection' )->willReturn( $mockDB2 );
|
|
|
|
|
$lb2->method( 'getServerCount' )->willReturn( 2 );
|
|
|
|
|
$lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
|
|
|
|
|
$lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
|
|
|
|
|
function () use ( $mockDB2 ) {
|
|
|
|
|
$p = 0;
|
|
|
|
|
$p |= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
|
|
|
|
|
$p |= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
|
|
|
|
|
|
|
|
|
|
return (bool)$p;
|
|
|
|
|
}
|
|
|
|
|
) );
|
|
|
|
|
$lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
|
|
|
|
|
$lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
|
2015-10-18 19:53:40 +00:00
|
|
|
|
|
|
|
|
$bag = new HashBagOStuff();
|
|
|
|
|
$cp = new ChronologyProtector(
|
|
|
|
|
$bag,
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-10-18 19:53:40 +00:00
|
|
|
'ip' => '127.0.0.1',
|
|
|
|
|
'agent' => "Totally-Not-FireFox"
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2015-10-18 19:53:40 +00:00
|
|
|
);
|
|
|
|
|
|
2017-12-08 21:36:25 +00:00
|
|
|
$mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
|
|
|
|
|
$mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
|
|
|
|
|
$mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
|
|
|
|
|
$mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
|
|
|
|
|
|
|
|
|
|
// Nothing to wait for on first HTTP request start
|
|
|
|
|
$cp->initLB( $lb1 );
|
|
|
|
|
$cp->initLB( $lb2 );
|
|
|
|
|
// Record positions in stash on first HTTP request end
|
|
|
|
|
$cp->shutdownLB( $lb1 );
|
|
|
|
|
$cp->shutdownLB( $lb2 );
|
|
|
|
|
$cpIndex = null;
|
|
|
|
|
$cp->shutdown( null, 'sync', $cpIndex );
|
2015-10-18 19:53:40 +00:00
|
|
|
|
2017-12-08 21:36:25 +00:00
|
|
|
$this->assertEquals( 1, $cpIndex, "CP write index set" );
|
2015-10-18 19:53:40 +00:00
|
|
|
|
|
|
|
|
// (b) Second HTTP request
|
2017-12-08 21:36:25 +00:00
|
|
|
|
|
|
|
|
// Load balancer for master DB 1
|
|
|
|
|
$lb1 = $this->getMockBuilder( 'LoadBalancer' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$lb1->method( 'getServerCount' )->willReturn( 2 );
|
|
|
|
|
$lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
|
|
|
|
|
$lb1->expects( $this->once() )
|
|
|
|
|
->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
|
|
|
|
|
// Load balancer for master DB 2
|
|
|
|
|
$lb2 = $this->getMockBuilder( 'LoadBalancer' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$lb2->method( 'getServerCount' )->willReturn( 2 );
|
|
|
|
|
$lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
|
|
|
|
|
$lb2->expects( $this->once() )
|
|
|
|
|
->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
|
|
|
|
|
|
2015-10-18 19:53:40 +00:00
|
|
|
$cp = new ChronologyProtector(
|
|
|
|
|
$bag,
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-10-18 19:53:40 +00:00
|
|
|
'ip' => '127.0.0.1',
|
|
|
|
|
'agent' => "Totally-Not-FireFox"
|
2017-12-08 21:36:25 +00:00
|
|
|
],
|
|
|
|
|
$cpIndex
|
2015-10-18 19:53:40 +00:00
|
|
|
);
|
|
|
|
|
|
2017-12-08 21:36:25 +00:00
|
|
|
// Wait for last positions to be reached on second HTTP request start
|
|
|
|
|
$cp->initLB( $lb1 );
|
|
|
|
|
$cp->initLB( $lb2 );
|
|
|
|
|
// Shutdown (nothing to record)
|
|
|
|
|
$cp->shutdownLB( $lb1 );
|
|
|
|
|
$cp->shutdownLB( $lb2 );
|
|
|
|
|
$cpIndex = null;
|
|
|
|
|
$cp->shutdown( null, 'sync', $cpIndex );
|
2015-10-18 19:53:40 +00:00
|
|
|
|
2017-12-08 21:36:25 +00:00
|
|
|
$this->assertEquals( null, $cpIndex, "CP write index retained" );
|
2015-10-18 19:53:40 +00:00
|
|
|
}
|
2016-09-17 04:39:57 +00:00
|
|
|
|
2016-11-16 10:35:20 +00:00
|
|
|
private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
|
2018-01-12 21:44:12 +00:00
|
|
|
global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
|
|
|
|
|
global $wgSQLiteDataDir;
|
2016-11-16 10:35:20 +00:00
|
|
|
|
|
|
|
|
return new LBFactoryMulti( $baseOverride + [
|
|
|
|
|
'sectionsByDB' => [],
|
|
|
|
|
'sectionLoads' => [
|
|
|
|
|
'DEFAULT' => [
|
|
|
|
|
'test-db1' => 1,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'serverTemplate' => $serverOverride + [
|
|
|
|
|
'dbname' => $wgDBname,
|
2018-01-12 21:44:12 +00:00
|
|
|
'tablePrefix' => $wgDBprefix,
|
2016-11-16 10:35:20 +00:00
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'flags' => DBO_DEFAULT
|
|
|
|
|
],
|
|
|
|
|
'hostsByName' => [
|
|
|
|
|
'test-db1' => $wgDBserver,
|
|
|
|
|
],
|
|
|
|
|
'loadMonitorClass' => 'LoadMonitorNull',
|
2018-01-12 21:44:12 +00:00
|
|
|
'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix )
|
2016-11-16 10:35:20 +00:00
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 04:39:57 +00:00
|
|
|
public function testNiceDomains() {
|
2016-10-27 05:44:08 +00:00
|
|
|
global $wgDBname, $wgDBtype;
|
|
|
|
|
|
|
|
|
|
if ( $wgDBtype === 'sqlite' ) {
|
|
|
|
|
$tmpDir = $this->getNewTempDirectory();
|
|
|
|
|
$dbPath = "$tmpDir/unit_test_db.sqlite";
|
|
|
|
|
file_put_contents( $dbPath, '' );
|
|
|
|
|
$tempFsFile = new TempFSFile( $dbPath );
|
|
|
|
|
$tempFsFile->autocollect();
|
|
|
|
|
} else {
|
|
|
|
|
$dbPath = null;
|
|
|
|
|
}
|
2016-09-17 04:39:57 +00:00
|
|
|
|
2016-10-27 05:44:08 +00:00
|
|
|
$factory = $this->newLBFactoryMulti(
|
|
|
|
|
[],
|
|
|
|
|
[ 'dbFilePath' => $dbPath ]
|
|
|
|
|
);
|
2016-09-17 04:39:57 +00:00
|
|
|
$lb = $factory->getMainLB();
|
|
|
|
|
|
2016-10-27 05:44:08 +00:00
|
|
|
if ( $wgDBtype !== 'sqlite' ) {
|
|
|
|
|
$db = $lb->getConnectionRef( DB_MASTER );
|
|
|
|
|
$this->assertEquals(
|
2018-01-12 21:44:12 +00:00
|
|
|
wfWikiID(),
|
2016-10-27 05:44:08 +00:00
|
|
|
$db->getDomainID()
|
|
|
|
|
);
|
|
|
|
|
unset( $db );
|
|
|
|
|
}
|
2016-09-17 04:39:57 +00:00
|
|
|
|
2016-09-26 22:40:07 +00:00
|
|
|
/** @var Database $db */
|
2016-09-17 04:39:57 +00:00
|
|
|
$db = $lb->getConnection( DB_MASTER, [], '' );
|
|
|
|
|
|
2018-01-12 21:44:12 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$wgDBname,
|
|
|
|
|
$db->getDomainId(),
|
|
|
|
|
'Main domain ID handle used; same DB name'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$wgDBname,
|
|
|
|
|
$db->getDBname(),
|
|
|
|
|
'Main domain ID handle used; same DB name'
|
|
|
|
|
);
|
2016-09-17 04:39:57 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'',
|
2018-01-12 21:44:12 +00:00
|
|
|
$db->tablePrefix(),
|
|
|
|
|
'Main domain ID handle used; prefix is empty though'
|
2016-09-17 04:39:57 +00:00
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( "$wgDBname.page" ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'nice_db.page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
2018-01-12 21:44:12 +00:00
|
|
|
$lb->reuseConnection( $db ); // don't care
|
|
|
|
|
|
|
|
|
|
$db = $lb->getConnection( DB_MASTER ); // local domain connection
|
2016-09-17 04:39:57 +00:00
|
|
|
$factory->setDomainPrefix( 'my_' );
|
2018-01-12 21:44:12 +00:00
|
|
|
|
2016-09-17 04:39:57 +00:00
|
|
|
$this->assertEquals(
|
2018-01-12 21:44:12 +00:00
|
|
|
"$wgDBname-my_",
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->getDomainID()
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'my_page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'other_nice_db.page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$factory->closeAll();
|
|
|
|
|
$factory->destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTrickyDomain() {
|
2018-01-12 21:44:12 +00:00
|
|
|
global $wgDBtype, $wgDBname;
|
2016-10-27 05:44:08 +00:00
|
|
|
|
|
|
|
|
if ( $wgDBtype === 'sqlite' ) {
|
|
|
|
|
$tmpDir = $this->getNewTempDirectory();
|
|
|
|
|
$dbPath = "$tmpDir/unit_test_db.sqlite";
|
|
|
|
|
file_put_contents( $dbPath, '' );
|
|
|
|
|
$tempFsFile = new TempFSFile( $dbPath );
|
|
|
|
|
$tempFsFile->autocollect();
|
|
|
|
|
} else {
|
|
|
|
|
$dbPath = null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 21:44:12 +00:00
|
|
|
$dbname = 'unittest-domain'; // explodes if DB is selected
|
2016-09-17 04:39:57 +00:00
|
|
|
$factory = $this->newLBFactoryMulti(
|
2018-01-12 21:44:12 +00:00
|
|
|
[ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
|
|
|
|
|
[ 'dbFilePath' => $dbPath ]
|
2016-10-27 05:44:08 +00:00
|
|
|
);
|
2016-09-17 04:39:57 +00:00
|
|
|
$lb = $factory->getMainLB();
|
2016-09-26 22:40:07 +00:00
|
|
|
/** @var Database $db */
|
2016-09-17 04:39:57 +00:00
|
|
|
$db = $lb->getConnection( DB_MASTER, [], '' );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2018-01-12 21:44:12 +00:00
|
|
|
$wgDBname,
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->getDomainID()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( "$dbname.page" ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'nice_db.page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
2018-01-12 21:44:12 +00:00
|
|
|
$lb->reuseConnection( $db ); // don't care
|
|
|
|
|
|
2016-09-17 04:39:57 +00:00
|
|
|
$factory->setDomainPrefix( 'my_' );
|
2018-01-12 21:44:12 +00:00
|
|
|
$db = $lb->getConnection( DB_MASTER, [], "$wgDBname-my_" );
|
2016-09-17 04:39:57 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'my_page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'other_nice_db.page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2016-10-27 05:44:08 +00:00
|
|
|
$this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
|
2016-09-17 04:39:57 +00:00
|
|
|
$db->tableName( 'garbage-db.page' ),
|
|
|
|
|
"Correct full table name"
|
|
|
|
|
);
|
|
|
|
|
|
2017-05-10 01:27:28 +00:00
|
|
|
if ( $db->databasesAreIndependent() ) {
|
|
|
|
|
try {
|
|
|
|
|
$e = null;
|
|
|
|
|
$db->selectDB( 'garbage-db' );
|
|
|
|
|
} catch ( \Wikimedia\Rdbms\DBConnectionError $e ) {
|
|
|
|
|
// expected
|
|
|
|
|
}
|
|
|
|
|
$this->assertInstanceOf( '\Wikimedia\Rdbms\DBConnectionError', $e );
|
|
|
|
|
$this->assertFalse( $db->isOpen() );
|
|
|
|
|
} else {
|
|
|
|
|
\MediaWiki\suppressWarnings();
|
|
|
|
|
$this->assertFalse( $db->selectDB( 'garbage-db' ) );
|
|
|
|
|
\MediaWiki\restoreWarnings();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-12 21:44:12 +00:00
|
|
|
$lb->reuseConnection( $db ); // don't care
|
|
|
|
|
|
2016-09-17 04:39:57 +00:00
|
|
|
$factory->closeAll();
|
|
|
|
|
$factory->destroy();
|
|
|
|
|
}
|
2016-10-27 05:44:08 +00:00
|
|
|
|
|
|
|
|
private function quoteTable( Database $db, $table ) {
|
|
|
|
|
if ( $db->getType() === 'sqlite' ) {
|
|
|
|
|
return $table;
|
|
|
|
|
} else {
|
|
|
|
|
return $db->addIdentifierQuotes( $table );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-27 10:17:06 +00:00
|
|
|
}
|