| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use MediaWikiUnitTestCase; |
| 6 |
use Wikimedia\Rdbms\IDatabase; |
| 7 |
use Wikimedia\Rdbms\LoadBalancer; |
| 8 |
use Wikimedia\Rdbms\SessionConsistentConnectionManager; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class SessionConsistentConnectionManagerTest extends MediaWikiUnitTestCase { |
| 16 |
|
| 17 |
public function testGetReadConnection() { |
| 18 |
$database = $this->createMock( IDatabase::class ); |
| 19 |
$lb = $this->createMock( LoadBalancer::class ); |
| 20 |
|
| 21 |
$lb->expects( $this->once() ) |
| 22 |
->method( 'getConnection' ) |
| 23 |
->with( DB_REPLICA ) |
| 24 |
->willReturn( $database ); |
| 25 |
|
| 26 |
$manager = new SessionConsistentConnectionManager( $lb ); |
| 27 |
$actual = $manager->getReadConnection(); |
| 28 |
|
| 29 |
$this->assertSame( $database, $actual ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testGetReadConnectionReturnsWriteDbOnForceMaster() { |
| 33 |
$database = $this->createMock( IDatabase::class ); |
| 34 |
$lb = $this->createMock( LoadBalancer::class ); |
| 35 |
|
| 36 |
$lb->expects( $this->once() ) |
| 37 |
->method( 'getConnection' ) |
| 38 |
->with( DB_PRIMARY ) |
| 39 |
->willReturn( $database ); |
| 40 |
|
| 41 |
$manager = new SessionConsistentConnectionManager( $lb ); |
| 42 |
$manager->prepareForUpdates(); |
| 43 |
$actual = $manager->getReadConnection(); |
| 44 |
|
| 45 |
$this->assertSame( $database, $actual ); |
| 46 |
} |
| 47 |
|
| 48 |
public function testGetWriteConnection() { |
| 49 |
$database = $this->createMock( IDatabase::class ); |
| 50 |
$lb = $this->createMock( LoadBalancer::class ); |
| 51 |
|
| 52 |
$lb->expects( $this->once() ) |
| 53 |
->method( 'getConnection' ) |
| 54 |
->with( DB_PRIMARY ) |
| 55 |
->willReturn( $database ); |
| 56 |
|
| 57 |
$manager = new SessionConsistentConnectionManager( $lb ); |
| 58 |
$actual = $manager->getWriteConnection(); |
| 59 |
|
| 60 |
$this->assertSame( $database, $actual ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testForceMaster() { |
| 64 |
$database = $this->createMock( IDatabase::class ); |
| 65 |
$lb = $this->createMock( LoadBalancer::class ); |
| 66 |
|
| 67 |
$lb->expects( $this->once() ) |
| 68 |
->method( 'getConnection' ) |
| 69 |
->with( DB_PRIMARY ) |
| 70 |
->willReturn( $database ); |
| 71 |
|
| 72 |
$manager = new SessionConsistentConnectionManager( $lb ); |
| 73 |
$manager->prepareForUpdates(); |
| 74 |
$manager->getReadConnection(); |
| 75 |
} |
| 76 |
} |