Skip to content

Commit f970795

Browse files
committed
PHPLIB-466: Implement mongos pinning prose tests
1 parent 45346d1 commit f970795

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

0 commit comments

Comments
 (0)