summaryrefslogtreecommitdiff
path: root/jstests/core/currentop.js
blob: 636fdee2cb380fcc94b6ff03d34896ffd326ba60 (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
/**
 * Tests that long-running operations show up in currentOp and report the locks they are holding.
 *
 * @tags: [
 *   assumes_superuser_permissions,
 *   # fsync command is not available on embedded
 *   incompatible_with_embedded,
 * ]
 */

(function() {
"use strict";
const coll = db.jstests_currentop;
coll.drop();

// We fsync+lock the server to cause all subsequent write operations to block.
assert.commandWorked(db.fsyncLock());

const awaitInsertShell = startParallelShell(function() {
    assert.writeOK(db.jstests_currentop.insert({}));
});

// Wait until the write appears in the currentOp output reporting that it is waiting for a lock.
assert.soon(
    function() {
        var lock_type = "";
        if (jsTest.options().storageEngine === "mobile") {
            lock_type = "W";
        } else {
            lock_type = "w";
        }
        const ops = db.currentOp({
            $and: [
                {"locks.Global": lock_type, waitingForLock: true},
                // Depending on whether CurOp::setNS_inlock() has been called, the "ns" field
                // may either be the full collection name or the command namespace.
                {
                    $or: [
                        {ns: coll.getFullName()},
                        {ns: db.$cmd.getFullName(), "command.insert": coll.getName()}
                    ]
                },
                {type: "op"}
            ]
        });
        return ops.inprog.length === 1;
    },
    function() {
        return "Failed to find blocked insert in currentOp() output: " + tojson(db.currentOp());
    });

// Unlock the server and make sure the write finishes.
const fsyncResponse = assert.commandWorked(db.fsyncUnlock());
assert.eq(fsyncResponse.lockCount, 0);
awaitInsertShell();
}());