|
| 1 | +--TEST-- |
| 2 | +MongoDB\Driver\Session::commitTransaction() applies w:majority when retrying |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> |
| 5 | +<?php skip_if_not_libmongoc_crypto() ?> |
| 6 | +<?php skip_if_not_replica_set(); ?> |
| 7 | +<?php skip_if_server_version('<', '4.0'); ?> |
| 8 | +<?php skip_if_not_clean(); ?> |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 12 | + |
| 13 | +class Test implements MongoDB\Driver\Monitoring\CommandSubscriber |
| 14 | +{ |
| 15 | + private $manager; |
| 16 | + |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + $this->manager = new MongoDB\Driver\Manager(URI); |
| 20 | + |
| 21 | + $this->manager->executeCommand( |
| 22 | + DATABASE_NAME, |
| 23 | + new MongoDB\Driver\Command(['create' => COLLECTION_NAME]), |
| 24 | + ['writeConcern' => new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY)] |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + public function run(array $startTransactionOptions) |
| 29 | + { |
| 30 | + $session = $this->manager->startSession(); |
| 31 | + |
| 32 | + $session->startTransaction($startTransactionOptions); |
| 33 | + |
| 34 | + $bulk = new MongoDB\Driver\BulkWrite; |
| 35 | + $bulk->insert(['x' => 1]); |
| 36 | + $this->manager->executeBulkWrite(NS, $bulk, ['session' => $session]); |
| 37 | + |
| 38 | + MongoDB\Driver\Monitoring\addSubscriber($this); |
| 39 | + |
| 40 | + $session->commitTransaction(); |
| 41 | + $session->commitTransaction(); |
| 42 | + |
| 43 | + MongoDB\Driver\Monitoring\removeSubscriber($this); |
| 44 | + } |
| 45 | + |
| 46 | + public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event) |
| 47 | + { |
| 48 | + if ($event->getCommandName() !== 'commitTransaction') { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + printf("commitTransaction included write concern: %s\n", json_encode($event->getCommand()->writeConcern)); |
| 53 | + } |
| 54 | + |
| 55 | + public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event) |
| 60 | + { |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +$test = new Test; |
| 65 | + |
| 66 | +echo "Applies w:majority and default wtimeout when retrying commitTransaction\n"; |
| 67 | +$test->run(['writeConcern' => new MongoDB\Driver\WriteConcern(1)]); |
| 68 | + |
| 69 | +echo "\nPreserves other WC options when retrying commitTransaction\n"; |
| 70 | +$test->run(['writeConcern' => new MongoDB\Driver\WriteConcern(1, 5000)]); |
| 71 | + |
| 72 | +?> |
| 73 | +===DONE=== |
| 74 | +<?php exit(0); ?> |
| 75 | +--EXPECT-- |
| 76 | +Applies w:majority and default wtimeout when retrying commitTransaction |
| 77 | +commitTransaction included write concern: {"w":1} |
| 78 | +commitTransaction included write concern: {"w":"majority","wtimeout":10000} |
| 79 | + |
| 80 | +Preserves other WC options when retrying commitTransaction |
| 81 | +commitTransaction included write concern: {"w":1,"wtimeout":5000} |
| 82 | +commitTransaction included write concern: {"w":"majority","wtimeout":5000} |
| 83 | +===DONE=== |
0 commit comments