summaryrefslogtreecommitdiff
path: root/jstests/core/list_all_local_cursors.js
blob: 5cc34fd32bf4b290f0d5bdd5d73ccc141e4cd51f (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
// Basic tests for the $listLocalCursors aggregation stage.
//
// $listLocalCursors relies on in-memory state, which may not survive failovers.
// Uses features that require featureCompatibilityVersion 3.6.
// @tags: [
//   does_not_support_stepdowns,
//   requires_fcv36,
//   uses_testing_only_commands,
// ]

(function() {
    "use strict";

    const admin = db.getSisterDB("admin");
    const countAllMatchingLocalCursors = function(match) {
        return admin
            .aggregate([
                {"$listLocalCursors": {}},
                {"$match": match},
                {"$count": "matches"},
            ])
            .next()
            .matches;
    };

    let session = db.getMongo().startSession();
    let testDb = db.getSisterDB("listAllLocalCursors");
    let testDbWithSession = session.getDatabase("listAllLocalCursors");
    testDb.data.drop();
    assert.writeOK(testDb.data.insert({a: 1}));
    assert.writeOK(testDb.data.insert({a: 1}));
    let cursorIdWithSession =
        assert.commandWorked(testDbWithSession.runCommand({find: "data", batchSize: 0})).cursor.id;
    let cursorIdWithoutSession =
        assert.commandWorked(testDb.runCommand({find: "data", batchSize: 0})).cursor.id;

    // Ensure that the cache now contains the session and is visible by admin.
    const count = countAllMatchingLocalCursors({
        "ns": "listAllLocalCursors.data",
        "$or": [
            {
              "id": cursorIdWithSession,
              "lsid.id": session._serverSession.handle.getId().id,
            },
            {
              "id": cursorIdWithoutSession,
            },
        ],
    });
    assert.eq(count, 2);

    assert.commandWorked(testDbWithSession.runCommand(
        {killCursors: "data", cursors: [cursorIdWithSession, cursorIdWithoutSession]}));
    session.endSession();
})();