summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server18198.js
blob: 6d8d827e41632d59d89541b1dc628615a53dbb16 (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
// SERVER-18198 check read pref is only applied when there is no $out stage
// in aggregate shell helper
(function() {
    "use strict";
    var t = db.server18198;
    t.drop();

    var mongo = db.getMongo();

    try {
        var commandsRan = [];
        // hook in our patched mongo
        var mockMongo = {
            getSlaveOk: function() { return true; },
            runCommand: function(db, cmd, opts) {
              commandsRan.push({db: db, cmd: cmd, opts: opts});
              return {ok: 1.0};
            },
            getReadPref: function() { return {mode: "secondaryPreferred"}; },
            getReadPrefMode: function() { return "secondaryPreferred"; }
        };

        db._mongo = mockMongo;

        // this query should not get a read pref
        t.aggregate([{$sort: {"x" : 1}}, {$out: "foo"}]);
        assert.eq(commandsRan.length, 1);
        // check that it doesn't have a read preference
        assert(!commandsRan[0].cmd.hasOwnProperty("$readPreference"));

        commandsRan = [];

        t.aggregate([{$sort: {"x" : 1}}]);
        // check another command was run
        assert.eq(commandsRan.length, 1);
        // check that it has a read preference
        assert(commandsRan[0].cmd.hasOwnProperty("$readPreference"));
    }
    finally {
        db._mongo = mongo;
    }
})();