summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Wlodarek <gregory.wlodarek@mongodb.com>2020-07-14 11:20:55 -0400
committerGregory Wlodarek <gregory.wlodarek@mongodb.com>2020-07-17 01:21:55 -0400
commitf55c432ba68ef32a430ba3a92cd1f564c0105c20 (patch)
tree56e94267565e8be3401dbf8524608a7b66b60b6f
parentb29c1758d425a73daddda6256d677add47683c59 (diff)
downloadmongo-f55c432ba68ef32a430ba3a92cd1f564c0105c20.tar.gz
SERVER-49155 Remove mock objects from MultiIndexBlockTest and test the onRollback() commit handler for MultiIndexBlock::init()
(cherry picked from commit aaeb549ed1e6804e229c1db09fc60260bfd82a62)
-rw-r--r--src/mongo/db/catalog/multi_index_block_test.cpp131
1 files changed, 77 insertions, 54 deletions
diff --git a/src/mongo/db/catalog/multi_index_block_test.cpp b/src/mongo/db/catalog/multi_index_block_test.cpp
index 3f0615b14aa..4b3eaf3e5d1 100644
--- a/src/mongo/db/catalog/multi_index_block_test.cpp
+++ b/src/mongo/db/catalog/multi_index_block_test.cpp
@@ -31,11 +31,10 @@
#include "mongo/db/catalog/multi_index_block.h"
-#include "mongo/db/catalog/collection_mock.h"
-#include "mongo/db/catalog/index_catalog_noop.h"
-#include "mongo/db/jsobj.h"
+#include "mongo/db/catalog/catalog_test_fixture.h"
+#include "mongo/db/catalog_raii.h"
+#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/repl/replication_coordinator_mock.h"
-#include "mongo/db/service_context_test_fixture.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
@@ -43,41 +42,39 @@ namespace {
/**
* Unit test for MultiIndexBlock to verify basic functionality.
- * Using a mocked Collection object ensures that we are pulling in a minimal set of library
- * dependencies.
- * For integration tests, it may be necessary to make this test fixture inherit from
- * ServiceContextMongoDTest.
*/
-class MultiIndexBlockTest : public ServiceContextTest {
+class MultiIndexBlockTest : public CatalogTestFixture {
private:
void setUp() override;
void tearDown() override;
protected:
- OperationContext* getOpCtx() const;
- Collection* getCollection() const;
- MultiIndexBlock* getIndexer() const;
+ NamespaceString getNSS() const {
+ return _nss;
+ }
+
+ MultiIndexBlock* getIndexer() const {
+ return _indexer.get();
+ }
private:
- ServiceContext::UniqueOperationContext _opCtx;
- std::unique_ptr<Collection> _collection;
+ NamespaceString _nss;
std::unique_ptr<MultiIndexBlock> _indexer;
};
void MultiIndexBlockTest::setUp() {
- ServiceContextTest::setUp();
+ CatalogTestFixture::setUp();
auto service = getServiceContext();
repl::ReplicationCoordinator::set(service,
std::make_unique<repl::ReplicationCoordinatorMock>(service));
- _opCtx = makeOperationContext();
+ _nss = NamespaceString("db.coll");
- NamespaceString nss("mydb.mycoll");
- auto collectionMock =
- std::make_unique<CollectionMock>(nss, std::make_unique<IndexCatalogNoop>());
- _collection = std::move(collectionMock);
+ CollectionOptions options;
+ options.uuid = UUID::gen();
+ ASSERT_OK(storageInterface()->createCollection(operationContext(), _nss, options));
_indexer = std::make_unique<MultiIndexBlock>();
}
@@ -87,39 +84,26 @@ void MultiIndexBlockTest::tearDown() {
_indexer = {};
- _collection = {};
-
- _opCtx = {};
-
- ServiceContextTest::tearDown();
-}
-
-OperationContext* MultiIndexBlockTest::getOpCtx() const {
- return _opCtx.get();
-}
-
-Collection* MultiIndexBlockTest::getCollection() const {
- return _collection.get();
-}
-
-MultiIndexBlock* MultiIndexBlockTest::getIndexer() const {
- return _indexer.get();
+ CatalogTestFixture::tearDown();
}
TEST_F(MultiIndexBlockTest, CommitWithoutInsertingDocuments) {
auto indexer = getIndexer();
+ AutoGetCollection autoColl(operationContext(), getNSS(), MODE_X);
+ Collection* coll = autoColl.getCollection();
+
auto specs = unittest::assertGet(indexer->init(
- getOpCtx(), getCollection(), std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
+ operationContext(), coll, std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
ASSERT_EQUALS(0U, specs.size());
- ASSERT_OK(indexer->dumpInsertsFromBulk(getOpCtx()));
- ASSERT_OK(indexer->checkConstraints(getOpCtx()));
+ ASSERT_OK(indexer->dumpInsertsFromBulk(operationContext()));
+ ASSERT_OK(indexer->checkConstraints(operationContext()));
{
- WriteUnitOfWork wunit(getOpCtx());
- ASSERT_OK(indexer->commit(getOpCtx(),
- getCollection(),
+ WriteUnitOfWork wunit(operationContext());
+ ASSERT_OK(indexer->commit(operationContext(),
+ coll,
MultiIndexBlock::kNoopOnCreateEachFn,
MultiIndexBlock::kNoopOnCommitFn));
wunit.commit();
@@ -129,34 +113,73 @@ TEST_F(MultiIndexBlockTest, CommitWithoutInsertingDocuments) {
TEST_F(MultiIndexBlockTest, CommitAfterInsertingSingleDocument) {
auto indexer = getIndexer();
+ AutoGetCollection autoColl(operationContext(), getNSS(), MODE_X);
+ Collection* coll = autoColl.getCollection();
+
auto specs = unittest::assertGet(indexer->init(
- getOpCtx(), getCollection(), std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
+ operationContext(), coll, std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
ASSERT_EQUALS(0U, specs.size());
- ASSERT_OK(indexer->insert(getOpCtx(), {}, {}));
- ASSERT_OK(indexer->dumpInsertsFromBulk(getOpCtx()));
- ASSERT_OK(indexer->checkConstraints(getOpCtx()));
+ ASSERT_OK(indexer->insert(operationContext(), {}, {}));
+ ASSERT_OK(indexer->dumpInsertsFromBulk(operationContext()));
+ ASSERT_OK(indexer->checkConstraints(operationContext()));
{
- WriteUnitOfWork wunit(getOpCtx());
- ASSERT_OK(indexer->commit(getOpCtx(),
- getCollection(),
+ WriteUnitOfWork wunit(operationContext());
+ ASSERT_OK(indexer->commit(operationContext(),
+ coll,
MultiIndexBlock::kNoopOnCreateEachFn,
MultiIndexBlock::kNoopOnCommitFn));
wunit.commit();
}
// abort() should have no effect after the index build is committed.
- indexer->abortIndexBuild(getOpCtx(), getCollection(), MultiIndexBlock::kNoopOnCleanUpFn);
+ indexer->abortIndexBuild(operationContext(), coll, MultiIndexBlock::kNoopOnCleanUpFn);
}
TEST_F(MultiIndexBlockTest, AbortWithoutCleanupAfterInsertingSingleDocument) {
auto indexer = getIndexer();
+
+ AutoGetCollection autoColl(operationContext(), getNSS(), MODE_X);
+ Collection* coll = autoColl.getCollection();
+
auto specs = unittest::assertGet(indexer->init(
- getOpCtx(), getCollection(), std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
+ operationContext(), coll, std::vector<BSONObj>(), MultiIndexBlock::kNoopOnInitFn));
ASSERT_EQUALS(0U, specs.size());
- ASSERT_OK(indexer->insert(getOpCtx(), {}, {}));
- indexer->abortWithoutCleanup(getOpCtx());
+ ASSERT_OK(indexer->insert(operationContext(), {}, {}));
+ indexer->abortWithoutCleanup(operationContext());
+}
+
+TEST_F(MultiIndexBlockTest, InitWriteConflictException) {
+ auto indexer = getIndexer();
+
+ AutoGetCollection autoColl(operationContext(), getNSS(), MODE_X);
+ Collection* coll = autoColl.getCollection();
+
+ BSONObj spec = BSON("key" << BSON("a" << 1) << "name"
+ << "a_1"
+ << "v" << static_cast<int>(IndexDescriptor::kLatestIndexVersion));
+
+ {
+ WriteUnitOfWork wuow(operationContext());
+ ASSERT_THROWS_CODE(indexer->init(operationContext(),
+ coll,
+ {spec},
+ [](std::vector<BSONObj>& specs) -> Status {
+ throw WriteConflictException();
+ }),
+ DBException,
+ ErrorCodes::WriteConflict);
+ }
+
+ {
+ WriteUnitOfWork wuow(operationContext());
+ ASSERT_OK(indexer->init(operationContext(), coll, {spec}, MultiIndexBlock::kNoopOnInitFn)
+ .getStatus());
+ wuow.commit();
+ }
+
+ indexer->abortIndexBuild(operationContext(), coll, MultiIndexBlock::kNoopOnCleanUpFn);
}
} // namespace