Skip to content

Commit 2789277

Browse files
committed
Merge pull request #671
2 parents fcd0d78 + d353a82 commit 2789277

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+18524
-13
lines changed

src/Operation/ListCollections.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private function executeCommand(Server $server)
130130
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
131131
}
132132

133-
$cursor = $server->executeCommand($this->databaseName, new Command($cmd), $this->createOptions());
133+
$cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
134134
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
135135

136136
return new CollectionInfoCommandIterator(new CachingIterator($cursor));

src/Operation/ListDatabases.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function execute(Server $server)
9898
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
9999
}
100100

101-
$cursor = $server->executeCommand('admin', new Command($cmd), $this->createOptions());
101+
$cursor = $server->executeReadCommand('admin', new Command($cmd), $this->createOptions());
102102
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
103103
$result = current($cursor->toArray());
104104

src/Operation/ListIndexes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function executeCommand(Server $server)
126126
}
127127

128128
try {
129-
$cursor = $server->executeCommand($this->databaseName, new Command($cmd), $this->createOptions());
129+
$cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
130130
} catch (DriverRuntimeException $e) {
131131
/* The server may return an error if the collection does not exist.
132132
* Check for possible error codes (see: SERVER-20463) and return an

src/Operation/MapReduce.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,15 @@ public function execute(Server $server)
271271
$command = $this->createCommand($server);
272272
$options = $this->createOptions($hasOutputCollection);
273273

274+
/* If the mapReduce operation results in a write, use
275+
* executeReadWriteCommand to ensure we're handling the writeConcern
276+
* option.
277+
* In other cases, we use executeCommand as this will prevent the
278+
* mapReduce operation from being retried when retryReads is enabled.
279+
* See https://github.com/mongodb/specifications/blob/master/source/retryable-reads/retryable-reads.rst#unsupported-read-operations. */
274280
$cursor = $hasOutputCollection
275281
? $server->executeReadWriteCommand($this->databaseName, $command, $options)
276-
: $server->executeReadCommand($this->databaseName, $command, $options);
282+
: $server->executeCommand($this->databaseName, $command, $options);
277283

278284
if (isset($this->options['typeMap']) && ! $hasOutputCollection) {
279285
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'results.$'));

tests/SpecTests/CommandExpectations.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ public static function fromCrud(array $expectedEvents)
7777
return $o;
7878
}
7979

