2016-11-21 13:26:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\Tests\Rdbms;
|
|
|
|
|
|
2018-01-04 22:18:55 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-02-18 00:26:47 +00:00
|
|
|
use Wikimedia\Rdbms\LoadBalancer;
|
2016-11-21 13:26:07 +00:00
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
|
|
|
|
use Wikimedia\Rdbms\SessionConsistentConnectionManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
|
|
|
|
|
*
|
|
|
|
|
* @author Daniel Kinzler
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class SessionConsistentConnectionManagerTest extends \PHPUnit\Framework\TestCase {
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return IDatabase|PHPUnit_Framework_MockObject_MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getIDatabaseMock() {
|
2017-04-05 23:39:50 +00:00
|
|
|
return $this->getMockBuilder( IDatabase::class )
|
|
|
|
|
->getMock();
|
2016-11-21 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getLoadBalancerMock() {
|
|
|
|
|
$lb = $this->getMockBuilder( LoadBalancer::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
return $lb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetReadConnection() {
|
|
|
|
|
$database = $this->getIDatabaseMock();
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
|
|
|
|
->with( DB_REPLICA )
|
|
|
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$actual = $manager->getReadConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
|
|
|
|
|
$database = $this->getIDatabaseMock();
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
|
|
|
|
->with( DB_MASTER )
|
|
|
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$manager->prepareForUpdates();
|
|
|
|
|
$actual = $manager->getReadConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetWriteConnection() {
|
|
|
|
|
$database = $this->getIDatabaseMock();
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
|
|
|
|
->with( DB_MASTER )
|
|
|
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$actual = $manager->getWriteConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testForceMaster() {
|
|
|
|
|
$database = $this->getIDatabaseMock();
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
|
|
|
|
->with( DB_MASTER )
|
|
|
|
|
->will( $this->returnValue( $database ) );
|
|
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$manager->prepareForUpdates();
|
|
|
|
|
$manager->getReadConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReleaseConnection() {
|
|
|
|
|
$database = $this->getIDatabaseMock();
|
|
|
|
|
$lb = $this->getLoadBalancerMock();
|
|
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'reuseConnection' )
|
|
|
|
|
->with( $database )
|
|
|
|
|
->will( $this->returnValue( null ) );
|
|
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$manager->releaseConnection( $database );
|
|
|
|
|
}
|
|
|
|
|
}
|