|
4 | 4 |
|
5 | 5 | use MongoDB\BSON\Int64;
|
6 | 6 | use MongoDB\BSON\Timestamp;
|
| 7 | +use MongoDB\Client; |
7 | 8 | use MongoDB\Driver\Command;
|
8 | 9 | use MongoDB\Driver\Exception\ServerException;
|
9 | 10 | use MongoDB\Driver\Manager;
|
10 | 11 | use MongoDB\Driver\ReadPreference;
|
11 | 12 | use MongoDB\Driver\Server;
|
12 | 13 | use stdClass;
|
13 | 14 | use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
|
| 15 | +use function array_unique; |
14 | 16 | use function basename;
|
| 17 | +use function count; |
15 | 18 | use function dirname;
|
16 | 19 | use function file_get_contents;
|
17 | 20 | use function get_object_vars;
|
@@ -205,6 +208,80 @@ public function provideTests()
|
205 | 208 | return $testArgs;
|
206 | 209 | }
|
207 | 210 |
|
| 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 | + |
208 | 285 | /**
|
209 | 286 | * Create the collection, since it cannot be created within a transaction.
|
210 | 287 | */
|
|
0 commit comments