f8ab7f481be2effe11f2d8d2e5a4baa523f07ced
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LoadBalancerTest.php
1 <?php
2
3 use Wikimedia\Rdbms\LoadBalancer;
4
5 /**
6 * Holds tests for LoadBalancer MediaWiki class.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @group Database
24 * @file
25 */
26 class LoadBalancerTest extends MediaWikiTestCase {
27 public function testLBSimpleServer() {
28 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
29
30 $servers = [
31 [
32 'host' => $wgDBserver,
33 'dbname' => $wgDBname,
34 'user' => $wgDBuser,
35 'password' => $wgDBpassword,
36 'type' => $wgDBtype,
37 'dbDirectory' => $wgSQLiteDataDir,
38 'load' => 0,
39 'flags' => DBO_TRX // REPEATABLE-READ for consistency
40 ],
41 ];
42
43 $lb = new LoadBalancer( [
44 'servers' => $servers,
45 'localDomain' => wfWikiID()
46 ] );
47
48 $dbw = $lb->getConnection( DB_MASTER );
49 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
50 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
51
52 $dbr = $lb->getConnection( DB_REPLICA );
53 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
54 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
55
56 $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
57 $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
58 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
59 $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
60
61 $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
62 $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
63 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
64 $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
65
66 $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
67 $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
68
69 $lb->closeAll();
70 }
71
72 public function testLBSimpleServers() {
73 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
74
75 $servers = [
76 [ // master
77 'host' => $wgDBserver,
78 'dbname' => $wgDBname,
79 'user' => $wgDBuser,
80 'password' => $wgDBpassword,
81 'type' => $wgDBtype,
82 'dbDirectory' => $wgSQLiteDataDir,
83 'load' => 0,
84 'flags' => DBO_TRX // REPEATABLE-READ for consistency
85 ],
86 [ // emulated slave
87 'host' => $wgDBserver,
88 'dbname' => $wgDBname,
89 'user' => $wgDBuser,
90 'password' => $wgDBpassword,
91 'type' => $wgDBtype,
92 'dbDirectory' => $wgSQLiteDataDir,
93 'load' => 100,
94 'flags' => DBO_TRX // REPEATABLE-READ for consistency
95 ]
96 ];
97
98 $lb = new LoadBalancer( [
99 'servers' => $servers,
100 'localDomain' => wfWikiID(),
101 'loadMonitorClass' => 'LoadMonitorNull'
102 ] );
103
104 $dbw = $lb->getConnection( DB_MASTER );
105 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
106 $this->assertEquals(
107 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
108 $dbw->getLBInfo( 'clusterMasterHost' ),
109 'cluster master set' );
110 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
111
112 $dbr = $lb->getConnection( DB_REPLICA );
113 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
114 $this->assertEquals(
115 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
116 $dbr->getLBInfo( 'clusterMasterHost' ),
117 'cluster master set' );
118 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
119
120 $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
121 $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
122 $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
123 $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
124
125 $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
126 $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
127 $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
128 $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
129
130 $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
131 $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
132
133 $lb->closeAll();
134 }
135 }