summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMihai Andrei <mihai.andrei@10gen.com>2021-11-19 13:53:51 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-12-03 15:58:56 +0000
commitd77b27f4baa8139ef8baa48d1588342e95cae389 (patch)
tree1bd0b2ff3c32b67eb31e57fc18350cb0bacf3cdc
parent1303aa0fb325e2d6e2d1e88e1508cd207918d9b7 (diff)
downloadmongo-d77b27f4baa8139ef8baa48d1588342e95cae389.tar.gz
SERVER-60788 Update 'merge_causes_infinite_loop.js' to demonstrate the halloween problem
-rw-r--r--jstests/noPassthrough/merge_causes_infinite_loop.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/jstests/noPassthrough/merge_causes_infinite_loop.js b/jstests/noPassthrough/merge_causes_infinite_loop.js
index 2e626180414..70cbd85c1a7 100644
--- a/jstests/noPassthrough/merge_causes_infinite_loop.js
+++ b/jstests/noPassthrough/merge_causes_infinite_loop.js
@@ -2,7 +2,9 @@
* Test that exposes the Halloween problem.
*
* The Halloween problem describes the potential for a document to be visited more than once
- * following an update operation that changes its physical location.
+ * following an update operation that changes its physical location. The purpose of this test is
+ * to show that this behavior can be encountered when running a $merge aggregation which writes
+ * to the collection being read from.
*/
(function() {
"use strict";
@@ -71,18 +73,32 @@ for (const doc of diffCollResult) {
assert.eq(doc["a"], expectedVal, doc);
}
-// Targeting the same collection that is being aggregated over will still result in each
-// document's value of 'a' being updated exactly once.
+// Targeting the same collection that is being aggregated over will result in some documents' value
+// of 'a' being updated multiple times.
assert.commandWorked(
db.runCommand({aggregate: coll.getName(), pipeline: sameCollPipeline, cursor: {}}));
-const sameCollResult = out.find({}, {largeArray: 0}).toArray();
+const sameCollResult = coll.find({}, {largeArray: 0}).toArray();
+// At least one document in our collection should have been updated multiple times.
+let foundDocumentUpdatedMultipleTimes = false;
for (const doc of sameCollResult) {
assert(doc.hasOwnProperty("a"), doc);
const expectedVal = doc["_id"] * 2 * largeNum;
- assert.eq(doc["a"], expectedVal, doc);
+ const actualVal = doc["a"];
+
+ // If we find a mismatch, it must be the case that 'actualVal' is at least twice as large as
+ // 'expectedVal'. This means that the $multiply expression was applied multiple times to the
+ // same document.
+ if (actualVal !== expectedVal && actualVal >= 2 * expectedVal) {
+ foundDocumentUpdatedMultipleTimes = true;
+ break;
+ }
}
+assert(foundDocumentUpdatedMultipleTimes,
+ "All documents were updated exactly once, which is unexpected. Contents of the collection" +
+ " being aggregated over and merged into: " + tojson(sameCollResult));
+
MongoRunner.stopMongod(conn);
}());