Commit ba0b611a authored by Jeremy Mikola's avatar Jeremy Mikola

Regression tests for find() and aggregate() using primary RP within transaction

This bumps the PHPC requirement to 1.5.2+, since that includes libmongoc 1.12 and CDRIVER-2744.
parent cad17308
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
"php": ">=5.5", "php": ">=5.5",
"ext-hash": "*", "ext-hash": "*",
"ext-json": "*", "ext-json": "*",
"ext-mongodb": "^1.5.0" "ext-mongodb": "^1.5.2"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.8.36 || ^6.4" "phpunit/phpunit": "^4.8.36 || ^6.4"
......
...@@ -10,8 +10,10 @@ use MongoDB\Driver\ReadPreference; ...@@ -10,8 +10,10 @@ use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Operation\Count; use MongoDB\Operation\Count;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\MapReduce; use MongoDB\Operation\MapReduce;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use Exception;
use stdClass; use stdClass;
/** /**
...@@ -104,6 +106,39 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -104,6 +106,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace()); $this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
} }
public function testAggregateWithinTransaction()
{
$this->skipIfTransactionsAreNotSupported();
// Collection must be created before the transaction starts
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
$operation->execute($this->getPrimaryServer());
$session = $this->manager->startSession();
$session->startTransaction();
try {
$this->createFixtures(3, ['session' => $session]);
$cursor = $this->collection->aggregate(
[['$match' => ['_id' => ['$lt' => 3]]]],
['session' => $session]
);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
];
$this->assertSameDocuments($expected, $cursor);
$session->commitTransaction();
} finally {
$session->endSession();
}
}
public function testCreateIndexSplitsCommandOptions() public function testCreateIndexSplitsCommandOptions()
{ {
if (version_compare($this->getServerVersion(), '3.6.0', '<')) { if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
...@@ -179,6 +214,39 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -179,6 +214,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertSameDocument($expected, $this->collection->findOne($filter, $options)); $this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
} }
public function testFindWithinTransaction()
{
$this->skipIfTransactionsAreNotSupported();
// Collection must be created before the transaction starts
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
$operation->execute($this->getPrimaryServer());
$session = $this->manager->startSession();
$session->startTransaction();
try {
$this->createFixtures(3, ['session' => $session]);
$cursor = $this->collection->find(
['_id' => ['$lt' => 3]],
['session' => $session]
);
$expected = [
['_id' => 1, 'x' => 11],
['_id' => 2, 'x' => 22],
];
$this->assertSameDocuments($expected, $cursor);
$session->commitTransaction();
} finally {
$session->endSession();
}
}
public function testWithOptionsInheritsOptions() public function testWithOptionsInheritsOptions()
{ {
$collectionOptions = [ $collectionOptions = [
...@@ -252,8 +320,9 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -252,8 +320,9 @@ class CollectionFunctionalTest extends FunctionalTestCase
* Create data fixtures. * Create data fixtures.
* *
* @param integer $n * @param integer $n
* @param array $executeBulkWriteOptions
*/ */
private function createFixtures($n) private function createFixtures($n, array $executeBulkWriteOptions = [])
{ {
$bulkWrite = new BulkWrite(['ordered' => true]); $bulkWrite = new BulkWrite(['ordered' => true]);
...@@ -264,7 +333,7 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -264,7 +333,7 @@ class CollectionFunctionalTest extends FunctionalTestCase
]); ]);
} }
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite, $executeBulkWriteOptions);
$this->assertEquals($n, $result->getInsertedCount()); $this->assertEquals($n, $result->getInsertedCount());
} }
......
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException; use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Operation\Aggregate; use MongoDB\Operation\Aggregate;
use MongoDB\Operation\CreateCollection;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use ArrayIterator; use ArrayIterator;
use Exception;
use stdClass; use stdClass;
class AggregateFunctionalTest extends FunctionalTestCase class AggregateFunctionalTest extends FunctionalTestCase
...@@ -274,12 +277,50 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -274,12 +277,50 @@ class AggregateFunctionalTest extends FunctionalTestCase
]; ];
} }
public function testReadPreferenceWithinTransaction()
{
$this->skipIfTransactionsAreNotSupported();
// Collection must be created before the transaction starts
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
$operation->execute($this->getPrimaryServer());
$session = $this->manager->startSession();
$session->startTransaction();
try {
$this->createFixtures(3, ['session' => $session]);
$pipeline = [['$match' => ['_id' => ['$lt' => 3]]]];
$options = [
'readPreference' => new ReadPreference('primary'),
'session' => $session,
];
$operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), $pipeline, $options);
$cursor = $operation->execute($this->getPrimaryServer());
$expected = [
['_id' => 1, 'x' => ['foo' => 'bar']],
['_id' => 2, 'x' => ['foo' => 'bar']],
];
$this->assertSameDocuments($expected, $cursor);
$session->commitTransaction();
} finally {
$session->endSession();
}
}
/** /**
* Create data fixtures. * Create data fixtures.
* *
* @param integer $n * @param integer $n
* @param array $executeBulkWriteOptions
*/ */
private function createFixtures($n) private function createFixtures($n, array $executeBulkWriteOptions = [])
{ {
$bulkWrite = new BulkWrite(['ordered' => true]); $bulkWrite = new BulkWrite(['ordered' => true]);
...@@ -290,7 +331,7 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -290,7 +331,7 @@ class AggregateFunctionalTest extends FunctionalTestCase
]); ]);
} }
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite, $executeBulkWriteOptions);
$this->assertEquals($n, $result->getInsertedCount()); $this->assertEquals($n, $result->getInsertedCount());
} }
......
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite; use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\CreateCollection; use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\CreateIndexes; use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropCollection; use MongoDB\Operation\DropCollection;
use MongoDB\Operation\Find; use MongoDB\Operation\Find;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use Exception;
use stdClass; use stdClass;
class FindFunctionalTest extends FunctionalTestCase class FindFunctionalTest extends FunctionalTestCase
...@@ -217,12 +220,50 @@ class FindFunctionalTest extends FunctionalTestCase ...@@ -217,12 +220,50 @@ class FindFunctionalTest extends FunctionalTestCase
$this->assertFalse($it->valid()); $this->assertFalse($it->valid());
} }
public function testReadPreferenceWithinTransaction()
{
$this->skipIfTransactionsAreNotSupported();
// Collection must be created before the transaction starts
$options = ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)];
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
$operation->execute($this->getPrimaryServer());
$session = $this->manager->startSession();
$session->startTransaction();
try {
$this->createFixtures(3, ['session' => $session]);
$filter = ['_id' => ['$lt' => 3]];
$options = [
'readPreference' => new ReadPreference('primary'),
'session' => $session,
];
$operation = new Find($this->getDatabaseName(), $this->getCollectionName(), $filter, $options);
$cursor = $operation->execute($this->getPrimaryServer());
$expected = [
['_id' => 1, 'x' => ['foo' => 'bar']],
['_id' => 2, 'x' => ['foo' => 'bar']],
];
$this->assertSameDocuments($expected, $cursor);
$session->commitTransaction();
} finally {
$session->endSession();
}
}
/** /**
* Create data fixtures. * Create data fixtures.
* *
* @param integer $n * @param integer $n
* @param array $executeBulkWriteOptions
*/ */
private function createFixtures($n) private function createFixtures($n, array $executeBulkWriteOptions = [])
{ {
$bulkWrite = new BulkWrite(['ordered' => true]); $bulkWrite = new BulkWrite(['ordered' => true]);
...@@ -233,7 +274,7 @@ class FindFunctionalTest extends FunctionalTestCase ...@@ -233,7 +274,7 @@ class FindFunctionalTest extends FunctionalTestCase
]); ]);
} }
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite, $executeBulkWriteOptions);
$this->assertEquals($n, $result->getInsertedCount()); $this->assertEquals($n, $result->getInsertedCount());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment