3 * Holds tests for LBFactory abstract MediaWiki class.
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.
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.
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
22 * @author Antoine Musso
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Wikimedia Foundation Inc.
26 class LBFactoryTest
extends MediaWikiTestCase
{
29 * @dataProvider getLBFactoryClassProvider
31 public function testGetLBFactoryClass( $expected, $deprecated ) {
32 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
33 ->disableOriginalConstructor()
37 'class' => $deprecated,
38 'connection' => $mockDB,
39 # Various other parameters required:
42 'serverTemplate' => [],
45 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
46 $result = MWLBFactory
::getLBFactoryClass( $config );
48 $this->assertEquals( $expected, $result );
51 public function getLBFactoryClassProvider() {
53 # Format: new class, old class
54 [ 'LBFactorySimple', 'LBFactory_Simple' ],
55 [ 'LBFactorySingle', 'LBFactory_Single' ],
56 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
60 public function testLBFactorySimpleServer() {
61 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
65 'host' => $wgDBserver,
66 'dbname' => $wgDBname,
68 'password' => $wgDBpassword,
70 'dbDirectory' => $wgSQLiteDataDir,
72 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
76 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
77 $lb = $factory->getMainLB();
79 $dbw = $lb->getConnection( DB_MASTER
);
80 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
82 $dbr = $lb->getConnection( DB_SLAVE
);
83 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
89 public function testLBFactorySimpleServers() {
90 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
94 'host' => $wgDBserver,
95 'dbname' => $wgDBname,
97 'password' => $wgDBpassword,
99 'dbDirectory' => $wgSQLiteDataDir,
101 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
104 'host' => $wgDBserver,
105 'dbname' => $wgDBname,
107 'password' => $wgDBpassword,
109 'dbDirectory' => $wgSQLiteDataDir,
111 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
115 $factory = new LBFactorySimple( [
116 'servers' => $servers,
117 'loadMonitorClass' => 'LoadMonitorNull'
119 $lb = $factory->getMainLB();
121 $dbw = $lb->getConnection( DB_MASTER
);
122 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
124 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
125 $dbw->getLBInfo( 'clusterMasterHost' ),
126 'cluster master set' );
128 $dbr = $lb->getConnection( DB_SLAVE
);
129 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
131 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
132 $dbr->getLBInfo( 'clusterMasterHost' ),
133 'cluster master set' );
135 $factory->shutdown();
139 public function testLBFactoryMulti() {
140 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
142 $factory = new LBFactoryMulti( [
143 'sectionsByDB' => [],
150 'serverTemplate' => [
151 'dbname' => $wgDBname,
153 'password' => $wgDBpassword,
155 'dbDirectory' => $wgSQLiteDataDir,
156 'flags' => DBO_DEFAULT
159 'test-db1' => $wgDBserver,
160 'test-db2' => $wgDBserver
162 'loadMonitorClass' => 'LoadMonitorNull'
164 $lb = $factory->getMainLB();
166 $dbw = $lb->getConnection( DB_MASTER
);
167 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
169 $dbr = $lb->getConnection( DB_SLAVE
);
170 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
172 $factory->shutdown();
176 public function testChronologyProtector() {
177 // (a) First HTTP request
178 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
180 $now = microtime( true );
181 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
182 ->disableOriginalConstructor()
184 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
185 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
186 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
188 $lb = $this->getMockBuilder( 'LoadBalancer' )
189 ->disableOriginalConstructor()
191 $lb->method( 'getConnection' )->willReturn( $mockDB );
192 $lb->method( 'getServerCount' )->willReturn( 2 );
193 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
194 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
195 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
196 function () use ( $mockDB ) {
198 $p |
= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
199 $p |
= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
204 $lb->method( 'getMasterPos' )->willReturn( $mPos );
206 $bag = new HashBagOStuff();
207 $cp = new ChronologyProtector(
211 'agent' => "Totally-Not-FireFox"
215 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
216 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
218 // Nothing to wait for
221 $cp->shutdownLB( $lb );
224 // (b) Second HTTP request
225 $cp = new ChronologyProtector(
229 'agent' => "Totally-Not-FireFox"
233 $lb->expects( $this->once() )
234 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
239 $cp->shutdownLB( $lb );
243 public function testNiceDomains() {
244 global $wgDBname, $wgDBtype;
246 if ( $wgDBtype === 'sqlite' ) {
247 $tmpDir = $this->getNewTempDirectory();
248 $dbPath = "$tmpDir/unit_test_db.sqlite";
249 file_put_contents( $dbPath, '' );
250 $tempFsFile = new TempFSFile( $dbPath );
251 $tempFsFile->autocollect();
256 $factory = $this->newLBFactoryMulti(
258 [ 'dbFilePath' => $dbPath ]
260 $lb = $factory->getMainLB();
262 if ( $wgDBtype !== 'sqlite' ) {
263 $db = $lb->getConnectionRef( DB_MASTER
);
271 /** @var Database $db */
272 $db = $lb->getConnection( DB_MASTER
, [], '' );
273 $lb->reuseConnection( $db ); // don't care
281 $this->quoteTable( $db, 'page' ),
282 $db->tableName( 'page' ),
283 "Correct full table name"
287 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
288 $db->tableName( "$wgDBname.page" ),
289 "Correct full table name"
293 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
294 $db->tableName( 'nice_db.page' ),
295 "Correct full table name"
298 $factory->setDomainPrefix( 'my_' );
304 $this->quoteTable( $db, 'my_page' ),
305 $db->tableName( 'page' ),
306 "Correct full table name"
309 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
310 $db->tableName( 'other_nice_db.page' ),
311 "Correct full table name"
314 $factory->closeAll();
318 public function testTrickyDomain() {
321 if ( $wgDBtype === 'sqlite' ) {
322 $tmpDir = $this->getNewTempDirectory();
323 $dbPath = "$tmpDir/unit_test_db.sqlite";
324 file_put_contents( $dbPath, '' );
325 $tempFsFile = new TempFSFile( $dbPath );
326 $tempFsFile->autocollect();
331 $dbname = 'unittest-domain';
332 $factory = $this->newLBFactoryMulti(
333 [ 'localDomain' => $dbname ],
334 [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ]
336 $lb = $factory->getMainLB();
337 /** @var Database $db */
338 $db = $lb->getConnection( DB_MASTER
, [], '' );
339 $lb->reuseConnection( $db ); // don't care
347 $this->quoteTable( $db, 'page' ),
348 $db->tableName( 'page' ),
349 "Correct full table name"
353 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
354 $db->tableName( "$dbname.page" ),
355 "Correct full table name"
359 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
360 $db->tableName( 'nice_db.page' ),
361 "Correct full table name"
364 $factory->setDomainPrefix( 'my_' );
367 $this->quoteTable( $db, 'my_page' ),
368 $db->tableName( 'page' ),
369 "Correct full table name"
372 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
373 $db->tableName( 'other_nice_db.page' ),
374 "Correct full table name"
377 \MediaWiki\
suppressWarnings();
378 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
379 \MediaWiki\restoreWarnings
();
382 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
383 $db->tableName( 'garbage-db.page' ),
384 "Correct full table name"
387 $factory->closeAll();
392 * @covers LBFactory::declareUsageSectionStart()
393 * @covers LBFactory::declareUsageSectionEnd()
394 * @covers LoadBalancer::declareUsageSectionStart()
395 * @covers LoadBalancer::declareUsageSectionEnd()
397 public function testUsageInfo() {
398 $wallTime = microtime( true );
400 $mockDB = $this->getMockBuilder( 'DatabaseMysql' )
401 ->disableOriginalConstructor()
407 'getSessionLagStatus',
408 'getApproximateLagStatus'
411 $mockDB->method( 'doQuery' )->willReturn( new FakeResultWrapper( [] ) );
412 $mockDB->method( 'affectedRows' )->willReturn( 0 );
413 $mockDB->method( 'getLag' )->willReturn( 3 );
414 $mockDB->method( 'getSessionLagStatus' )->willReturn( [
415 'lag' => 3, 'since' => $wallTime
417 $mockDB->method( 'getApproximateLagStatus' )->willReturn( [
418 'lag' => 3, 'since' => $wallTime
420 $mockDBProbe = TestingAccessWrapper
::newFromObject( $mockDB );
421 $mockDBProbe->profiler
= new ProfilerStub( [] );
422 $mockDBProbe->trxProfiler
= new TransactionProfiler();
423 $mockDBProbe->connLogger
= new \Psr\Log\
NullLogger();
424 $mockDBProbe->queryLogger
= new \Psr\Log\
NullLogger();
425 $lbFactory = new LBFactorySingle( [
426 'connection' => $mockDB
428 $mockDB->setLBInfo( 'replica', true );
430 $id = $lbFactory->declareUsageSectionStart( 'test' );
431 $mockDB->query( "SELECT 1" );
432 $mockDB->query( "SELECT 1" );
433 $mockDB->query( "SELECT 1" );
434 $info = $lbFactory->declareUsageSectionEnd( $id );
436 $this->assertEquals( 3, $info['readQueries'] );
437 $this->assertEquals( 0, $info['writeQueries'] );
438 $this->assertEquals( false, $info['cacheSetOptions']['pending'] );
439 $this->assertEquals( 3, $info['cacheSetOptions']['lag'] );
440 $this->assertGreaterThanOrEqual( $wallTime - 10, $info['cacheSetOptions']['since'] );
441 $this->assertLessThan( $wallTime +
10, $info['cacheSetOptions']['since'] );
444 $mockDB->query( "UPDATE x SET y=1" );
445 $id = $lbFactory->declareUsageSectionStart( 'k' );
446 $mockDB->query( "UPDATE x SET y=2" );
448 $info = $lbFactory->declareUsageSectionEnd( $id );
450 $this->assertEquals( 2, $info['readQueries'] ); // +1 for ping()
451 $this->assertEquals( 1, $info['writeQueries'] );
452 $this->assertEquals( true, $info['cacheSetOptions']['pending'] );
453 $this->assertEquals( 3, $info['cacheSetOptions']['lag'] );
454 $this->assertGreaterThanOrEqual( $wallTime - 10, $info['cacheSetOptions']['since'] );
455 $this->assertLessThan( $wallTime +
10, $info['cacheSetOptions']['since'] );
458 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
459 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir;
461 return new LBFactoryMulti( $baseOverride +
[
462 'sectionsByDB' => [],
468 'serverTemplate' => $serverOverride +
[
469 'dbname' => $wgDBname,
471 'password' => $wgDBpassword,
473 'dbDirectory' => $wgSQLiteDataDir,
474 'flags' => DBO_DEFAULT
477 'test-db1' => $wgDBserver,
479 'loadMonitorClass' => 'LoadMonitorNull',
480 'localDomain' => wfWikiID()
484 private function quoteTable( Database
$db, $table ) {
485 if ( $db->getType() === 'sqlite' ) {
488 return $db->addIdentifierQuotes( $table );