From b46ac14ee3895b8129069c8f1b03e3820ebcea99 Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Sun, 25 Apr 2021 18:28:02 -0400 Subject: SERVER-47123 remove AutoGetOrCreateDb from catalog unit tests AutoGetOrCreateDb is being deprecated in favor or AutoGetDb::ensureDbExists() and AutoGetCollection. --- src/mongo/db/catalog/create_collection_test.cpp | 23 ++++++------- src/mongo/db/catalog/database_test.cpp | 44 ++++++++++++------------- src/mongo/db/catalog/drop_database_test.cpp | 4 +-- src/mongo/db/catalog/rename_collection_test.cpp | 4 +-- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/mongo/db/catalog/create_collection_test.cpp b/src/mongo/db/catalog/create_collection_test.cpp index e53d80f8d63..e56a2fa58b0 100644 --- a/src/mongo/db/catalog/create_collection_test.cpp +++ b/src/mongo/db/catalog/create_collection_test.cpp @@ -34,7 +34,7 @@ #include "mongo/db/catalog/collection_catalog.h" #include "mongo/db/catalog/create_collection.h" #include "mongo/db/catalog/database_holder.h" -#include "mongo/db/client.h" +#include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/db_raii.h" #include "mongo/db/jsobj.h" #include "mongo/db/repl/replication_coordinator.h" @@ -96,21 +96,22 @@ void CreateCollectionTest::validateValidator(const std::string& validatorStr, NamespaceString newNss("test.newCollWithValidation"); auto opCtx = makeOpCtx(); - Lock::GlobalLock lk(opCtx.get(), MODE_X); // Satisfy low-level locking invariants. CollectionOptions options; options.validator = fromjson(validatorStr); options.uuid = UUID::gen(); - AutoGetOrCreateDb autoDb(opCtx.get(), newNss.db(), MODE_X); - auto db = autoDb.getDb(); - ASSERT_TRUE(db) << "Cannot create collection " << newNss << " because database " << newNss.db() - << " does not exist."; - - WriteUnitOfWork wuow(opCtx.get()); - const auto status = - db->userCreateNS(opCtx.get(), newNss, options, false /*createDefaultIndexes*/); - ASSERT_EQ(expectedError, status.code()); + return writeConflictRetry(opCtx.get(), "create", newNss.ns(), [&] { + AutoGetCollection autoColl(opCtx.get(), newNss, MODE_IX); + auto db = autoColl.ensureDbExists(); + ASSERT_TRUE(db) << "Cannot create collection " << newNss << " because database " + << newNss.db() << " does not exist."; + + WriteUnitOfWork wuow(opCtx.get()); + const auto status = + db->userCreateNS(opCtx.get(), newNss, options, false /*createDefaultIndexes*/); + ASSERT_EQ(expectedError, status.code()) << status; + }); } /** diff --git a/src/mongo/db/catalog/database_test.cpp b/src/mongo/db/catalog/database_test.cpp index 19f61925c5e..dde410d6f24 100644 --- a/src/mongo/db/catalog/database_test.cpp +++ b/src/mongo/db/catalog/database_test.cpp @@ -117,8 +117,8 @@ void DatabaseTest::tearDown() { TEST_F(DatabaseTest, SetDropPendingThrowsExceptionIfDatabaseIsAlreadyInADropPendingState) { writeConflictRetry(_opCtx.get(), "testSetDropPending", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); ASSERT_FALSE(db->isDropPending(_opCtx.get())); @@ -140,8 +140,8 @@ TEST_F(DatabaseTest, SetDropPendingThrowsExceptionIfDatabaseIsAlreadyInADropPend TEST_F(DatabaseTest, CreateCollectionThrowsExceptionWhenDatabaseIsInADropPendingState) { writeConflictRetry( _opCtx.get(), "testÇreateCollectionWhenDatabaseIsInADropPendingState", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); db->setDropPending(_opCtx.get(), true); @@ -172,8 +172,8 @@ void _testDropCollection(OperationContext* opCtx, if (createCollectionBeforeDrop) { writeConflictRetry(opCtx, "testDropCollection", nss.ns(), [=] { WriteUnitOfWork wuow(opCtx); - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); ASSERT_TRUE(db->createCollection(opCtx, nss, collOpts)); wuow.commit(); @@ -181,8 +181,8 @@ void _testDropCollection(OperationContext* opCtx, } writeConflictRetry(opCtx, "testDropCollection", nss.ns(), [=] { - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); WriteUnitOfWork wuow(opCtx); @@ -246,8 +246,8 @@ TEST_F(DatabaseTest, DropCollectionRejectsProvidedDropOpTimeIfWritesAreReplicate auto opCtx = _opCtx.get(); auto nss = _nss; - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); writeConflictRetry(opCtx, "testDropOpTimeWithReplicated", nss.ns(), [&] { ASSERT_TRUE(db); @@ -290,8 +290,8 @@ TEST_F( void _testDropCollectionThrowsExceptionIfThereAreIndexesInProgress(OperationContext* opCtx, const NamespaceString& nss) { writeConflictRetry(opCtx, "testDropCollectionWithIndexesInProgress", nss.ns(), [opCtx, nss] { - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); Collection* collection = nullptr; @@ -350,8 +350,8 @@ TEST_F(DatabaseTest, RenameCollectionPreservesUuidOfSourceCollectionAndUpdatesUu auto toNss = NamespaceString(fromNss.getSisterNS("bar")); ASSERT_NOT_EQUALS(fromNss, toNss); - AutoGetOrCreateDb autoDb(opCtx, fromNss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, fromNss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); auto fromUuid = UUID::gen(); @@ -393,8 +393,8 @@ TEST_F(DatabaseTest, RenameCollectionPreservesUuidOfSourceCollectionAndUpdatesUu TEST_F(DatabaseTest, MakeUniqueCollectionNamespaceReturnsFailedToParseIfModelDoesNotContainPercentSign) { writeConflictRetry(_opCtx.get(), "testMakeUniqueCollectionNamespace", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); ASSERT_EQUALS( ErrorCodes::FailedToParse, @@ -404,8 +404,8 @@ TEST_F(DatabaseTest, TEST_F(DatabaseTest, MakeUniqueCollectionNamespaceReplacesPercentSignsWithRandomCharacters) { writeConflictRetry(_opCtx.get(), "testMakeUniqueCollectionNamespace", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); auto model = "tmp%%%%"_sd; @@ -446,8 +446,8 @@ TEST_F( DatabaseTest, MakeUniqueCollectionNamespaceReturnsNamespaceExistsIfGeneratedNamesMatchExistingCollections) { writeConflictRetry(_opCtx.get(), "testMakeUniqueCollectionNamespace", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); auto model = "tmp%"_sd; @@ -528,8 +528,8 @@ TEST_F(DatabaseTest, CreateCollectionProhibitsReplicatedCollectionsWithoutIdInde "testÇreateCollectionProhibitsReplicatedCollectionsWithoutIdIndex", _nss.ns(), [this] { - AutoGetOrCreateDb autoDb(_opCtx.get(), _nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(_opCtx.get(), _nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); WriteUnitOfWork wuow(_opCtx.get()); diff --git a/src/mongo/db/catalog/drop_database_test.cpp b/src/mongo/db/catalog/drop_database_test.cpp index e5b68aa013e..a77d3a34e2b 100644 --- a/src/mongo/db/catalog/drop_database_test.cpp +++ b/src/mongo/db/catalog/drop_database_test.cpp @@ -180,8 +180,8 @@ void DropDatabaseTest::tearDown() { */ void _createCollection(OperationContext* opCtx, const NamespaceString& nss) { writeConflictRetry(opCtx, "testDropCollection", nss.ns(), [=] { - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db); WriteUnitOfWork wuow(opCtx); diff --git a/src/mongo/db/catalog/rename_collection_test.cpp b/src/mongo/db/catalog/rename_collection_test.cpp index b34d7eedf48..7acd0df9b1e 100644 --- a/src/mongo/db/catalog/rename_collection_test.cpp +++ b/src/mongo/db/catalog/rename_collection_test.cpp @@ -375,8 +375,8 @@ void _createCollection(OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions options = {}) { writeConflictRetry(opCtx, "_createCollection", nss.ns(), [=] { - AutoGetOrCreateDb autoDb(opCtx, nss.db(), MODE_X); - auto db = autoDb.getDb(); + AutoGetDb autoDb(opCtx, nss.db(), MODE_X); + auto db = autoDb.ensureDbExists(); ASSERT_TRUE(db) << "Cannot create collection " << nss << " because database " << nss.db() << " does not exist."; -- cgit v1.2.1