summaryrefslogtreecommitdiff
path: root/jstests/core/bulk_legacy_enforce_gle.js
blob: 2e8e076e070bec1551d8af092e722d4898b91caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * Tests the resetError logic when the bulk api enforces the write concern for unordered
 * writes. The tests indirectly checks whether resetError was called by inspecting the
 * response of the getLastError command after executing the bulk ops.
 */

var coll = db.bulk_legacy_enforce_gle;

// 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);

var gle = db.runCommand({getLastError: 1});
assert(gle.ok, tojson(gle));
assert.eq(1, gle.n);

// 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);

// 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);

// 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));
// mongos sends the bulk as one while the shell sends the write individually
assert.gte(gle.n, 1);

// 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);

// 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);

// 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);

gle = db.runCommand({getLastError: 1, w: 0});
assert(gle.ok, tojson(gle));
assert.neq(null, gle.err);