|
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 file_get_contents;
|
16 | 19 | use function get_object_vars;
|
17 | 20 | use function glob;
|
@@ -209,6 +212,84 @@ public function provideTests()
|
209 | 212 | return $testArgs;
|
210 | 213 | }
|
211 | 214 |
|
| 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 | + |
212 | 293 | /**
|
213 | 294 | * Create the collection, since it cannot be created within a transaction.
|
214 | 295 | */
|
|
0 commit comments