2016-11-21 13:26:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\Tests\Rdbms;
|
|
|
|
|
|
2023-05-26 15:47:59 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
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 Wikimedia\Rdbms\SessionConsistentConnectionManager;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \Wikimedia\Rdbms\SessionConsistentConnectionManager
|
2016-11-21 13:26:07 +00:00
|
|
|
*
|
|
|
|
|
* @author Daniel Kinzler
|
|
|
|
|
*/
|
2023-05-26 15:47:59 +00:00
|
|
|
class SessionConsistentConnectionManagerTest extends MediaWikiUnitTestCase {
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
public function testGetReadConnection() {
|
2022-08-05 10:47:38 +00:00
|
|
|
$database = $this->createMock( IDatabase::class );
|
|
|
|
|
$lb = $this->createMock( LoadBalancer::class );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
|
|
|
|
->with( DB_REPLICA )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $database );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$actual = $manager->getReadConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 06:38:30 +00:00
|
|
|
public function testGetReadConnectionReturnsWriteDbOnForceMaster() {
|
2022-08-05 10:47:38 +00:00
|
|
|
$database = $this->createMock( IDatabase::class );
|
|
|
|
|
$lb = $this->createMock( LoadBalancer::class );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
2021-04-29 02:37:11 +00:00
|
|
|
->with( DB_PRIMARY )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $database );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$manager->prepareForUpdates();
|
|
|
|
|
$actual = $manager->getReadConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetWriteConnection() {
|
2022-08-05 10:47:38 +00:00
|
|
|
$database = $this->createMock( IDatabase::class );
|
|
|
|
|
$lb = $this->createMock( LoadBalancer::class );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
2021-04-29 02:37:11 +00:00
|
|
|
->with( DB_PRIMARY )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $database );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$actual = $manager->getWriteConnection();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $database, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testForceMaster() {
|
2022-08-05 10:47:38 +00:00
|
|
|
$database = $this->createMock( IDatabase::class );
|
|
|
|
|
$lb = $this->createMock( LoadBalancer::class );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$lb->expects( $this->once() )
|
|
|
|
|
->method( 'getConnection' )
|
2021-04-29 02:37:11 +00:00
|
|
|
->with( DB_PRIMARY )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $database );
|
2016-11-21 13:26:07 +00:00
|
|
|
|
|
|
|
|
$manager = new SessionConsistentConnectionManager( $lb );
|
|
|
|
|
$manager->prepareForUpdates();
|
|
|
|
|
$manager->getReadConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|