Daniel Kinzler has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/401591 )
Change subject: Add test for LoadBalancer::getConenction with groups. ...................................................................... Add test for LoadBalancer::getConenction with groups. Change-Id: I37b0dfb6b356b4a6397b721de9ba88c76d2abbeb --- M tests/phpunit/includes/db/LoadBalancerTest.php 1 file changed, 83 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/91/401591/1 diff --git a/tests/phpunit/includes/db/LoadBalancerTest.php b/tests/phpunit/includes/db/LoadBalancerTest.php index 9b82c54..9a148ae 100644 --- a/tests/phpunit/includes/db/LoadBalancerTest.php +++ b/tests/phpunit/includes/db/LoadBalancerTest.php @@ -133,6 +133,11 @@ $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" ); $this->assertWriteForbidden( $dbr ); + $db1 = $lb->getConnection( $lb->getReaderIndex() ); + $this->assertSame( $dbr, $db1, 'getReaderIndex() is equivalent to DB_REPLICA' ); + $this->assertTrue( $db1->getLBInfo( 'replica' ), 'Reader shows as replica' ); + $this->assertWriteForbidden( $db1 ); + $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO ); $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" ); $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" ); @@ -149,6 +154,84 @@ $lb->closeAll(); } + public function testWithGroups() { + global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir; + + $servers = [ + [ // master + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 0, + 'flags' => DBO_TRX, // REPEATABLE-READ for consistency + 'test-name' => 'master', + ], + [ // emulated replica for default group + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 100, + 'groupLoads' => [ 'foo' => 0 ], + 'test-name' => 'def-replica', + ], + [ // emulated replica in foo group + 'host' => $wgDBserver, + 'dbname' => $wgDBname, + 'user' => $wgDBuser, + 'password' => $wgDBpassword, + 'type' => $wgDBtype, + 'dbDirectory' => $wgSQLiteDataDir, + 'load' => 0, + 'groupLoads' => [ 'foo' => 100 ], + 'test-name' => 'foo-replica', + ] + ]; + + $lb = new LoadBalancer( [ + 'servers' => $servers, + 'localDomain' => wfWikiID(), + 'loadMonitorClass' => 'LoadMonitorNull' + ] ); + + $db = $lb->getConnection( DB_MASTER ); + $this->assertSame( 'master', $db->getLBInfo( 'test-name' ), 'DB_MASTER gets master' ); + + $db = $lb->getConnection( $lb->getWriterIndex() ); + $this->assertSame( 'master', $db->getLBInfo( 'test-name' ), 'getWriterIndex get master' ); + + $db = $lb->getConnection( DB_REPLICA ); + $this->assertSame( 'def-replica', $db->getLBInfo( 'test-name' ), 'DB_REPLICA get replica' ); + + $db = $lb->getConnection( $lb->getReaderIndex() ); + $this->assertSame( 'def-replica', $db->getLBInfo( 'test-name' ), 'getReaderIndex get replica' ); + + $db = $lb->getConnection( DB_MASTER, [ 'foo' ] ); + $this->assertSame( 'master', $db->getLBInfo( 'test-name' ), 'DB_MASTER ignores group' ); + + $db = $lb->getConnection( $lb->getWriterIndex(), [ 'foo' ] ); + $this->assertSame( 'master', $db->getLBInfo( 'test-name' ), 'getWriterIndex ignores group' ); + + $db = $lb->getConnection( DB_REPLICA, [ 'foo' ] ); + $this->assertSame( 'foo-replica', $db->getLBInfo( 'test-name' ), 'DB_REPLICA uses group' ); + + $db = $lb->getConnection( $lb->getReaderIndex( 'foo' ) ); + $this->assertSame( 'foo-replica', $db->getLBInfo( 'test-name' ), 'getReaderIndex uses group' ); + + $db = $lb->getConnection( $lb->getReaderIndex(), [ 'foo' ] ); + $this->assertSame( 'def-replica', $db->getLBInfo( 'test-name' ), 'getReaderIndex forces group' ); + + $db = $lb->getConnection( DB_REPLICA, [ 'xyzzy' ] ); + $this->assertSame( 'def-replica', $db->getLBInfo( 'test-name' ), 'bad group falls back to default' ); + + $lb->closeAll(); + } + private function assertWriteForbidden( IDatabase $db ) { try { $db->delete( 'user', [ 'user_id' => 57634126 ], 'TEST' ); -- To view, visit https://gerrit.wikimedia.org/r/401591 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I37b0dfb6b356b4a6397b721de9ba88c76d2abbeb Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits