diff options
author | Yuhong Zhang <danielzhangyh@gmail.com> | 2021-11-18 15:26:25 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-11-19 04:32:34 +0000 |
commit | 88bd9ede7754014ab16141bee4208b7949711e9d (patch) | |
tree | 959099bb9d296ef4f1aa42c400118f5d84293b44 /src/mongo/dbtests | |
parent | 63b46f14867df2de8f823b9eb1b65bda3ac4b1c2 (diff) | |
download | mongo-88bd9ede7754014ab16141bee4208b7949711e9d.tar.gz |
SERVER-59831 Pass `dupsAllowed=true` to `WiredTigerIndexUnique::_insert` on secondaries
Diffstat (limited to 'src/mongo/dbtests')
-rw-r--r-- | src/mongo/dbtests/index_access_method_test.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/mongo/dbtests/index_access_method_test.cpp b/src/mongo/dbtests/index_access_method_test.cpp index ad4bcca07bd..409ae85770a 100644 --- a/src/mongo/dbtests/index_access_method_test.cpp +++ b/src/mongo/dbtests/index_access_method_test.cpp @@ -31,8 +31,10 @@ #include "mongo/bson/bsonobj.h" #include "mongo/bson/simple_bsonobj_comparator.h" +#include "mongo/db/client.h" #include "mongo/db/index/index_access_method.h" #include "mongo/db/json.h" +#include "mongo/dbtests/dbtests.h" #include "mongo/unittest/unittest.h" namespace mongo { @@ -223,6 +225,37 @@ TEST(IndexAccessMethodSetDifference, ShouldNotReportOverlapsFromNonDisjointSets) } } +TEST(IndexAccessMethodInsertKeys, DuplicatesCheckingOnSecondaryUniqueIndexes) { + ServiceContext::UniqueOperationContext opCtxRaii = cc().makeOperationContext(); + OperationContext* opCtx = opCtxRaii.get(); + NamespaceString nss("unittests.DuplicatesCheckingOnSecondaryUniqueIndexes"); + auto indexName = "a_1"; + auto indexSpec = BSON("name" << indexName << "key" << BSON("a" << 1) << "unique" << true << "v" + << static_cast<int>(IndexDescriptor::IndexVersion::kV2)); + ASSERT_OK(dbtests::createIndexFromSpec(opCtx, nss.ns(), indexSpec)); + + AutoGetCollection autoColl(opCtx, nss, LockMode::MODE_X); + const auto& coll = autoColl.getCollection(); + auto indexDescriptor = coll->getIndexCatalog()->findIndexByName(opCtx, indexName); + IndexAccessMethod* indexAccessMethod = + coll->getIndexCatalog()->getEntry(indexDescriptor)->accessMethod(); + + KeyString::HeapBuilder keyString1( + KeyString::Version::kLatestVersion, BSON("" << 1), Ordering::make(BSONObj()), RecordId(1)); + KeyString::HeapBuilder keyString2( + KeyString::Version::kLatestVersion, BSON("" << 1), Ordering::make(BSONObj()), RecordId(2)); + KeyStringSet keys{keyString1.release(), keyString2.release()}; + struct InsertDeleteOptions options; /* options.dupsAllowed = false */ + + // Checks duplicates and returns the error code when constraints are enforced. + auto status = indexAccessMethod->insertKeys(opCtx, coll, keys, {}, options, {}, nullptr); + ASSERT_EQ(status.code(), ErrorCodes::DuplicateKey); + + // Skips the check on duplicates when constraints are not enforced. + opCtx->setEnforceConstraints(false); + ASSERT_OK(indexAccessMethod->insertKeys(opCtx, coll, keys, {}, options, {}, nullptr)); +} + } // namespace } // namespace mongo |