80+
public static function fromRetryableReads(array $expectedEvents)
81+
{
82+
$o = new self($expectedEvents);
83+
84+
$o->ignoreCommandFailed = true;
85+
$o->ignoreCommandSucceeded = true;
86+
87+
/* Retryable read spec tests don't include extra commands, e.g. the
88+
* killCursors command issued when a change stream is garbage collected.
89+
* We ignore any extra events for that reason. \*/
90+
$o->ignoreExtraEvents = true;
91+
92+
return $o;
93+
}
94+
8095
public static function fromTransactions(array $expectedEvents)
8196
{
8297
$o = new self($expectedEvents);

tests/SpecTests/Context.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
final class Context
2323
{
24+
public $bucketName;
2425
public $client;
2526
public $collectionName;
2627
public $databaseName;
@@ -81,6 +82,19 @@ public static function fromCrud(stdClass $test, $databaseName, $collectionName)
8182
return $o;
8283
}
8384

85+
public static function fromRetryableReads(stdClass $test, $databaseName, $collectionName, $bucketName)
86+
{
87+
$o = new self($databaseName, $collectionName);
88+
89+
$o->bucketName = $bucketName;
90+
91+
$clientOptions = isset($test->clientOptions) ? (array) $test->clientOptions : [];
92+
93+
$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);
94+
95+
return $o;
96+
}
97+
8498
public static function fromRetryableWrites(stdClass $test, $databaseName, $collectionName)
8599
{
86100
$o = new self($databaseName, $collectionName);
@@ -133,6 +147,14 @@ public static function fromTransactions(stdClass $test, $databaseName, $collecti
133147
return $o;
134148
}
135149

150+
/**
151+
* @return Client
152+
*/
153+
public function getClient()
154+
{
155+
return $this->client;
156+
}
157+
136158
public function getCollection(array $collectionOptions = [])
137159
{
138160
return $this->selectCollection(
@@ -147,6 +169,11 @@ public function getDatabase(array $databaseOptions = [])
147169
return $this->selectDatabase($this->databaseName, $databaseOptions);
148170
}
149171

172+
public function getGridFSBucket(array $bucketOptions = [])
173+
{
174+
return $this->selectGridFSBucket($this->databaseName, $this->bucketName, $bucketOptions);
175+
}
176+
150177
/**
151178
* Prepare options readConcern, readPreference, and writeConcern options by
152179
* creating value objects.
@@ -272,6 +299,20 @@ public function selectDatabase($databaseName, array $databaseOptions = [])
272299
);
273300
}
274301

302+
public function selectGridFSBucket($databaseName, $bucketName, array $bucketOptions = [])
303+
{
304+
return $this->selectDatabase($databaseName)->selectGridFSBucket($this->prepareGridFSBucketOptions($bucketOptions, $bucketName));
305+
}
306+
307+
private function prepareGridFSBucketOptions(array $options, $bucketPrefix)
308+
{
309+
if ($bucketPrefix !== null) {
310+
$options['bucketPrefix'] = $bucketPrefix;
311+
}
312+
313+
return $options;
314+
}
315+
275316
private function prepareSessionOptions(array $options)
276317
{
277318
if (isset($options['defaultTransactionOptions'])) {

tests/SpecTests/ErrorExpectation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public static function fromChangeStreams(stdClass $result)
6060
return $o;
6161
}
6262

63+
public static function fromRetryableReads(stdClass $operation)
64+
{
65+
$o = new self();
66+
67+
if (isset($operation->error)) {
68+
$o->isExpected = $operation->error;
69+
}
70+
71+
return $o;
72+
}
73+
6374
public static function fromRetryableWrites(stdClass $outcome)
6475
{
6576
$o = new self();

tests/SpecTests/FunctionalTestCase.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,44 @@ protected function dropTestAndOutcomeCollections()
185185
{
186186
$context = $this->getContext();
187187

188-
$collection = $context->getCollection();
189-
$collection->drop($context->defaultWriteOptions);
188+
if ($context->bucketName !== null) {
189+
$bucket = $context->getGridFSBucket($context->defaultWriteOptions);
190+
$bucket->drop();
191+
}
190192

191-
$outcomeCollection = $this->getOutcomeCollection();
193+
$collection = null;
194+
if ($context->collectionName !== null) {
195+
$collection = $context->getCollection();
196+
$collection->drop($context->defaultWriteOptions);
197+
}
192198

193-
// Avoid redundant drop if the test and outcome collections are the same
194-
if ($outcomeCollection->getNamespace() !== $collection->getNamespace()) {
195-
$outcomeCollection->drop($context->defaultWriteOptions);
199+
if ($context->outcomeCollectionName !== null) {
200+
$outcomeCollection = $this->getOutcomeCollection();
201+
202+
// Avoid redundant drop if the test and outcome collections are the same
203+
if ($collection === null || $outcomeCollection->getNamespace() !== $collection->getNamespace()) {
204+
$outcomeCollection->drop($context->defaultWriteOptions);
205+
}
196206
}
197207
}
198208

199209
/**
200210
* Insert data fixtures into the test collection.
201211
*
202-
* @param array $documents
212+
* @param array $documents
213+
* @param string|null $collectionName
203214
*/
204-
protected function insertDataFixtures(array $documents)
215+
protected function insertDataFixtures(array $documents, $collectionName = null)
205216
{
206217
if (empty($documents)) {
207218
return;
208219
}
209220

210221
$context = $this->getContext();
211-
$collection = $context->getCollection();
222+
$collection = $collectionName ? $context->selectCollection($context->databaseName, $collectionName) : $context->getCollection();
212223
$collection->insertMany($documents, $context->defaultWriteOptions);
224+
225+
return;
213226
}
214227

215228
private function getOutcomeCollection()

0 commit comments

Comments
 (0)