Skip to content

Commit 5f6af21

Browse files
committed
PHPLIB-466: Implement mongos pinning prose tests
1 parent ecb5538 commit 5f6af21

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tests/SpecTests/TransactionsSpecTest.php

Lines changed: 81 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 file_get_contents;
1619
use function get_object_vars;
1720
use function glob;
@@ -209,6 +212,84 @@ public function provideTests()
209212
return $testArgs;
210213
}
211214

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

0 commit comments

Comments
 (0)