summaryrefslogtreecommitdiff
path: root/jstests/libs/fail_point_util.js
blob: d8592a653fb050bfe943b3203c006bf23b92c31b (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
/**
 * Utilities for turning on/off and waiting for fail points.
 */

var configureFailPoint;
var kDefaultWaitForFailPointTimeout;

(function() {
"use strict";

if (configureFailPoint) {
    return;  // Protect against this file being double-loaded.
}

kDefaultWaitForFailPointTimeout = 5 * 60 * 1000;

configureFailPoint = function(conn, failPointName, data = {}, failPointMode = "alwaysOn") {
    return {
        conn: conn,
        failPointName: failPointName,
        timesEntered: assert
                          .commandWorked(conn.adminCommand(
                              {configureFailPoint: failPointName, mode: failPointMode, data: data}))
                          .count,
        wait:
            function(maxTimeMS = kDefaultWaitForFailPointTimeout) {
                // Can only be called once because this function does not keep track of the
                // number of times the fail point is entered between the time it returns
                // and the next time it gets called.
                assert.commandWorked(conn.adminCommand({
                    waitForFailPoint: failPointName,
                    timesEntered: this.timesEntered + 1,
                    maxTimeMS: maxTimeMS
                }));
            },
        waitWithTimeout:
            function(timeoutMS) {
                // This function has three possible outcomes:
                //
                // 1) Returns true when the failpoint was hit.
                // 2) Returns false when the command returned a `MaxTimeMSExpired` response.
                // 3) Otherwise, this throws for an unexpected error.
                let res = assert.commandWorkedOrFailedWithCode(conn.adminCommand({
                    waitForFailPoint: failPointName,
                    timesEntered: this.timesEntered + 1,
                    maxTimeMS: timeoutMS
                }),
                                                               ErrorCodes.MaxTimeMSExpired);
                return res["ok"] === 1;
            },
        off:
            function() {
                assert.commandWorked(
                    conn.adminCommand({configureFailPoint: failPointName, mode: "off"}));
            }
    };
};
})();