diff options
author | Gregory Wlodarek <gregory.wlodarek@mongodb.com> | 2020-07-14 11:20:55 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-07-14 17:03:46 +0000 |
commit | aaeb549ed1e6804e229c1db09fc60260bfd82a62 (patch) | |
tree | 3e8f05835bf9d00d4d7c7abbbf78bae328a39d1c | |
parent | 5d787e11adfab43295fc7e4b72449cc1bec81594 (diff) | |
download | mongo-aaeb549ed1e6804e229c1db09fc60260bfd82a62.tar.gz |
SERVER-49155 Remove mock objects from MultiIndexBlockTest and test the onRollback() commit handler for MultiIndexBlock::init()
-rw-r--r-- | src/mongo/db/catalog/multi_index_block_test.cpp | 131 |
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 a49464eef01..61b8a0bc6e5 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->abortWithoutCleanupForRollback(getOpCtx()); + ASSERT_OK(indexer->insert(operationContext(), {}, {})); + indexer->abortWithoutCleanupForRollback(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 |