3 namespace Wikimedia\Tests\Rdbms
;
5 use Wikimedia\Rdbms\IDatabase
;
6 use Wikimedia\Rdbms\LoadBalancer
;
7 use PHPUnit_Framework_MockObject_MockObject
;
8 use Wikimedia\Rdbms\SessionConsistentConnectionManager
;
11 * @covers Wikimedia\Rdbms\SessionConsistentConnectionManager
13 * @author Daniel Kinzler
15 class SessionConsistentConnectionManagerTest
extends \PHPUnit\Framework\TestCase
{
16 use \PHPUnit4And6Compat
;
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 return $this->createMock( LoadBalancer
::class );
33 public function testGetReadConnection() {
34 $database = $this->getIDatabaseMock();
35 $lb = $this->getLoadBalancerMock();
37 $lb->expects( $this->once() )
38 ->method( 'getConnection' )
40 ->will( $this->returnValue( $database ) );
42 $manager = new SessionConsistentConnectionManager( $lb );
43 $actual = $manager->getReadConnection();
45 $this->assertSame( $database, $actual );
48 public function testGetReadConnectionReturnsWriteDbOnForceMatser() {
49 $database = $this->getIDatabaseMock();
50 $lb = $this->getLoadBalancerMock();
52 $lb->expects( $this->once() )
53 ->method( 'getConnection' )
55 ->will( $this->returnValue( $database ) );
57 $manager = new SessionConsistentConnectionManager( $lb );
58 $manager->prepareForUpdates();
59 $actual = $manager->getReadConnection();
61 $this->assertSame( $database, $actual );
64 public function testGetWriteConnection() {
65 $database = $this->getIDatabaseMock();
66 $lb = $this->getLoadBalancerMock();
68 $lb->expects( $this->once() )
69 ->method( 'getConnection' )
71 ->will( $this->returnValue( $database ) );
73 $manager = new SessionConsistentConnectionManager( $lb );
74 $actual = $manager->getWriteConnection();
76 $this->assertSame( $database, $actual );
79 public function testForceMaster() {
80 $database = $this->getIDatabaseMock();
81 $lb = $this->getLoadBalancerMock();
83 $lb->expects( $this->once() )
84 ->method( 'getConnection' )
86 ->will( $this->returnValue( $database ) );
88 $manager = new SessionConsistentConnectionManager( $lb );
89 $manager->prepareForUpdates();
90 $manager->getReadConnection();
93 public function testReleaseConnection() {
94 $database = $this->getIDatabaseMock();
95 $lb = $this->getLoadBalancerMock();
97 $lb->expects( $this->once() )
98 ->method( 'reuseConnection' )
100 ->will( $this->returnValue( null ) );
102 $manager = new SessionConsistentConnectionManager( $lb );
103 $manager->releaseConnection( $database );