3 namespace Wikimedia\Tests\Rdbms
;
6 use Wikimedia\Rdbms\LoadBalancer
;
7 use PHPUnit_Framework_MockObject_MockObject
;
8 use Wikimedia\Rdbms\SessionConsistentConnectionManager
;
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
14 * @author Daniel Kinzler
16 class SessionConsistentConnectionManagerTest
extends \PHPUnit_Framework_TestCase
{
19 * @return IDatabase|PHPUnit_Framework_MockObject_MockObject
21 private function getIDatabaseMock() {
22 return $this->getMockBuilder( IDatabase
::class )
27 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
29 private function getLoadBalancerMock() {
30 $lb = $this->getMockBuilder( LoadBalancer
::class )
31 ->disableOriginalConstructor()
37 public function testGetReadConnection() {
38 $database = $this->getIDatabaseMock();
39 $lb = $this->getLoadBalancerMock();
41 $lb->expects( $this->once() )
42 ->method( 'getConnection' )
44 ->will( $this->returnValue( $database ) );
46 $manager = new SessionConsistentConnectionManager( $lb );
47 $actual = $manager->getReadConnection();
49 $this->assertSame( $database, $actual );
52 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
53 $database = $this->getIDatabaseMock();
54 $lb = $this->getLoadBalancerMock();
56 $lb->expects( $this->once() )
57 ->method( 'getConnection' )
59 ->will( $this->returnValue( $database ) );
61 $manager = new SessionConsistentConnectionManager( $lb );
62 $manager->prepareForUpdates();
63 $actual = $manager->getReadConnection();
65 $this->assertSame( $database, $actual );
68 public function testGetWriteConnection() {
69 $database = $this->getIDatabaseMock();
70 $lb = $this->getLoadBalancerMock();
72 $lb->expects( $this->once() )
73 ->method( 'getConnection' )
75 ->will( $this->returnValue( $database ) );
77 $manager = new SessionConsistentConnectionManager( $lb );
78 $actual = $manager->getWriteConnection();
80 $this->assertSame( $database, $actual );
83 public function testForceMaster() {
84 $database = $this->getIDatabaseMock();
85 $lb = $this->getLoadBalancerMock();
87 $lb->expects( $this->once() )
88 ->method( 'getConnection' )
90 ->will( $this->returnValue( $database ) );
92 $manager = new SessionConsistentConnectionManager( $lb );
93 $manager->prepareForUpdates();
94 $manager->getReadConnection();
97 public function testReleaseConnection() {
98 $database = $this->getIDatabaseMock();
99 $lb = $this->getLoadBalancerMock();
101 $lb->expects( $this->once() )
102 ->method( 'reuseConnection' )
104 ->will( $this->returnValue( null ) );
106 $manager = new SessionConsistentConnectionManager( $lb );
107 $manager->releaseConnection( $database );