summaryrefslogtreecommitdiff
path: root/jstests/sharding/group_slaveok.js
blob: 2a18cd7a22ee73730ce47675be73880bf982b060 (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
// Tests group using slaveOk
(function() {
    'use strict';

    var st = new ShardingTest(
        {name: "groupSlaveOk", shards: 1, mongos: 1, other: {rs: true, rs0: {nodes: 2}}});

    var rst = st._rs[0].test;

    // Insert data into replica set
    var conn = new Mongo(st.s.host);
    conn.setLogLevel(3);

    var coll = conn.getCollection("test.groupSlaveOk");
    coll.drop();

    var bulk = coll.initializeUnorderedBulkOp();
    for (var i = 0; i < 300; i++) {
        bulk.insert({i: i % 10});
    }
    assert.writeOK(bulk.execute());

    // Wait for client to update itself and replication to finish
    rst.awaitReplication();

    var primary = rst.getPrimary();
    var sec = rst.getSecondary();

    // Data now inserted... stop the master, since only two in set, other will still be secondary
    rst.stop(rst.getPrimary());
    printjson(rst.status());

    // Wait for the mongos to recognize the slave
    ReplSetTest.awaitRSClientHosts(conn, sec, {ok: true, secondary: true});

    // Need to check slaveOk=true first, since slaveOk=false will destroy conn in pool when
    // master is down
    conn.setSlaveOk();

    // Should not throw exception, since slaveOk'd
    assert.eq(10,
              coll.group({
                      key: {i: true},
                      reduce: function(obj, ctx) {
                          ctx.count += 1;
                      },
                      initial: {count: 0}
                  })
                  .length);

    try {
        conn.setSlaveOk(false);
        var res = coll.group({
            key: {i: true},
            reduce: function(obj, ctx) {
                ctx.count += 1;
            },
            initial: {count: 0}
        });

        print("Should not reach here! Group result: " + tojson(res));
        assert(false);
    } catch (e) {
        print("Non-slaveOk'd connection failed." + tojson(e));
    }

    st.stop();

})();