summaryrefslogtreecommitdiff
path: root/jstests/core/count10.js
blob: 007c4e6918bb7c52bada6d3ec46bf65e85e85921 (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
// Test that interrupting a count returns an error code.
//
// @tags: [
//   # This test attempts to perform a count command and find it using the currentOp command. The
//   # former operation may be routed to a secondary in the replica set, whereas the latter must be
//   # routed to the primary.
//   assumes_read_preference_unchanged,
//   does_not_support_stepdowns,
//
//   # Uses $where operator
//   requires_scripting,
// ]

t = db.count10;
t.drop();

for (i = 0; i < 100; i++) {
    t.save({x: i});
}

// Start a parallel shell which repeatedly checks for a count
// query using db.currentOp(). As soon as the op is found,
// kill it via db.killOp().
s = startParallelShell('assert.soon(function() {' +
                       '   current = db.currentOp({"ns": db.count10.getFullName(), ' +
                       '                           "command.count": db.count10.getName()}); ' +

                       // Check that we found the count op. If not, return false so
                       // that assert.soon will retry.
                       '   assert("inprog" in current); ' +
                       '   if (current.inprog.length === 0) { ' +
                       '       jsTest.log("count10.js: did not find count op, retrying"); ' +
                       '       printjson(current); ' +
                       '       return false; ' +
                       '   } ' +
                       '   countOp = current.inprog[0]; ' +
                       '   if (!countOp) { ' +
                       '       jsTest.log("count10.js: did not find count op, retrying"); ' +
                       '       printjson(current); ' +
                       '       return false; ' +
                       '   } ' +

                       // Found the count op. Try to kill it.
                       '   jsTest.log("count10.js: found count op:"); ' +
                       '   printjson(current); ' +
                       '   printjson(db.killOp(countOp.opid)); ' +
                       '   return true; ' +
                       '}, "count10.js: could not find count op after retrying, gave up");');

function getKilledCount() {
    try {
        db.count10.find("sleep(1000)").count();
        jsTest.log("count10.js: count op completed without being killed");
    } catch (e) {
        return e;
    }
}

var res = getKilledCount();
jsTest.log("count10.js: killed count output start");
printjson(res);
jsTest.log("count10.js: killed count output end");
assert(res);
assert(res.message.match(/count failed/) !== null);
assert(res.message.match(/\"code\"/) !== null);

s();