summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/expression_function_kill.js
blob: 398f08ce4a54958cf97b683ad095707e00c85be2 (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 where/function can be interrupted through maxTimeMS and query knob.
 */
(function() {
"use strict";

const mongodOptions = {};
const conn = MongoRunner.runMongod(mongodOptions);

let db = conn.getDB("test_where_function_interrupt");
let coll = db.getCollection("foo");

let expensiveFunction = function() {
    sleep(1000);
    return true;
};
assert.commandWorked(coll.insert(Array.from({length: 1000}, _ => ({}))));

let checkInterrupt = function(cursor) {
    let err = assert.throws(function() {
        cursor.itcount();
    }, [], "expected interrupt error due to maxTimeMS being exceeded");
    assert.commandFailedWithCode(
        err, [ErrorCodes.MaxTimeMSExpired, ErrorCodes.Interrupted, ErrorCodes.InternalError]);
};

let tests = [
    {
        // Test that $where can be interrupted with a maxTimeMS of 100 ms.
        timeout: 100,
        query: {$where: expensiveFunction},
        err: checkInterrupt,
    },
    {
        // Test that $function can be interrupted with a maxTimeMS of 100 ms.
        timeout: 100,
        query: {
            $expr: {
                $function: {
                    body: expensiveFunction,
                    args: [],
                    lang: 'js',
                }
            }
        },
        err: checkInterrupt
    },
    {

        // Test that $function can be interrupted by a query knob of 100 ms.
        pre: function() {
            assert.commandWorked(
                db.adminCommand({setParameter: 1, internalQueryJavaScriptFnTimeoutMillis: 100}));
        },
        query: {
            $expr: {
                $function: {
                    body: expensiveFunction,
                    args: [],
                    lang: 'js',
                }
            }
        },
        err: checkInterrupt
    },
];

tests.forEach(function(testCase) {
    if (testCase.pre) {
        testCase.pre();
    }

    let cursor = coll.find(testCase.query);

    if (testCase.timeout) {
        cursor.maxTimeMS(testCase.timeout);
    }
    testCase.err(cursor);
});

MongoRunner.stopMongod(conn);
})();