summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorGeert Bosch <geert@mongodb.com>2014-12-16 10:34:18 -0500
committerGeert Bosch <geert@mongodb.com>2014-12-16 11:22:00 -0500
commit35d89db03ecb922e3b43a1b1f8f0aaab3794d87f (patch)
treee8b692546efa73ae7562429085911617feea54e2 /jstests
parentfd08ed58f4d3ffe284e1d28d9ebe44ab10ffbe6f (diff)
downloadmongo-35d89db03ecb922e3b43a1b1f8f0aaab3794d87f.tar.gz
SERVER-16262: Add test case for Map/Reduce WriteConflict
Diffstat (limited to 'jstests')
-rw-r--r--jstests/noPassthroughWithMongod/mr_writeconflict.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/mr_writeconflict.js b/jstests/noPassthroughWithMongod/mr_writeconflict.js
new file mode 100644
index 00000000000..e3963e80c98
--- /dev/null
+++ b/jstests/noPassthroughWithMongod/mr_writeconflict.js
@@ -0,0 +1,71 @@
+// SERVER-16262: Write-conflict during map-reduce operations
+(function() {
+ "use strict";
+
+ load('jstests/libs/parallelTester.js');
+
+ var makeDoc = function(keyLimit, valueLimit) {
+ return {
+ _id: ObjectId(),
+ key: Random.randInt(keyLimit),
+ value: Random.randInt(valueLimit)
+ };
+ };
+
+ var main = function() {
+
+ function mapper() {
+ var obj = {};
+ obj[this.value] = 1;
+ emit(this.key, obj);
+ }
+
+ function reducer(key, values) {
+ var res = {};
+
+ values.forEach(function(obj) {
+ Object.keys(obj).forEach(function(value) {
+ if (!res.hasOwnProperty(value)) {
+ res[value] = 0;
+ }
+ res[value] += obj[value];
+ });
+ });
+
+ return res;
+ }
+
+ for (var i = 0; i < 10; i++) {
+ // Have all threads combine their results into the same collection
+ var res = db.source.mapReduce(mapper, reducer, { out: { reduce: 'dest' } });
+ assert.commandWorked(res);
+ }
+ };
+
+ var numDocs = 200;
+ var bulk = db.source.initializeUnorderedBulkOp();
+ var i;
+ for (i = 0; i < numDocs; ++i) {
+ var doc = makeDoc(numDocs / 100, numDocs / 10);
+ bulk.insert(doc);
+ }
+
+ var res = bulk.execute();
+ assert.writeOK(res);
+ assert.eq(numDocs, res.nInserted);
+
+ db.dest.drop();
+ assert.commandWorked(db.createCollection('dest'));
+
+ var numThreads = 6;
+ var t = [];
+ for (i = 0; i < numThreads - 1; ++i) {
+ t[i] = new ScopedThread(main);
+ t[i].start();
+ }
+
+ main();
+ for (i = 0; i < numThreads - 1; ++i) {
+ t[i].join();
+ }
+}());