summaryrefslogtreecommitdiff
path: root/jstests/core/bulk_legacy_enforce_gle.js
diff options
context:
space:
mode:
authorKyle Suarez <kyle.suarez@mongodb.com>2017-12-14 09:53:42 -0500
committerKyle Suarez <kyle.suarez@mongodb.com>2017-12-14 09:56:32 -0500
commitfe40a36217a2b4e4064165340d44cc1442d84e13 (patch)
tree0f7e589e3e824c5fd58aa82a967c95b2a1714a9f /jstests/core/bulk_legacy_enforce_gle.js
parent9611f1368349db2fcbe7a7090d7bb7afe0f789af (diff)
downloadmongo-fe40a36217a2b4e4064165340d44cc1442d84e13.tar.gz
SERVER-32204 refactor bulk_legacy_enforce_gle.js
Diffstat (limited to 'jstests/core/bulk_legacy_enforce_gle.js')
-rw-r--r--jstests/core/bulk_legacy_enforce_gle.js196
1 files changed, 104 insertions, 92 deletions
diff --git a/jstests/core/bulk_legacy_enforce_gle.js b/jstests/core/bulk_legacy_enforce_gle.js
index 6e377f31107..473f6a4684d 100644
--- a/jstests/core/bulk_legacy_enforce_gle.js
+++ b/jstests/core/bulk_legacy_enforce_gle.js
@@ -3,109 +3,121 @@
* writes. The tests indirectly checks whether resetError was called by inspecting the
* response of the getLastError command after executing the bulk ops.
*/
+(function() {
+ "use strict";
+ const coll = db.bulk_legacy_enforce_gle;
-var coll = db.bulk_legacy_enforce_gle;
+ /**
+ * Inserts 'doc' into the collection, asserting that the write succeeds. This runs a
+ * getLastError if the insert does not return a response.
+ */
+ function insertDocument(doc) {
+ let res = coll.insert(doc);
+ if (res) {
+ assert.writeOK(res);
+ } else {
+ assert.gleOK(db.runCommand({getLastError: 1}));
+ }
+ }
-// batch of size 1 no error case.
-coll.drop();
-var bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-assert(bulk.execute() instanceof BulkWriteResult);
+ coll.drop();
+ let bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ assert.writeOK(bulk.execute());
+ let gle = assert.gleOK(db.runCommand({getLastError: 1}));
+ assert.eq(1, gle.n, tojson(gle));
-var gle = db.runCommand({getLastError: 1});
-assert(gle.ok, tojson(gle));
-assert.eq(1, gle.n, tojson(gle));
+ // Batch of size 1 should not call resetError even when it errors out.
+ assert(coll.drop());
+ insertDocument({_id: 1});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ assert.throws(function() {
+ bulk.execute();
+ });
-// batch of size 1 should not call resetError even when it errors out.
-coll.drop();
-coll.insert({_id: 1});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-assert.throws(function() {
- bulk.execute();
-});
+ gle = db.runCommand({getLastError: 1});
+ assert(gle.ok, tojson(gle));
+ assert.neq(null, gle.err, tojson(gle));
-gle = db.runCommand({getLastError: 1});
-assert(gle.ok, tojson(gle));
-assert.neq(null, gle.err, tojson(gle));
+ // Batch with all error except last should not call resetError.
+ assert(coll.drop());
+ insertDocument({_id: 1});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 0});
+ let res = assert.throws(function() {
+ bulk.execute();
+ });
+ assert.eq(2, res.getWriteErrors().length);
-// batch with all error except last should not call resetError.
-coll.drop();
-coll.insert({_id: 1});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 0});
-var res = assert.throws(function() {
- bulk.execute();
-});
-assert.eq(2, res.getWriteErrors().length);
+ gle = db.runCommand({getLastError: 1});
+ assert(gle.ok, tojson(gle));
+ assert.eq(1, gle.n, tojson(gle));
-gle = db.runCommand({getLastError: 1});
-assert(gle.ok, tojson(gle));
-assert.eq(1, gle.n, tojson(gle));
+ // Batch with error at middle should not call resetError.
+ assert(coll.drop());
+ insertDocument({_id: 1});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 0});
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 2});
+ res = assert.throws(function() {
+ bulk.execute();
+ });
+ assert.eq(1, res.getWriteErrors().length);
-// batch with error at middle should not call resetError.
-coll.drop();
-coll.insert({_id: 1});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 0});
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 2});
-var res = assert.throws(function() {
- bulk.execute();
-});
-assert.eq(1, res.getWriteErrors().length);
+ gle = db.runCommand({getLastError: 1});
+ assert(gle.ok, tojson(gle));
+ // For legacy writes, mongos sends the bulk as one while the shell sends the write individually.
+ assert.gte(gle.n, 1, tojson(gle));
-gle = db.runCommand({getLastError: 1});
-assert(gle.ok, tojson(gle));
-// mongos sends the bulk as one while the shell sends the write individually
-assert.gte(gle.n, 1, tojson(gle));
+ // Batch with error at last should call resetError.
+ assert(coll.drop());
+ insertDocument({_id: 2});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 0});
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 2});
+ res = assert.throws(function() {
+ bulk.execute();
+ });
+ assert.eq(1, res.getWriteErrors().length);
-// batch with error at last should call resetError.
-coll.drop();
-coll.insert({_id: 2});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 0});
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 2});
-res = assert.throws(function() {
- bulk.execute();
-});
-assert.eq(1, res.getWriteErrors().length);
+ gle = db.runCommand({getLastError: 1});
+ assert(gle.ok, tojson(gle));
+ assert.eq(0, gle.n, tojson(gle));
-gle = db.runCommand({getLastError: 1});
-assert(gle.ok, tojson(gle));
-assert.eq(0, gle.n, tojson(gle));
+ // Batch with error at last should not call resetError if { w: 1 }.
+ assert(coll.drop());
+ insertDocument({_id: 2});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 0});
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 2});
+ res = assert.throws(function() {
+ bulk.execute();
+ });
+ assert.eq(1, res.getWriteErrors().length);
-// batch with error at last should not call resetError if { w: 1 }
-coll.drop();
-coll.insert({_id: 2});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 0});
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 2});
-res = assert.throws(function() {
- bulk.execute();
-});
-assert.eq(1, res.getWriteErrors().length);
+ gle = db.runCommand({getLastError: 1, w: 1});
+ assert(gle.ok, tojson(gle));
+ assert.neq(null, gle.err, tojson(gle));
-gle = db.runCommand({getLastError: 1, w: 1});
-assert(gle.ok, tojson(gle));
-assert.neq(null, gle.err, tojson(gle));
+ // Batch with error at last should not call resetError if { w: 0 }.
+ assert(coll.drop());
+ insertDocument({_id: 2});
+ bulk = coll.initializeUnorderedBulkOp();
+ bulk.find({none: 1}).upsert().updateOne({_id: 0});
+ bulk.find({none: 1}).upsert().updateOne({_id: 1});
+ bulk.find({none: 1}).upsert().updateOne({_id: 2});
+ res = assert.throws(function() {
+ bulk.execute();
+ });
+ assert.eq(1, res.getWriteErrors().length, res);
-// batch with error at last should not call resetError if { w: 0 }
-coll.drop();
-coll.insert({_id: 2});
-bulk = coll.initializeUnorderedBulkOp();
-bulk.find({none: 1}).upsert().updateOne({_id: 0});
-bulk.find({none: 1}).upsert().updateOne({_id: 1});
-bulk.find({none: 1}).upsert().updateOne({_id: 2});
-res = assert.throws(function() {
- bulk.execute();
-});
-assert.eq(1, res.getWriteErrors().length, res);
-
-gle = db.runCommand({getLastError: 1, w: 0});
-assert(gle.ok, tojson(gle));
-assert.neq(null, gle.err, tojson(gle));
+ gle = db.runCommand({getLastError: 1, w: 0});
+ assert(gle.ok, tojson(gle));
+ assert.neq(null, gle.err, tojson(gle));
+}());