summaryrefslogtreecommitdiff
path: root/jstests/replsets/batch_write_command_wc.js
blob: 195e649fab8edf23f9ca6c4c23930e817877dcc6 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Tests write-concern-related batch write protocol 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]

(function() {

    // Skip this test if running with the "wiredTiger" storage engine, since it requires
    // using 'nojournal' in a replica set, which is not supported when using WT.
    if (!jsTest.options().storageEngine || jsTest.options().storageEngine === "wiredTiger") {
        // WT is currently the default engine so it is used when 'storageEngine' is not set.
        jsTest.log("Skipping test because it is not applicable for the wiredTiger storage engine");
        return;
    }

    var request;
    var result;

    // NOTE: ALL TESTS BELOW SHOULD BE SELF-CONTAINED, FOR EASIER DEBUGGING

    jsTest.log("Starting no journal/repl set tests...");

    // Start a single-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.batch_write_command_wc");

    //
    // Basic insert, default WC
    coll.remove({});
    printjson(request = {insert: coll.getName(), documents: [{a: 1}]});
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(1, coll.find().itcount());

    //
    // Basic insert, majority WC
    coll.remove({});
    printjson(
        request = {insert: coll.getName(), documents: [{a: 1}], writeConcern: {w: 'majority'}});
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(1, coll.find().itcount());

    //
    // Basic insert,  w:2 WC
    coll.remove({});
    printjson(request = {insert: coll.getName(), documents: [{a: 1}], writeConcern: {w: 2}});
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(1, coll.find().itcount());

    //
    // Basic insert, immediate nojournal error
    coll.remove({});
    printjson(request = {insert: coll.getName(), documents: [{a: 1}], writeConcern: {j: true}});
    printjson(result = coll.runCommand(request));
    assert(!result.ok);
    assert.eq(0, coll.find().itcount());

    //
    // Basic insert, timeout wc error
    coll.remove({});
    printjson(
        request = {insert: coll.getName(), documents: [{a: 1}], writeConcern: {w: 3, wtimeout: 1}});
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert(result.writeConcernError);
    assert.eq(100, result.writeConcernError.code);
    assert.eq(1, coll.find().itcount());

    //
    // Basic insert, wmode wc error
    coll.remove({});
    printjson(
        request = {insert: coll.getName(), documents: [{a: 1}], writeConcern: {w: 'invalid'}});
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert(result.writeConcernError);
    assert.eq(1, coll.find().itcount());

    //
    // Two ordered inserts, write error and wc error both reported
    coll.remove({});
    printjson(request = {
        insert: coll.getName(),
        documents: [{a: 1}, {$invalid: 'doc'}],
        writeConcern: {w: 'invalid'}
    });
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(result.writeErrors.length, 1);
    assert.eq(result.writeErrors[0].index, 1);
    assert(result.writeConcernError);
    assert.eq(1, coll.find().itcount());

    //
    // Two unordered inserts, write error and wc error reported
    coll.remove({});
    printjson(request = {
        insert: coll.getName(),
        documents: [{a: 1}, {$invalid: 'doc'}],
        writeConcern: {w: 'invalid'},
        ordered: false
    });
    printjson(result = coll.runCommand(request));
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(result.writeErrors.length, 1);
    assert.eq(result.writeErrors[0].index, 1);
    assert(result.writeConcernError);
    assert.eq(1, coll.find().itcount());

    //
    // Write error with empty writeConcern object.
    coll.remove({});
    request =
        {insert: coll.getName(), documents: [{_id: 1}, {_id: 1}], writeConcern: {}, ordered: false};
    result = coll.runCommand(request);
    assert(result.ok);
    assert.eq(1, result.n);
    assert.eq(result.writeErrors.length, 1);
    assert.eq(result.writeErrors[0].index, 1);
    assert.eq(null, result.writeConcernError);
    assert.eq(1, coll.find().itcount());

    //
    // Write error with unspecified w.
    coll.remove({});
    request = {
        insert: coll.getName(),
        documents: [{_id: 1}, {_id: 1}],
        writeConcern: {wtimeout: 1},
        ordered: false
    };
    result = assert.commandWorkedIgnoringWriteErrors(coll.runCommand(request));
    assert.eq(1, result.n);
    assert.eq(result.writeErrors.length, 1);
    assert.eq(result.writeErrors[0].index, 1);
    assert.eq(null, result.writeConcernError);
    assert.eq(1, coll.find().itcount());

    jsTest.log("DONE no journal/repl tests");
    rst.stopSet();

})();