3 use Wikimedia\Rdbms\DBError
;
4 use Wikimedia\Rdbms\IDatabase
;
5 use Wikimedia\Rdbms\LoadBalancer
;
8 * Holds tests for LoadBalancer MediaWiki class.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
28 class LoadBalancerTest
extends MediaWikiTestCase
{
29 public function testWithoutReplica() {
30 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
34 'host' => $wgDBserver,
35 'dbname' => $wgDBname,
37 'password' => $wgDBpassword,
39 'dbDirectory' => $wgSQLiteDataDir,
41 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
45 $lb = new LoadBalancer( [
46 'servers' => $servers,
47 'localDomain' => wfWikiID()
50 $dbw = $lb->getConnection( DB_MASTER
);
51 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
52 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX
), "DBO_TRX set on master" );
53 $this->assertWriteAllowed( $dbw );
55 $dbr = $lb->getConnection( DB_REPLICA
);
56 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
57 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX
), "DBO_TRX set on replica" );
59 $dbwAuto = $lb->getConnection( DB_MASTER
, [], false, $lb::CONN_TRX_AUTO
);
60 $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX
), "No DBO_TRX with CONN_TRX_AUTO" );
61 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX
), "DBO_TRX still set on master" );
62 $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
64 $dbrAuto = $lb->getConnection( DB_REPLICA
, [], false, $lb::CONN_TRX_AUTO
);
65 $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX
), "No DBO_TRX with CONN_TRX_AUTO" );
66 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX
), "DBO_TRX still set on replica" );
67 $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
69 $dbwAuto2 = $lb->getConnection( DB_MASTER
, [], false, $lb::CONN_TRX_AUTO
);
70 $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
75 public function testWithReplica() {
76 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
80 'host' => $wgDBserver,
81 'dbname' => $wgDBname,
83 'password' => $wgDBpassword,
85 'dbDirectory' => $wgSQLiteDataDir,
87 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
90 'host' => $wgDBserver,
91 'dbname' => $wgDBname,
93 'password' => $wgDBpassword,
95 'dbDirectory' => $wgSQLiteDataDir,
97 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
101 $lb = new LoadBalancer( [
102 'servers' => $servers,
103 'localDomain' => wfWikiID(),
104 'loadMonitorClass' => 'LoadMonitorNull'
107 $dbw = $lb->getConnection( DB_MASTER
);
108 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
110 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
111 $dbw->getLBInfo( 'clusterMasterHost' ),
112 'cluster master set' );
113 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX
), "DBO_TRX set on master" );
114 $this->assertWriteAllowed( $dbw );
116 $dbr = $lb->getConnection( DB_REPLICA
);
117 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
119 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
120 $dbr->getLBInfo( 'clusterMasterHost' ),
121 'cluster master set' );
122 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX
), "DBO_TRX set on replica" );
123 $this->assertWriteForbidden( $dbr );
125 $dbwAuto = $lb->getConnection( DB_MASTER
, [], false, $lb::CONN_TRX_AUTO
);
126 $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX
), "No DBO_TRX with CONN_TRX_AUTO" );
127 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX
), "DBO_TRX still set on master" );
128 $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
130 $dbrAuto = $lb->getConnection( DB_REPLICA
, [], false, $lb::CONN_TRX_AUTO
);
131 $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX
), "No DBO_TRX with CONN_TRX_AUTO" );
132 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX
), "DBO_TRX still set on replica" );
133 $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
135 $dbwAuto2 = $lb->getConnection( DB_MASTER
, [], false, $lb::CONN_TRX_AUTO
);
136 $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
141 private function assertWriteForbidden( IDatabase
$db ) {
143 $db->delete( 'user', [ 'user_id' => 57634126 ], 'TEST' );
144 $this->fail( 'Write operation should have failed!' );
145 } catch ( DBError
$ex ) {
146 // check that the exception message contains "Write operation"
147 $constraint = new PHPUnit_Framework_Constraint_StringContains( 'Write operation' );
149 if ( !$constraint->evaluate( $ex->getMessage(), '', true ) ) {
150 // re-throw original error, to preserve stack trace
154 $db->rollback( __METHOD__
, 'flush' );
158 private function assertWriteAllowed( IDatabase
$db ) {
160 $this->assertNotSame( false, $db->delete( 'user', [ 'user_id' => 57634126 ] ) );
162 $db->rollback( __METHOD__
, 'flush' );