Move ChronologyProtector/TransactionProfiler to Rdbms namespace
[mediawiki.git] / tests / phpunit / includes / db / LBFactoryTest.php
blob1a52dde82e00b3fb0a8120f96e2d436d9270e45b
1 <?php
3 use Wikimedia\Rdbms\ChronologyProtector;
5 /**
6 * Holds tests for LBFactory abstract MediaWiki class.
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.
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.
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
23 * @group Database
24 * @file
25 * @author Antoine Musso
26 * @copyright © 2013 Antoine Musso
27 * @copyright © 2013 Wikimedia Foundation Inc.
29 class LBFactoryTest extends MediaWikiTestCase {
31 /**
32 * @dataProvider getLBFactoryClassProvider
34 public function testGetLBFactoryClass( $expected, $deprecated ) {
35 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
36 ->disableOriginalConstructor()
37 ->getMock();
39 $config = [
40 'class' => $deprecated,
41 'connection' => $mockDB,
42 # Various other parameters required:
43 'sectionsByDB' => [],
44 'sectionLoads' => [],
45 'serverTemplate' => [],
48 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
49 $result = MWLBFactory::getLBFactoryClass( $config );
51 $this->assertEquals( $expected, $result );
54 public function getLBFactoryClassProvider() {
55 return [
56 # Format: new class, old class
57 [ 'LBFactorySimple', 'LBFactory_Simple' ],
58 [ 'LBFactorySingle', 'LBFactory_Single' ],
59 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
63 public function testLBFactorySimpleServer() {
64 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
66 $servers = [
68 'host' => $wgDBserver,
69 'dbname' => $wgDBname,
70 'user' => $wgDBuser,
71 'password' => $wgDBpassword,
72 'type' => $wgDBtype,
73 'dbDirectory' => $wgSQLiteDataDir,
74 'load' => 0,
75 'flags' => DBO_TRX // REPEATABLE-READ for consistency
79 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
80 $lb = $factory->getMainLB();
82 $dbw = $lb->getConnection( DB_MASTER );
83 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
85 $dbr = $lb->getConnection( DB_SLAVE );
86 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
88 $factory->shutdown();
89 $lb->closeAll();
92 public function testLBFactorySimpleServers() {
93 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
95 $servers = [
96 [ // master
97 'host' => $wgDBserver,
98 'dbname' => $wgDBname,
99 'user' => $wgDBuser,
100 'password' => $wgDBpassword,
101 'type' => $wgDBtype,
102 'dbDirectory' => $wgSQLiteDataDir,
103 'load' => 0,
104 'flags' => DBO_TRX // REPEATABLE-READ for consistency
106 [ // emulated slave
107 'host' => $wgDBserver,
108 'dbname' => $wgDBname,
109 'user' => $wgDBuser,
110 'password' => $wgDBpassword,
111 'type' => $wgDBtype,
112 'dbDirectory' => $wgSQLiteDataDir,
113 'load' => 100,
114 'flags' => DBO_TRX // REPEATABLE-READ for consistency
118 $factory = new LBFactorySimple( [
119 'servers' => $servers,
120 'loadMonitorClass' => 'LoadMonitorNull'
121 ] );
122 $lb = $factory->getMainLB();
124 $dbw = $lb->getConnection( DB_MASTER );
125 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
126 $this->assertEquals(
127 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
128 $dbw->getLBInfo( 'clusterMasterHost' ),
129 'cluster master set' );
131 $dbr = $lb->getConnection( DB_SLAVE );
132 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
133 $this->assertEquals(
134 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
135 $dbr->getLBInfo( 'clusterMasterHost' ),
136 'cluster master set' );
138 $factory->shutdown();
139 $lb->closeAll();
142 public function testLBFactoryMulti() {
143 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
145 $factory = new LBFactoryMulti( [
146 'sectionsByDB' => [],
147 'sectionLoads' => [
148 'DEFAULT' => [
149 'test-db1' => 0,
150 'test-db2' => 100,
153 'serverTemplate' => [
154 'dbname' => $wgDBname,
155 'user' => $wgDBuser,
156 'password' => $wgDBpassword,
157 'type' => $wgDBtype,
158 'dbDirectory' => $wgSQLiteDataDir,
159 'flags' => DBO_DEFAULT
161 'hostsByName' => [
162 'test-db1' => $wgDBserver,
163 'test-db2' => $wgDBserver
165 'loadMonitorClass' => 'LoadMonitorNull'
166 ] );
167 $lb = $factory->getMainLB();
169 $dbw = $lb->getConnection( DB_MASTER );
170 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
172 $dbr = $lb->getConnection( DB_SLAVE );
173 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
175 $factory->shutdown();
176 $lb->closeAll();
179 public function testChronologyProtector() {
180 // (a) First HTTP request
181 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
183 $now = microtime( true );
184 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
185 ->disableOriginalConstructor()
186 ->getMock();
187 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
188 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
189 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
191 $lb = $this->getMockBuilder( 'LoadBalancer' )
192 ->disableOriginalConstructor()
193 ->getMock();
194 $lb->method( 'getConnection' )->willReturn( $mockDB );
195 $lb->method( 'getServerCount' )->willReturn( 2 );
196 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
197 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
198 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
199 function () use ( $mockDB ) {
200 $p = 0;
201 $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
202 $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
204 return (bool)$p;
206 ) );
207 $lb->method( 'getMasterPos' )->willReturn( $mPos );
209 $bag = new HashBagOStuff();
210 $cp = new ChronologyProtector(
211 $bag,
213 'ip' => '127.0.0.1',
214 'agent' => "Totally-Not-FireFox"
218 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
219 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
221 // Nothing to wait for
222 $cp->initLB( $lb );
223 // Record in stash
224 $cp->shutdownLB( $lb );
225 $cp->shutdown();
227 // (b) Second HTTP request
228 $cp = new ChronologyProtector(
229 $bag,
231 'ip' => '127.0.0.1',
232 'agent' => "Totally-Not-FireFox"
236 $lb->expects( $this->once() )
237 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
239 // Wait
240 $cp->initLB( $lb );
241 // Record in stash
242 $cp->shutdownLB( $lb );
243 $cp->shutdown();
246 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
247 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir;
249 return new LBFactoryMulti( $baseOverride + [
250 'sectionsByDB' => [],
251 'sectionLoads' => [
252 'DEFAULT' => [
253 'test-db1' => 1,
256 'serverTemplate' => $serverOverride + [
257 'dbname' => $wgDBname,
258 'user' => $wgDBuser,
259 'password' => $wgDBpassword,
260 'type' => $wgDBtype,
261 'dbDirectory' => $wgSQLiteDataDir,
262 'flags' => DBO_DEFAULT
264 'hostsByName' => [
265 'test-db1' => $wgDBserver,
267 'loadMonitorClass' => 'LoadMonitorNull',
268 'localDomain' => wfWikiID()
269 ] );
272 public function testNiceDomains() {
273 global $wgDBname, $wgDBtype;
275 if ( $wgDBtype === 'sqlite' ) {
276 $tmpDir = $this->getNewTempDirectory();
277 $dbPath = "$tmpDir/unit_test_db.sqlite";
278 file_put_contents( $dbPath, '' );
279 $tempFsFile = new TempFSFile( $dbPath );
280 $tempFsFile->autocollect();
281 } else {
282 $dbPath = null;
285 $factory = $this->newLBFactoryMulti(
287 [ 'dbFilePath' => $dbPath ]
289 $lb = $factory->getMainLB();
291 if ( $wgDBtype !== 'sqlite' ) {
292 $db = $lb->getConnectionRef( DB_MASTER );
293 $this->assertEquals(
294 $wgDBname,
295 $db->getDomainID()
297 unset( $db );
300 /** @var Database $db */
301 $db = $lb->getConnection( DB_MASTER, [], '' );
302 $lb->reuseConnection( $db ); // don't care
304 $this->assertEquals(
306 $db->getDomainID()
309 $this->assertEquals(
310 $this->quoteTable( $db, 'page' ),
311 $db->tableName( 'page' ),
312 "Correct full table name"
315 $this->assertEquals(
316 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
317 $db->tableName( "$wgDBname.page" ),
318 "Correct full table name"
321 $this->assertEquals(
322 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
323 $db->tableName( 'nice_db.page' ),
324 "Correct full table name"
327 $factory->setDomainPrefix( 'my_' );
328 $this->assertEquals(
330 $db->getDomainID()
332 $this->assertEquals(
333 $this->quoteTable( $db, 'my_page' ),
334 $db->tableName( 'page' ),
335 "Correct full table name"
337 $this->assertEquals(
338 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
339 $db->tableName( 'other_nice_db.page' ),
340 "Correct full table name"
343 $factory->closeAll();
344 $factory->destroy();
347 public function testTrickyDomain() {
348 global $wgDBtype;
350 if ( $wgDBtype === 'sqlite' ) {
351 $tmpDir = $this->getNewTempDirectory();
352 $dbPath = "$tmpDir/unit_test_db.sqlite";
353 file_put_contents( $dbPath, '' );
354 $tempFsFile = new TempFSFile( $dbPath );
355 $tempFsFile->autocollect();
356 } else {
357 $dbPath = null;
360 $dbname = 'unittest-domain';
361 $factory = $this->newLBFactoryMulti(
362 [ 'localDomain' => $dbname ],
363 [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ]
365 $lb = $factory->getMainLB();
366 /** @var Database $db */
367 $db = $lb->getConnection( DB_MASTER, [], '' );
368 $lb->reuseConnection( $db ); // don't care
370 $this->assertEquals(
372 $db->getDomainID()
375 $this->assertEquals(
376 $this->quoteTable( $db, 'page' ),
377 $db->tableName( 'page' ),
378 "Correct full table name"
381 $this->assertEquals(
382 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
383 $db->tableName( "$dbname.page" ),
384 "Correct full table name"
387 $this->assertEquals(
388 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
389 $db->tableName( 'nice_db.page' ),
390 "Correct full table name"
393 $factory->setDomainPrefix( 'my_' );
395 $this->assertEquals(
396 $this->quoteTable( $db, 'my_page' ),
397 $db->tableName( 'page' ),
398 "Correct full table name"
400 $this->assertEquals(
401 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
402 $db->tableName( 'other_nice_db.page' ),
403 "Correct full table name"
406 \MediaWiki\suppressWarnings();
407 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
408 \MediaWiki\restoreWarnings();
410 $this->assertEquals(
411 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
412 $db->tableName( 'garbage-db.page' ),
413 "Correct full table name"
416 $factory->closeAll();
417 $factory->destroy();
420 private function quoteTable( Database $db, $table ) {
421 if ( $db->getType() === 'sqlite' ) {
422 return $table;
423 } else {
424 return $db->addIdentifierQuotes( $table );