summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/explain_max_time_ms.js
blob: 728d0f3c14449cd8c087f98c39f3ec9f1e0c3ce2 (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
/**
 * Tests the explain command with the maxTimeMS option.
 */
(function() {
"use strict";

const standalone = MongoRunner.runMongod();
assert.neq(null, standalone, "mongod was unable to start up");

const dbName = "test";
const db = standalone.getDB(dbName);
const collName = "explain_max_time_ms";
const coll = db.getCollection(collName);

const destCollName = "explain_max_time_ms_dest";
const mapFn = function() {
    emit(this.i, this.j);
};
const reduceFn = function(key, values) {
    return Array.sum(values);
};

coll.drop();
assert.commandWorked(db.createCollection(collName));

assert.commandWorked(coll.insert({i: 1, j: 1}));
assert.commandWorked(coll.insert({i: 2, j: 1}));
assert.commandWorked(coll.insert({i: 2, j: 2}));

// Set fail point to make sure operations with "maxTimeMS" set will time out.
assert.commandWorked(
    db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "alwaysOn"}));

for (const verbosity of ["executionStats", "allPlansExecution"]) {
    // Expect explain to time out if "maxTimeMS" is set on the aggregate command.
    assert.commandFailedWithCode(assert.throws(function() {
                                                  coll.explain(verbosity).aggregate(
                                                      [{$match: {i: 1}}], {maxTimeMS: 1});
                                              }),
                                              ErrorCodes.MaxTimeMSExpired);
    // Expect explain to time out if "maxTimeMS" is set on the count command.
    assert.commandFailedWithCode(assert.throws(function() {
                                                  coll.explain(verbosity).count({i: 1},
                                                                                {maxTimeMS: 1});
                                              }),
                                              ErrorCodes.MaxTimeMSExpired);
    // Expect explain to time out if "maxTimeMS" is set on the distinct command.
    assert.commandFailedWithCode(assert.throws(function() {
                                                  coll.explain(verbosity).distinct(
                                                      "i", {}, {maxTimeMS: 1});
                                              }),
                                              ErrorCodes.MaxTimeMSExpired);
    // Expect explain to time out if "maxTimeMS" is set on the find command.
    assert.commandFailedWithCode(assert.throws(function() {
                                                  coll.find().maxTimeMS(1).explain(verbosity);
                                              }),
                                              ErrorCodes.MaxTimeMSExpired);
    assert.commandFailedWithCode(
        assert.throws(function() {
                         coll.explain(verbosity).find().maxTimeMS(1).finish();
                     }),
                     ErrorCodes.MaxTimeMSExpired);
    // Expect explain to time out if "maxTimeMS" is set on the findAndModify command.
    assert.commandFailedWithCode(assert.throws(function() {
                                                  coll.explain(verbosity).findAndModify(
                                                      {update: {$inc: {j: 1}}, maxTimeMS: 1});
                                              }),
                                              ErrorCodes.MaxTimeMSExpired);
    // Expect explain to time out if "maxTimeMS" is set on the mapReduce command.
    assert.commandFailedWithCode(
        assert.throws(function() {
                         coll.explain(verbosity).mapReduce(
                             mapFn, reduceFn, {out: destCollName, maxTimeMS: 1});
                     }),
                     ErrorCodes.MaxTimeMSExpired);
}

// Disable fail point.
assert.commandWorked(db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "off"}));

MongoRunner.stopMongod(standalone);
})();