Skip to content

Commit 9281873

Browse files
committed
PHPLIB-466: Implement mongos pinning prose tests
1 parent e774434 commit 9281873

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/SpecTests/TransactionsSpecTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
use MongoDB\BSON\Int64;
66
use MongoDB\BSON\Timestamp;
7+
use MongoDB\Client;
78
use MongoDB\Driver\Command;
89
use MongoDB\Driver\Exception\ServerException;
910
use MongoDB\Driver\Manager;
1011
use MongoDB\Driver\ReadPreference;
1112
use MongoDB\Driver\Server;
1213
use stdClass;
1314
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
15+
use function array_unique;
1416
use function basename;
17+
use function count;
1518
use function dirname;
1619
use function file_get_contents;
1720
use function get_object_vars;
@@ -205,6 +208,80 @@ public function provideTests()
205208
return $testArgs;
206209
}
207210

211+
/**
212+
* Prose test 1: Test that starting a new transaction on a pinned
213+
* ClientSession unpins the session and normal server selection is performed
214+
* for the next operation.
215+
*/
216+
public function testStartingNewTransactionOnPinnedSessionUnpinsSession()
217+
{
218+
if (! $this->isShardedClusterUsingReplicasets()) {
219+
$this->markTestSkipped('Mongos pinning tests can only run on sharded clusters using replica sets');
220+
}
221+
222+
$client = new Client($this->getUri(true));
223+
224+
$session = $client->startSession();
225+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
226+
227+
// Create collection before transaction
228+
$collection->insertOne([]);
229+
230+
$session->startTransaction([]);
231+
$collection->insertOne([], ['session' => $session]);
232+
$session->commitTransaction();
233+
234+
$servers = [];
235+
for ($i = 0; $i < 50; $i++) {
236+
$session->startTransaction([]);
237+
$cursor = $collection->find([], ['session' => $session]);
238+
$servers[] = $cursor->getServer()->getHost() . ':' . $cursor->getServer()->getPort();
239+
$this->assertInstanceOf(Server::class, $session->getServer());
240+
$session->commitTransaction();
241+
}
242+
243+
$servers = array_unique($servers);
244+
$this->assertGreaterThan(1, count($servers));
245+
246+
$session->endSession();
247+
}
248+
249+
/**
250+
* Prose test 2: Test non-transaction operations using a pinned
251+
* ClientSession unpins the session and normal server selection is
252+
* performed.
253+
*/
254+
public function testRunningNonTransactionOperationOnPinnedSessionUnpinsSession()
255+
{
256+
if (! $this->isShardedClusterUsingReplicasets()) {
257+
$this->markTestSkipped('Mongos pinning tests can only run on sharded clusters using replica sets');
258+
}
259+
260+
$client = new Client($this->getUri(true));
261+
262+
$session = $client->startSession();
263+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
264+
265+
// Create collection before transaction
266+
$collection->insertOne([]);
267+
268+
$session->startTransaction([]);
269+
$collection->insertOne([], ['session' => $session]);
270+
$session->commitTransaction();
271+
272+
$servers = [];
273+
for ($i = 0; $i < 50; $i++) {
274+
$cursor = $collection->find([], ['session' => $session]);
275+
$servers[] = $cursor->getServer()->getHost() . ':' . $cursor->getServer()->getPort();
276+
$this->assertNull($session->getServer());
277+
}
278+
279+
$servers = array_unique($servers);
280+
$this->assertGreaterThan(1, count($servers));
281+
282+
$session->endSession();
283+
}
284+
208285
/**
209286
* Create the collection, since it cannot be created within a transaction.
210287
*/

0 commit comments

Comments
 (0)