summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod
diff options
context:
space:
mode:
authorYuhong Zhang <yuhong.zhang@mongodb.com>2022-04-01 13:43:55 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-04-01 15:03:43 +0000
commit5985d757ddc2645fb1b5df88f78abf6b9a833452 (patch)
treec4bc7d0add03b51648236033201e513d6d67efd8 /jstests/noPassthroughWithMongod
parent211be39f5e722bc7ccb9660fa622e33d44d0b30a (diff)
downloadmongo-5985d757ddc2645fb1b5df88f78abf6b9a833452.tar.gz
SERVER-65024 Fix the uniqueness constraint handling for foreground index builds
Diffstat (limited to 'jstests/noPassthroughWithMongod')
-rw-r--r--jstests/noPassthroughWithMongod/reindex_duplicate_keys.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/reindex_duplicate_keys.js b/jstests/noPassthroughWithMongod/reindex_duplicate_keys.js
new file mode 100644
index 00000000000..aa4a363a72a
--- /dev/null
+++ b/jstests/noPassthroughWithMongod/reindex_duplicate_keys.js
@@ -0,0 +1,46 @@
+/**
+ * Tests that reIndex command fails with duplicate key error when there are duplicates in the
+ * collection.
+ */
+
+(function() {
+"use strict";
+
+const collNamePrefix = "reindex_duplicate_keys_";
+let count = 0;
+
+// Bypasses DuplicateKey insertion error for testing via failpoint.
+let addDuplicateDocumentsToCol = function(db, coll, doc) {
+ jsTestLog("Inserts documents without index entries.");
+ assert.commandWorked(
+ db.adminCommand({configureFailPoint: "skipIndexNewRecords", mode: "alwaysOn"}));
+
+ assert.commandWorked(coll.insert(doc));
+ assert.commandWorked(coll.insert(doc));
+
+ assert.commandWorked(db.adminCommand({configureFailPoint: "skipIndexNewRecords", mode: "off"}));
+};
+
+let runTest = function(doc) {
+ const collName = collNamePrefix + count++;
+ const coll = db.getCollection(collName);
+ coll.drop();
+
+ // Makes sure to create the _id index.
+ assert.commandWorked(db.createCollection(collName));
+ if (doc) {
+ assert.commandWorked(coll.createIndex(doc, {unique: true}));
+ } else {
+ doc = {_id: 1};
+ }
+
+ // Inserts two violating documents without indexing them.
+ addDuplicateDocumentsToCol(db, coll, doc);
+
+ // Checks reIndex command fails with duplicate key error.
+ assert.commandFailedWithCode(coll.reIndex(), ErrorCodes.DuplicateKey);
+};
+
+runTest();
+runTest({a: 1});
+})(); \ No newline at end of file