summaryrefslogtreecommitdiff
path: root/jstests/replsets/bulk_api_wc.js
blob: f08e4df64b91e71f41e133e0b238ade5a84f8d4a (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
112
113
114
115
116
117
118
119
120
121
// Tests write-concern-related bulk api functionality
//
// This test asserts that a journaled write to a mongod running with --nojournal should be rejected,
// so cannot be run on the ephemeralForTest storage engine, as it accepts all journaled writes.
// @tags: [SERVER-21420]

jsTest.log("Starting bulk api write concern tests...");

// Start a 2-node replica set with no journal
// Allows testing immediate write concern failures and wc application failures
var rst = new ReplSetTest({nodes: 2});
rst.startSet({nojournal: ""});
rst.initiate();
var mongod = rst.getPrimary();
var coll = mongod.getCollection("test.bulk_api_wc");

var executeTests = function() {

    // Create a unique index, legacy writes validate too early to use invalid documents for write
    // error testing
    coll.ensureIndex({a: 1}, {unique: true});

    //
    // Ordered
    //

    //
    // Fail due to nojournal
    coll.remove({});
    var bulk = coll.initializeOrderedBulkOp();
    bulk.insert({a: 1});
    bulk.insert({a: 2});
    assert.throws(function() {
        bulk.execute({j: true});
    });

    //
    // Fail with write error, no write concern error even though it would fail on apply for ordered
    coll.remove({});
    var bulk = coll.initializeOrderedBulkOp();
    bulk.insert({a: 1});
    bulk.insert({a: 2});
    bulk.insert({a: 2});
    var result = assert.throws(function() {
        bulk.execute({w: 'invalid'});
    });
    assert.eq(result.nInserted, 2);
    assert.eq(result.getWriteErrors()[0].index, 2);
    assert(!result.getWriteConcernError());
    assert.eq(coll.count(), 2);

    //
    // Unordered
    //

    //
    // Fail with write error, write concern error reported when unordered
    coll.remove({});
    var bulk = coll.initializeUnorderedBulkOp();
    bulk.insert({a: 1});
    bulk.insert({a: 2});
    bulk.insert({a: 2});
    var result = assert.throws(function() {
        bulk.execute({w: 'invalid'});
    });
    assert.eq(result.nInserted, 2);
    assert.eq(result.getWriteErrors()[0].index, 2);
    assert(result.getWriteConcernError());
    assert.eq(coll.count(), 2);

    //
    // Fail with write error, write concern timeout reported when unordered
    // Note that wtimeout:true can only be reported when the batch is all the same, so there's not
    // multiple wc errors
    coll.remove({});
    var bulk = coll.initializeUnorderedBulkOp();
    bulk.insert({a: 1});
    bulk.insert({a: 2});
    bulk.insert({a: 2});
    var result = assert.throws(function() {
        bulk.execute({w: 3, wtimeout: 1});
    });
    assert.eq(result.nInserted, 2);
    assert.eq(result.getWriteErrors()[0].index, 2);
    assert.eq(100, result.getWriteConcernError().code);
    assert.eq(coll.count(), 2);

    //
    // Fail with write error and upserted, write concern error reported when unordered
    coll.remove({});
    var bulk = coll.initializeUnorderedBulkOp();
    bulk.insert({a: 1});
    bulk.insert({a: 2});
    bulk.find({a: 3}).upsert().updateOne({a: 3});
    bulk.insert({a: 3});
    var result = assert.throws(function() {
        bulk.execute({w: 'invalid'});
    });
    assert.eq(result.nInserted, 2);
    assert.eq(result.nUpserted, 1);
    assert.eq(result.getUpsertedIds()[0].index, 2);
    assert.eq(result.getWriteErrors()[0].index, 3);
    assert(result.getWriteConcernError());
    assert.eq(coll.count(), 3);
};

// Use write commands
coll.getMongo().useWriteCommands = function() {
    return true;
};
executeTests();

// FAILING currently due to incorrect batch api reading of GLE
// Use legacy opcodes
coll.getMongo().useWriteCommands = function() {
    return false;
};
executeTests();

jsTest.log("DONE bulk api wc tests");
rst.stopSet();