summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/commands_that_write_accept_wc_standalone.js
blob: 25083f1484c595e27b843865321e7b5206647436 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * This file tests that commands that should accept a writeConcern on a standalone can accept one.
 * This does not test that writes with j: true are actually made durable or that if j: true fails
 * that there is a writeConcern error.
 * @tags: [requires_journaling]
 */

(function() {
"use strict";
var collName = 'leaves';
var coll = db[collName];

var commands = [];

commands.push({
    req: {insert: collName, documents: [{type: 'maple'}]},
    setupFunc: function() {},
    confirmFunc: function() {
        assert.eq(coll.count({type: 'maple'}), 1);
    }
});

commands.push({
    req: {createIndexes: collName, indexes: [{key: {'type': 1}, name: 'type_index'}]},
    setupFunc: function() {
        coll.insert({type: 'oak'});
        assert.eq(coll.getIndexes().length, 1);
    },
    confirmFunc: function() {
        assert.eq(coll.getIndexes().length, 2);
    }
});

commands.push({
    req: {
        update: collName,
        updates: [{
            q: {type: 'oak'},
            u: [{$set: {type: 'ginkgo'}}],
        }],
        writeConcern: {w: 'majority'}
    },
    setupFunc: function() {
        coll.insert({type: 'oak'});
        assert.eq(coll.count({type: 'ginkgo'}), 0);
        assert.eq(coll.count({type: 'oak'}), 1);
    },
    confirmFunc: function() {
        assert.eq(coll.count({type: 'ginkgo'}), 1);
        assert.eq(coll.count({type: 'oak'}), 0);
    }
});

commands.push({
    req: {
        findAndModify: collName,
        query: {type: 'oak'},
        update: {$set: {type: 'ginkgo'}},
        writeConcern: {w: 'majority'}
    },
    setupFunc: function() {
        coll.insert({type: 'oak'});
        assert.eq(coll.count({type: 'ginkgo'}), 0);
        assert.eq(coll.count({type: 'oak'}), 1);
    },
    confirmFunc: function() {
        assert.eq(coll.count({type: 'ginkgo'}), 1);
        assert.eq(coll.count({type: 'oak'}), 0);
    }
});

commands.push({
    req: {
        findAndModify: collName,
        query: {type: 'oak'},
        update: [{$set: {type: 'ginkgo'}}],
        writeConcern: {w: 'majority'}
    },
    setupFunc: function() {
        coll.insert({type: 'oak'});
        assert.eq(coll.count({type: 'ginkgo'}), 0);
        assert.eq(coll.count({type: 'oak'}), 1);
    },
    confirmFunc: function() {
        assert.eq(coll.count({type: 'ginkgo'}), 1);
        assert.eq(coll.count({type: 'oak'}), 0);
    }
});

commands.push({
    req: {applyOps: [{op: "i", ns: coll.getFullName(), o: {_id: 1, type: "willow"}}]},
    setupFunc: function() {
        coll.insert({_id: 1, type: 'oak'});
        assert.eq(coll.count({type: 'willow'}), 0);
    },
    confirmFunc: function() {
        assert.eq(coll.count({type: 'willow'}), 1);
    }
});

commands.push({
    req: {aggregate: collName, pipeline: [{$sort: {type: 1}}, {$out: "foo"}], cursor: {}},
    setupFunc: function() {
        coll.insert({_id: 1, type: 'oak'});
        coll.insert({_id: 2, type: 'maple'});
    },
    confirmFunc: function() {
        assert.eq(db.foo.count({type: 'oak'}), 1);
        assert.eq(db.foo.count({type: 'maple'}), 1);
        db.foo.drop();
    }
});

commands.push({
    req: {
        mapReduce: collName,
        map: function() {
            this.tags.forEach(function(z) {
                emit(z, 1);
            });
        },
        reduce: function(key, values) {
            return {count: values.length};
        },
        out: "foo"
    },
    setupFunc: function() {
        coll.insert({x: 1, tags: ["a", "b"]});
        coll.insert({x: 2, tags: ["b", "c"]});
        coll.insert({x: 3, tags: ["c", "a"]});
        coll.insert({x: 4, tags: ["b", "c"]});
    },
    confirmFunc: function() {
        assert.eq(db.foo.findOne({_id: 'a'}).value.count, 2);
        assert.eq(db.foo.findOne({_id: 'b'}).value.count, 3);
        assert.eq(db.foo.findOne({_id: 'c'}).value.count, 3);
        db.foo.drop();
    }
});

function testValidWriteConcern(cmd) {
    cmd.req.writeConcern = {w: 1, j: true};
    jsTest.log("Testing " + tojson(cmd.req));

    coll.drop();
    cmd.setupFunc();
    var res = db.runCommand(cmd.req);
    assert.commandWorked(res);
    assert(!res.writeConcernError, 'command had writeConcernError: ' + tojson(res));
    cmd.confirmFunc();
}

function testInvalidWriteConcern(wc, cmd) {
    cmd.req.writeConcern = wc;
    jsTest.log("Testing " + tojson(cmd.req));

    coll.drop();
    cmd.setupFunc();
    var res = coll.runCommand(cmd.req);
    // These commands should fail because standalone writeConcerns are found to be invalid at
    // the validation stage when the writeConcern is parsed, before the command is run.
    assert.commandFailed(res);
}

var invalidWriteConcerns = [{w: 'invalid'}, {w: 2}, {j: 'invalid'}];

commands.forEach(function(cmd) {
    testValidWriteConcern(cmd);
    invalidWriteConcerns.forEach(function(wc) {
        testInvalidWriteConcern(wc, cmd);
    });
});
})();