summaryrefslogtreecommitdiff
path: root/jstests/core/evald.js
blob: 8049d2ba8ae7abefac8843ab7ac7b10c802dec6d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
t = db.jstests_evald;
t.drop();

function debug(x) {
    //    printjson( x );
}

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

function op(ev, where) {
    p = db.currentOp().inprog;
    debug(p);
    for (var i in p) {
        var o = p[i];
        if (where) {
            if (o.active && o.query && o.query.query && o.query.query.$where &&
                o.ns == "test.jstests_evald") {
                return o.opid;
            }
        } else {
            if (o.active && o.query && o.query.$eval && o.query.$eval == ev) {
                return o.opid;
            }
        }
    }
    return -1;
}

function doIt(ev, wait, where) {
    var awaitShell;

    if (where) {
        awaitShell = startParallelShell(ev);
    } else {
        awaitShell = startParallelShell("db.eval( '" + ev + "' )");
    }

    o = null;
    assert.soon(function() {
        o = op(ev, where);
        return o != -1;
    });

    if (wait) {
        sleep(2000);
    }

    debug("going to kill");

    db.killOp(o);

    debug("sent kill");

    var exitCode = awaitShell({checkExitSuccess: false});
    assert.neq(
        0, exitCode, "expected shell to exit abnormally due to JS execution being terminated");
}

// nested scope with nested invoke()
doIt("db.jstests_evald.count( { $where: function() { while(1) { sleep(1); } } } )", true, true);
doIt("db.jstests_evald.count( { $where: function() { while(1) { sleep(1); } } } )", false, true);

// simple tight loop tests with callback
doIt("while(1) { sleep(1); }", false);
doIt("while(1) { sleep(1); }", true);

// simple tight loop tests without callback
doIt("while(1) {;}", false);
doIt("while(1) {;}", true);

// the for loops are currently required, as a spawned op masks the parent op - see SERVER-1931
doIt("while(1) { for( var i = 0; i < 10000; ++i ) {;} db.jstests_evald.count({i:10}); }", true);
doIt("while(1) { for( var i = 0; i < 10000; ++i ) {;} db.jstests_evald.count({i:10}); }", false);
doIt("while(1) { for( var i = 0; i < 10000; ++i ) {;} db.jstests_evald.count(); }", true);
doIt("while(1) { for( var i = 0; i < 10000; ++i ) {;} db.jstests_evald.count(); }", false);

// try/catch with tight-loop kill tests.
// native callback with nested invoke(), drop JS exceptions
doIt("while(1) {                                  " +
         "   for(var i = 0; i < 10000; ++i) {;}       " +
         "   try {                                    " +
         "      db.jstests_evald.count({i:10});       " +
         "   } catch (e) {}                           " + "}",
     true);

// native callback, drop JS exceptions
doIt("while(1) {            " + "  try {               " + "      while(1) {      " +
         "          sleep(1);   " + "      }               " + "  } catch (e) {}      " + "}",
     true);

// no native callback and drop JS exceptions
doIt("while(1) {              " + "   try {                " + "       while(1) {;}     " +
         "   } catch (e) {}       " + "}",
     true);