Merge "Update Bugzilla references to Phabricator references"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
1 <?php
2 /**
3 * Holds tests for LBFactory abstract MediaWiki class.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @group Database
21 * @file
22 * @author Antoine Musso
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Wikimedia Foundation Inc.
25 */
26 class LBFactoryTest extends MediaWikiTestCase {
27
28 /**
29 * @dataProvider getLBFactoryClassProvider
30 */
31 public function testGetLBFactoryClass( $expected, $deprecated ) {
32 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
33 ->disableOriginalConstructor()
34 ->getMock();
35
36 $config = [
37 'class' => $deprecated,
38 'connection' => $mockDB,
39 # Various other parameters required:
40 'sectionsByDB' => [],
41 'sectionLoads' => [],
42 'serverTemplate' => [],
43 ];
44
45 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
46 $result = LBFactory::getLBFactoryClass( $config );
47
48 $this->assertEquals( $expected, $result );
49 }
50
51 public function getLBFactoryClassProvider() {
52 return [
53 # Format: new class, old class
54 [ 'LBFactorySimple', 'LBFactory_Simple' ],
55 [ 'LBFactorySingle', 'LBFactory_Single' ],
56 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
57 [ 'LBFactoryFake', 'LBFactory_Fake' ],
58 ];
59 }
60
61 public function testLBFactorySimpleServer() {
62 $this->setMwGlobals( 'wgDBservers', false );
63
64 $factory = new LBFactorySimple( [] );
65 $lb = $factory->getMainLB();
66
67 $dbw = $lb->getConnection( DB_MASTER );
68 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
69
70 $dbr = $lb->getConnection( DB_SLAVE );
71 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
72
73 $factory->shutdown();
74 $lb->closeAll();
75 }
76
77 public function testLBFactorySimpleServers() {
78 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
79
80 $this->setMwGlobals( 'wgDBservers', [
81 [ // master
82 'host' => $wgDBserver,
83 'dbname' => $wgDBname,
84 'user' => $wgDBuser,
85 'password' => $wgDBpassword,
86 'type' => $wgDBtype,
87 'load' => 0,
88 'flags' => DBO_TRX // REPEATABLE-READ for consistency
89 ],
90 [ // emulated slave
91 'host' => $wgDBserver,
92 'dbname' => $wgDBname,
93 'user' => $wgDBuser,
94 'password' => $wgDBpassword,
95 'type' => $wgDBtype,
96 'load' => 100,
97 'flags' => DBO_TRX // REPEATABLE-READ for consistency
98 ]
99 ] );
100
101 $factory = new LBFactorySimple( [ 'loadMonitorClass' => 'LoadMonitorNull' ] );
102 $lb = $factory->getMainLB();
103
104 $dbw = $lb->getConnection( DB_MASTER );
105 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
106 $this->assertEquals(
107 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
108
109 $dbr = $lb->getConnection( DB_SLAVE );
110 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
111 $this->assertEquals(
112 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
113
114 $factory->shutdown();
115 $lb->closeAll();
116 }
117
118 public function testLBFactoryMulti() {
119 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
120
121 $factory = new LBFactoryMulti( [
122 'sectionsByDB' => [],
123 'sectionLoads' => [
124 'DEFAULT' => [
125 'test-db1' => 0,
126 'test-db2' => 100,
127 ],
128 ],
129 'serverTemplate' => [
130 'dbname' => $wgDBname,
131 'user' => $wgDBuser,
132 'password' => $wgDBpassword,
133 'type' => $wgDBtype,
134 'flags' => DBO_DEFAULT
135 ],
136 'hostsByName' => [
137 'test-db1' => $wgDBserver,
138 'test-db2' => $wgDBserver
139 ],
140 'loadMonitorClass' => 'LoadMonitorNull'
141 ] );
142 $lb = $factory->getMainLB();
143
144 $dbw = $lb->getConnection( DB_MASTER );
145 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
146
147 $dbr = $lb->getConnection( DB_SLAVE );
148 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
149
150 $factory->shutdown();
151 $lb->closeAll();
152 }
153
154 public function testChronologyProtector() {
155 // (a) First HTTP request
156 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
157
158 $now = microtime( true );
159 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
160 ->disableOriginalConstructor()
161 ->getMock();
162 $mockDB->expects( $this->any() )
163 ->method( 'writesOrCallbacksPending' )->will( $this->returnValue( true ) );
164 $mockDB->expects( $this->any() )
165 ->method( 'lastDoneWrites' )->will( $this->returnValue( $now ) );
166 $mockDB->expects( $this->any() )
167 ->method( 'getMasterPos' )->will( $this->returnValue( $mPos ) );
168
169 $lb = $this->getMockBuilder( 'LoadBalancer' )
170 ->disableOriginalConstructor()
171 ->getMock();
172 $lb->expects( $this->any() )
173 ->method( 'getConnection' )->will( $this->returnValue( $mockDB ) );
174 $lb->expects( $this->any() )
175 ->method( 'getServerCount' )->will( $this->returnValue( 2 ) );
176 $lb->expects( $this->any() )
177 ->method( 'parentInfo' )->will( $this->returnValue( [ 'id' => "main-DEFAULT" ] ) );
178 $lb->expects( $this->any() )
179 ->method( 'getAnyOpenConnection' )->will( $this->returnValue( $mockDB ) );
180 $lb->expects( $this->any() )
181 ->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
182 function () use ( $mockDB ) {
183 $p = 0;
184 $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
185 $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
186
187 return (bool)$p;
188 }
189 ) );
190 $lb->expects( $this->any() )
191 ->method( 'getMasterPos' )->will( $this->returnValue( $mPos ) );
192
193 $bag = new HashBagOStuff();
194 $cp = new ChronologyProtector(
195 $bag,
196 [
197 'ip' => '127.0.0.1',
198 'agent' => "Totally-Not-FireFox"
199 ]
200 );
201
202 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
203 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
204
205 // Nothing to wait for
206 $cp->initLB( $lb );
207 // Record in stash
208 $cp->shutdownLB( $lb );
209 $cp->shutdown();
210
211 // (b) Second HTTP request
212 $cp = new ChronologyProtector(
213 $bag,
214 [
215 'ip' => '127.0.0.1',
216 'agent' => "Totally-Not-FireFox"
217 ]
218 );
219
220 $lb->expects( $this->once() )
221 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
222
223 // Wait
224 $cp->initLB( $lb );
225 // Record in stash
226 $cp->shutdownLB( $lb );
227 $cp->shutdown();
228 }
229 }