Skip to content

Commit 83b3e0e

Browse files
committed
Merge branch '2.0' into APM-merge-prototype
2 parents 727a75f + accc949 commit 83b3e0e

File tree

4 files changed

+156
-10
lines changed

4 files changed

+156
-10
lines changed

lib/collection.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,19 +452,14 @@ Collection.prototype.insertMany = function(docs, options, callback) {
452452

453453
// Execute using callback
454454
if(typeof callback == 'function') return bulkWrite(self, operations, options, function(err, r) {
455-
if(err) return callback(err);
456-
if(r.hasWriteErrors()) return callback(r);
455+
if(err) return callback(err, r);
457456
callback(null, mapInserManyResults(docs, r));
458457
});
459458

460459
// Return a Promise
461460
return new this.s.promiseLibrary(function(resolve, reject) {
462461
bulkWrite(self, operations, options, function(err, r) {
463462
if(err) return reject(err);
464-
if(r.hasWriteErrors()) {
465-
return reject(r);
466-
}
467-
468463
resolve(mapInserManyResults(docs, r));
469464
});
470465
});
@@ -554,7 +549,14 @@ var bulkWrite = function(self, operations, options, callback) {
554549

555550
// Execute the bulk
556551
bulk.execute(writeCon, function(err, r) {
557-
if(err) return callback(err);
552+
// We have connection level error
553+
if(!r && err) return callback(err, null);
554+
// We have single error
555+
if(r && r.hasWriteErrors() && r.getWriteErrorCount() == 1) {
556+
return callback(toError(r.getWriteErrorAt(0)), r);
557+
}
558+
559+
// if(err) return callback(err);
558560
r.insertedCount = r.nInserted;
559561
r.matchedCount = r.nMatched;
560562
r.modifiedCount = r.nModified || 0;
@@ -580,6 +582,22 @@ var bulkWrite = function(self, operations, options, callback) {
580582
r.upsertedIds[upserted[i].index] = upserted[i]._id;
581583
}
582584

585+
// Check if we have write errors
586+
if(r.hasWriteErrors()) {
587+
// Get all the errors
588+
var errors = r.getWriteErrors();
589+
// Return the MongoError object
590+
return callback(toError({
591+
message: 'write operation failed', code: errors[0].code, writeErrors: errors
592+
}), r);
593+
}
594+
595+
// Check if we have a writeConcern error
596+
if(r.getWriteConcernError()) {
597+
// Return the MongoError object
598+
return callback(toError(r.getWriteConcernError()), r);
599+
}
600+
583601
// Return the results
584602
callback(null, r);
585603
});

test/functional/insert_tests.js

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ exports.shouldThrowErrorIfSerializingFunctionUnOrdered = {
573573
test.equal(null, err);
574574

575575
collection.findOne({_id:result.ops[0]._id}, function(err, object) {
576-
console.dir(object)
577576
test.equal(func.toString(), object.z.code);
578577
test.equal(1, object.i);
579578
db.close();
@@ -1962,3 +1961,131 @@ exports['should correctly insert > 1000 docs using insert and insertMany'] = {
19621961
});
19631962
}
19641963
}
1964+
1965+
exports['should return error on unordered insertMany with multiple unique key constraints'] = {
1966+
// Add a tag that our runner can trigger on
1967+
// in this case we are setting that node needs to be higher than 0.10.X to run
1968+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
1969+
1970+
// The actual test we wish to run
1971+
test: function(configuration, test) {
1972+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {native_parser:false})
1973+
db.open(function(err, db) {
1974+
// Get collection
1975+
var col = db.collection('insertManyMultipleWriteErrors');
1976+
col.drop(function(err, r) {
1977+
1978+
// Create unique index
1979+
col.createIndex({a:1}, {unique:true}, function(err, r) {
1980+
test.equal(null, err);
1981+
1982+
col.insertMany([{a:1}, {a:2}, {a:1}, {a:3}, {a:1}], {ordered:false}, function(err, r) {
1983+
test.ok(err != null);
1984+
test.ok(err.writeErrors.length == 2);
1985+
1986+
db.close();
1987+
test.done();
1988+
});
1989+
});
1990+
});
1991+
});
1992+
}
1993+
}
1994+
1995+
exports['should return error on unordered insert with multiple unique key constraints'] = {
1996+
// Add a tag that our runner can trigger on
1997+
// in this case we are setting that node needs to be higher than 0.10.X to run
1998+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
1999+
2000+
// The actual test we wish to run
2001+
test: function(configuration, test) {
2002+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {native_parser:false})
2003+
db.open(function(err, db) {
2004+
// Get collection
2005+
var col = db.collection('insertManyMultipleWriteErrors1');
2006+
col.drop(function(err, r) {
2007+
2008+
// Create unique index
2009+
col.createIndex({a:1}, {unique:true}, function(err, r) {
2010+
test.equal(null, err);
2011+
2012+
col.insert([{a:1}, {a:2}, {a:1}, {a:3}, {a:1}], {ordered:false}, function(err, r) {
2013+
test.ok(err != null);
2014+
test.ok(err.writeErrors.length == 2);
2015+
2016+
db.close();
2017+
test.done();
2018+
});
2019+
});
2020+
});
2021+
});
2022+
}
2023+
}
2024+
2025+
exports['should return error on ordered insertMany with multiple unique key constraints'] = {
2026+
// Add a tag that our runner can trigger on
2027+
// in this case we are setting that node needs to be higher than 0.10.X to run
2028+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
2029+
2030+
// The actual test we wish to run
2031+
test: function(configuration, test) {
2032+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {native_parser:false})
2033+
db.open(function(err, db) {
2034+
// Get collection
2035+
var col = db.collection('insertManyMultipleWriteErrors2');
2036+
col.drop(function(err, r) {
2037+
2038+
// Create unique index
2039+
col.createIndex({a:1}, {unique:true}, function(err, r) {
2040+
test.equal(null, err);
2041+
2042+
col.insertMany([{a:1}, {a:2}, {a:1}, {a:3}, {a:1}], {ordered:true}, function(err, r) {
2043+
test.ok(err != null);
2044+
2045+
db.close();
2046+
test.done();
2047+
});
2048+
});
2049+
});
2050+
});
2051+
}
2052+
}
2053+
2054+
exports['should return error on ordered insert with multiple unique key constraints'] = {
2055+
// Add a tag that our runner can trigger on
2056+
// in this case we are setting that node needs to be higher than 0.10.X to run
2057+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
2058+
2059+
// The actual test we wish to run
2060+
test: function(configuration, test) {
2061+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {native_parser:false})
2062+
db.open(function(err, db) {
2063+
// Get collection
2064+
var col = db.collection('insertManyMultipleWriteErrors3');
2065+
col.drop(function(err, r) {
2066+
2067+
// Create unique index
2068+
col.createIndex({a:1}, {unique:true}, function(err, r) {
2069+
test.equal(null, err);
2070+
2071+
col.insert([{a:1}, {a:2}, {a:1}, {a:3}, {a:1}], {ordered:true}, function(err, r) {
2072+
test.ok(err != null);
2073+
2074+
db.close();
2075+
test.done();
2076+
});
2077+
});
2078+
});
2079+
});
2080+
}
2081+
}
2082+
2083+
2084+
2085+
2086+
2087+
2088+
2089+
2090+
2091+

test/functional/logger_tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ exports['Should correctly Enable logging'] = {
1313

1414
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
1515
db.open(function(err, db) {
16+
test.equal(null, err);
1617
var collection = db.collection('enable_logging_1');
1718

1819
// Logging setup

test/functional/reconnect_tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ exports['Should correctly stop reconnection attempts after limit reached'] = {
1616

1717
db.open(function(err, db) {
1818
// Now let's stop the server
19-
configuration.manager.stop(function() {
19+
configuration.manager.stop({signal:9}, function() {
2020

2121
db.collection('waiting_for_reconnect').insert({a:1}, function(err, r) {
2222
test.ok(err != null);
2323
db.close();
2424

25-
configuration.manager.start(function() {
25+
configuration.manager.start({purge:true, signal:9}, function() {
2626
test.done();
2727
});
2828
});

0 commit comments

Comments
 (0)