X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fdb%2FLBFactoryTest.php;h=356ebe55ba4cce4ff4d1f8d69f964c8395fcf43a;hb=a171943b6d66b3c9507998a09ccd00177cdf7a1d;hp=1efeeebdafa54876d94f3a8953604e6e16464e16;hpb=3e88f9fa2a5a00bc1330560accf8e9d09c8be42a;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php index 1efeeebdaf..356ebe55ba 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -1,10 +1,4 @@ getMockBuilder( 'DatabaseMysqli' ) + $mockDB = $this->getMockBuilder( DatabaseMysqli::class ) ->disableOriginalConstructor() ->getMock(); @@ -123,7 +131,7 @@ class LBFactoryTest extends MediaWikiTestCase { $factory = new LBFactorySimple( [ 'servers' => $servers, - 'loadMonitorClass' => 'LoadMonitorNull' + 'loadMonitorClass' => LoadMonitorNull::class ] ); $lb = $factory->getMainLB(); @@ -168,7 +176,7 @@ class LBFactoryTest extends MediaWikiTestCase { 'test-db1' => $wgDBserver, 'test-db2' => $wgDBserver ], - 'loadMonitorClass' => 'LoadMonitorNull' + 'loadMonitorClass' => LoadMonitorNull::class ] ); $lb = $factory->getMainLB(); @@ -182,35 +190,66 @@ class LBFactoryTest extends MediaWikiTestCase { $lb->closeAll(); } + /** + * @covers \Wikimedia\Rdbms\ChronologyProtector + */ public function testChronologyProtector() { // (a) First HTTP request - $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' ); + $m1Pos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' ); + $m2Pos = new MySQLMasterPos( 'db1064-bin.002400', '794074907' ); $now = microtime( true ); - $mockDB = $this->getMockBuilder( 'DatabaseMysqli' ) + + // Master DB 1 + $mockDB1 = $this->getMockBuilder( DatabaseMysqli::class ) ->disableOriginalConstructor() ->getMock(); - $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true ); - $mockDB->method( 'lastDoneWrites' )->willReturn( $now ); - $mockDB->method( 'getMasterPos' )->willReturn( $mPos ); - - $lb = $this->getMockBuilder( 'LoadBalancer' ) + $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true ); + $mockDB1->method( 'lastDoneWrites' )->willReturn( $now ); + $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos ); + // Load balancer for master DB 1 + $lb1 = $this->getMockBuilder( LoadBalancer::class ) ->disableOriginalConstructor() ->getMock(); - $lb->method( 'getConnection' )->willReturn( $mockDB ); - $lb->method( 'getServerCount' )->willReturn( 2 ); - $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] ); - $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB ); - $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback( - function () use ( $mockDB ) { + $lb1->method( 'getConnection' )->willReturn( $mockDB1 ); + $lb1->method( 'getServerCount' )->willReturn( 2 ); + $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 ); + $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback( + function () use ( $mockDB1 ) { $p = 0; - $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] ); - $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] ); + $p |= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] ); + $p |= call_user_func( [ $mockDB1, 'lastDoneWrites' ] ); return (bool)$p; } ) ); - $lb->method( 'getMasterPos' )->willReturn( $mPos ); + $lb1->method( 'getMasterPos' )->willReturn( $m1Pos ); + $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' ); + // Master DB 2 + $mockDB2 = $this->getMockBuilder( DatabaseMysqli::class ) + ->disableOriginalConstructor() + ->getMock(); + $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true ); + $mockDB2->method( 'lastDoneWrites' )->willReturn( $now ); + $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos ); + // Load balancer for master DB 2 + $lb2 = $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor() + ->getMock(); + $lb2->method( 'getConnection' )->willReturn( $mockDB2 ); + $lb2->method( 'getServerCount' )->willReturn( 2 ); + $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 ); + $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback( + function () use ( $mockDB2 ) { + $p = 0; + $p |= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] ); + $p |= call_user_func( [ $mockDB2, 'lastDoneWrites' ] ); + + return (bool)$p; + } + ) ); + $lb2->method( 'getMasterPos' )->willReturn( $m2Pos ); + $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' ); $bag = new HashBagOStuff(); $cp = new ChronologyProtector( @@ -221,36 +260,65 @@ class LBFactoryTest extends MediaWikiTestCase { ] ); - $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' ); - $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' ); + $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' ); + $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' ); + $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' ); + $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' ); + + // Nothing to wait for on first HTTP request start + $cp->initLB( $lb1 ); + $cp->initLB( $lb2 ); + // Record positions in stash on first HTTP request end + $cp->shutdownLB( $lb1 ); + $cp->shutdownLB( $lb2 ); + $cpIndex = null; + $cp->shutdown( null, 'sync', $cpIndex ); - // Nothing to wait for - $cp->initLB( $lb ); - // Record in stash - $cp->shutdownLB( $lb ); - $cp->shutdown(); + $this->assertEquals( 1, $cpIndex, "CP write index set" ); // (b) Second HTTP request + + // Load balancer for master DB 1 + $lb1 = $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor() + ->getMock(); + $lb1->method( 'getServerCount' )->willReturn( 2 ); + $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' ); + $lb1->expects( $this->once() ) + ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) ); + // Load balancer for master DB 2 + $lb2 = $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor() + ->getMock(); + $lb2->method( 'getServerCount' )->willReturn( 2 ); + $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' ); + $lb2->expects( $this->once() ) + ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) ); + $cp = new ChronologyProtector( $bag, [ 'ip' => '127.0.0.1', 'agent' => "Totally-Not-FireFox" - ] + ], + $cpIndex ); - $lb->expects( $this->once() ) - ->method( 'waitFor' )->with( $this->equalTo( $mPos ) ); + // Wait for last positions to be reached on second HTTP request start + $cp->initLB( $lb1 ); + $cp->initLB( $lb2 ); + // Shutdown (nothing to record) + $cp->shutdownLB( $lb1 ); + $cp->shutdownLB( $lb2 ); + $cpIndex = null; + $cp->shutdown( null, 'sync', $cpIndex ); - // Wait - $cp->initLB( $lb ); - // Record in stash - $cp->shutdownLB( $lb ); - $cp->shutdown(); + $this->assertEquals( null, $cpIndex, "CP write index retained" ); } private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) { - global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir; + global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype; + global $wgSQLiteDataDir; return new LBFactoryMulti( $baseOverride + [ 'sectionsByDB' => [], @@ -261,6 +329,7 @@ class LBFactoryTest extends MediaWikiTestCase { ], 'serverTemplate' => $serverOverride + [ 'dbname' => $wgDBname, + 'tablePrefix' => $wgDBprefix, 'user' => $wgDBuser, 'password' => $wgDBpassword, 'type' => $wgDBtype, @@ -270,8 +339,8 @@ class LBFactoryTest extends MediaWikiTestCase { 'hostsByName' => [ 'test-db1' => $wgDBserver, ], - 'loadMonitorClass' => 'LoadMonitorNull', - 'localDomain' => wfWikiID() + 'loadMonitorClass' => LoadMonitorNull::class, + 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ) ] ); } @@ -297,7 +366,7 @@ class LBFactoryTest extends MediaWikiTestCase { if ( $wgDBtype !== 'sqlite' ) { $db = $lb->getConnectionRef( DB_MASTER ); $this->assertEquals( - $wgDBname, + wfWikiID(), $db->getDomainID() ); unset( $db ); @@ -305,34 +374,45 @@ class LBFactoryTest extends MediaWikiTestCase { /** @var Database $db */ $db = $lb->getConnection( DB_MASTER, [], '' ); - $lb->reuseConnection( $db ); // don't care $this->assertEquals( '', - $db->getDomainID() + $db->getDomainId(), + 'Null domain ID handle used' + ); + $this->assertEquals( + '', + $db->getDBname(), + 'Null domain ID handle used' + ); + $this->assertEquals( + '', + $db->tablePrefix(), + 'Main domain ID handle used; prefix is empty though' ); - $this->assertEquals( $this->quoteTable( $db, 'page' ), $db->tableName( 'page' ), "Correct full table name" ); - $this->assertEquals( $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( "$wgDBname.page" ), "Correct full table name" ); - $this->assertEquals( $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'nice_db.page' ), "Correct full table name" ); + $lb->reuseConnection( $db ); // don't care + + $db = $lb->getConnection( DB_MASTER ); // local domain connection $factory->setDomainPrefix( 'my_' ); + $this->assertEquals( - '', + "$wgDBname-my_", $db->getDomainID() ); $this->assertEquals( @@ -351,7 +431,7 @@ class LBFactoryTest extends MediaWikiTestCase { } public function testTrickyDomain() { - global $wgDBtype; + global $wgDBtype, $wgDBname; if ( $wgDBtype === 'sqlite' ) { $tmpDir = $this->getNewTempDirectory(); @@ -363,20 +443,19 @@ class LBFactoryTest extends MediaWikiTestCase { $dbPath = null; } - $dbname = 'unittest-domain'; + $dbname = 'unittest-domain'; // explodes if DB is selected $factory = $this->newLBFactoryMulti( - [ 'localDomain' => $dbname ], - [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ] + [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ], + [ + 'dbFilePath' => $dbPath, + 'dbName' => 'do_not_select_me' // explodes if DB is selected + ] ); $lb = $factory->getMainLB(); /** @var Database $db */ $db = $lb->getConnection( DB_MASTER, [], '' ); - $lb->reuseConnection( $db ); // don't care - $this->assertEquals( - '', - $db->getDomainID() - ); + $this->assertEquals( '', $db->getDomainID(), "Null domain used" ); $this->assertEquals( $this->quoteTable( $db, 'page' ), @@ -396,7 +475,10 @@ class LBFactoryTest extends MediaWikiTestCase { "Correct full table name" ); + $lb->reuseConnection( $db ); // don't care + $factory->setDomainPrefix( 'my_' ); + $db = $lb->getConnection( DB_MASTER, [], "$wgDBname-my_" ); $this->assertEquals( $this->quoteTable( $db, 'my_page' ), @@ -408,7 +490,6 @@ class LBFactoryTest extends MediaWikiTestCase { $db->tableName( 'other_nice_db.page' ), "Correct full table name" ); - $this->assertEquals( $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ), $db->tableName( 'garbage-db.page' ), @@ -422,7 +503,7 @@ class LBFactoryTest extends MediaWikiTestCase { } catch ( \Wikimedia\Rdbms\DBConnectionError $e ) { // expected } - $this->assertInstanceOf( '\Wikimedia\Rdbms\DBConnectionError', $e ); + $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError::class, $e ); $this->assertFalse( $db->isOpen() ); } else { \MediaWiki\suppressWarnings(); @@ -430,6 +511,8 @@ class LBFactoryTest extends MediaWikiTestCase { \MediaWiki\restoreWarnings(); } + $lb->reuseConnection( $db ); // don't care + $factory->closeAll(); $factory->destroy(); }