Merge "Rephrase tooltip-rollback for clarity"
[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 = LBFactoryMW::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 ];
58 }
59
60 public function testLBFactorySimpleServer() {
61 $this->setMwGlobals( 'wgDBservers', false );
62
63 $factory = new LBFactorySimple( [] );
64 $lb = $factory->getMainLB();
65
66 $dbw = $lb->getConnection( DB_MASTER );
67 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
68
69 $dbr = $lb->getConnection( DB_SLAVE );
70 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
71
72 $factory->shutdown();
73 $lb->closeAll();
74 }
75
76 public function testLBFactorySimpleServers() {
77 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
78
79 $this->setMwGlobals( 'wgDBservers', [
80 [ // master
81 'host' => $wgDBserver,
82 'dbname' => $wgDBname,
83 'user' => $wgDBuser,
84 'password' => $wgDBpassword,
85 'type' => $wgDBtype,
86 'load' => 0,
87 'flags' => DBO_TRX // REPEATABLE-READ for consistency
88 ],
89 [ // emulated slave
90 'host' => $wgDBserver,
91 'dbname' => $wgDBname,
92 'user' => $wgDBuser,
93 'password' => $wgDBpassword,
94 'type' => $wgDBtype,
95 'load' => 100,
96 'flags' => DBO_TRX // REPEATABLE-READ for consistency
97 ]
98 ] );
99
100 $factory = new LBFactorySimple( [ 'loadMonitorClass' => 'LoadMonitorNull' ] );
101 $lb = $factory->getMainLB();
102
103 $dbw = $lb->getConnection( DB_MASTER );
104 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
105 $this->assertEquals(
106 $wgDBserver, $dbw->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
107
108 $dbr = $lb->getConnection( DB_SLAVE );
109 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
110 $this->assertEquals(
111 $wgDBserver, $dbr->getLBInfo( 'clusterMasterHost' ), 'cluster master set' );
112
113 $factory->shutdown();
114 $lb->closeAll();
115 }
116
117 public function testLBFactoryMulti() {
118 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype;
119
120 $factory = new LBFactoryMulti( [
121 'sectionsByDB' => [],
122 'sectionLoads' => [
123 'DEFAULT' => [
124 'test-db1' => 0,
125 'test-db2' => 100,
126 ],
127 ],
128 'serverTemplate' => [
129 'dbname' => $wgDBname,
130 'user' => $wgDBuser,
131 'password' => $wgDBpassword,
132 'type' => $wgDBtype,
133 'flags' => DBO_DEFAULT
134 ],
135 'hostsByName' => [
136 'test-db1' => $wgDBserver,
137 'test-db2' => $wgDBserver
138 ],
139 'loadMonitorClass' => 'LoadMonitorNull'
140 ] );
141 $lb = $factory->getMainLB();
142
143 $dbw = $lb->getConnection( DB_MASTER );
144 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
145
146 $dbr = $lb->getConnection( DB_SLAVE );
147 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
148
149 $factory->shutdown();
150 $lb->closeAll();
151 }
152
153 public function testChronologyProtector() {
154 // (a) First HTTP request
155 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
156
157 $now = microtime( true );
158 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
159 ->disableOriginalConstructor()
160 ->getMock();
161 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
162 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
163 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
164
165 $lb = $this->getMockBuilder( 'LoadBalancer' )
166 ->disableOriginalConstructor()
167 ->getMock();
168 $lb->method( 'getConnection' )->willReturn( $mockDB );
169 $lb->method( 'getServerCount' )->willReturn( 2 );
170 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
171 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
172 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
173 function () use ( $mockDB ) {
174 $p = 0;
175 $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
176 $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
177
178 return (bool)$p;
179 }
180 ) );
181 $lb->method( 'getMasterPos' )->willReturn( $mPos );
182
183 $bag = new HashBagOStuff();
184 $cp = new ChronologyProtector(
185 $bag,
186 [
187 'ip' => '127.0.0.1',
188 'agent' => "Totally-Not-FireFox"
189 ]
190 );
191
192 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
193 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
194
195 // Nothing to wait for
196 $cp->initLB( $lb );
197 // Record in stash
198 $cp->shutdownLB( $lb );
199 $cp->shutdown();
200
201 // (b) Second HTTP request
202 $cp = new ChronologyProtector(
203 $bag,
204 [
205 'ip' => '127.0.0.1',
206 'agent' => "Totally-Not-FireFox"
207 ]
208 );
209
210 $lb->expects( $this->once() )
211 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
212
213 // Wait
214 $cp->initLB( $lb );
215 // Record in stash
216 $cp->shutdownLB( $lb );
217 $cp->shutdown();
218 }
219 }