-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPLIB-476: Consider transaction readPreference in select_server (#1178)
This also refactors the conditionals in extract_session_from_options and extract_read_preference_from_options to improve readability. select_server() previously did not consider the read preference of an active transaction. This isn't very significant, as transactions require a primary read preference, but it is correct to do so. This also introduces select_server_for_write() to avoid inheriting an active transaction's readPreference option.
- Loading branch information
Showing
5 changed files
with
119 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests\Functions; | ||
|
||
use MongoDB\Driver\ReadPreference; | ||
use MongoDB\Driver\Server; | ||
use MongoDB\Tests\FunctionalTestCase; | ||
|
||
use function MongoDB\select_server; | ||
|
||
class SelectServerFunctionalTest extends FunctionalTestCase | ||
{ | ||
/** @dataProvider providePinnedOptions */ | ||
public function testSelectServerPrefersPinnedServer(array $options): void | ||
{ | ||
$this->skipIfTransactionsAreNotSupported(); | ||
|
||
if (! $this->isShardedCluster()) { | ||
$this->markTestSkipped('Pinning requires a sharded cluster'); | ||
} | ||
|
||
if ($this->isLoadBalanced()) { | ||
$this->markTestSkipped('libmongoc does not pin for load-balanced topology'); | ||
} | ||
|
||
/* By default, the Manager under test is created with a single-mongos | ||
* URI. Explicitly create a Client with multiple mongoses. */ | ||
$client = static::createTestClient(static::getUri(true)); | ||
|
||
// Collection must be created before the transaction starts | ||
$this->createCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
|
||
$session = $client->startSession(); | ||
$session->startTransaction(); | ||
|
||
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
$collection->find([], ['session' => $session]); | ||
|
||
$this->assertTrue($session->isInTransaction()); | ||
$this->assertInstanceOf(Server::class, $session->getServer(), 'Session is pinned'); | ||
$this->assertEquals($session->getServer(), select_server($client->getManager(), ['session' => $session])); | ||
} | ||
|
||
public static function providePinnedOptions(): array | ||
{ | ||
return [ | ||
[['readPreference' => new ReadPreference(ReadPreference::PRIMARY_PREFERRED)]], | ||
[[]], | ||
]; | ||
} | ||
} |