summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/end_sessions_command.js
blob: 07bc9316494349be1e63d8b58212183e9aba5b26 (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
(function() {
    "use script";

    var res;
    var refresh = {refreshLogicalSessionCacheNow: 1};
    var startSession = {startSession: 1};

    // Start up a standalone server.
    var conn = MongoRunner.runMongod({nojournal: ""});
    var admin = conn.getDB("admin");
    var config = conn.getDB("config");

    // Trigger an initial refresh, as a sanity check.
    res = admin.runCommand(refresh);
    assert.commandWorked(res, "failed to refresh");

    var sessions = [];
    for (var i = 0; i < 20; i++) {
        res = admin.runCommand(startSession);
        assert.commandWorked(res, "unable to start session");
        sessions.push(res);
    }

    res = admin.runCommand(refresh);
    assert.commandWorked(res, "failed to refresh");

    assert.eq(config.system.sessions.count(), 20, "refresh should have written 20 session records");

    var endSessionsIds = [];
    for (var i = 0; i < 10; i++) {
        endSessionsIds.push(sessions[i].id);
    }
    res = admin.runCommand({endSessions: endSessionsIds});
    assert.commandWorked(res, "failed to end sessions");

    res = admin.runCommand(refresh);
    assert.commandWorked(res, "failed to refresh");

    assert.eq(config.system.sessions.count(),
              10,
              "endSessions and refresh should result in 10 remaining sessions");

    // double delete the remaining 10
    endSessionsIds = [];
    for (var i = 10; i < 20; i++) {
        endSessionsIds.push(sessions[i].id);
        endSessionsIds.push(sessions[i].id);
    }

    res = admin.runCommand({endSessions: endSessionsIds});
    assert.commandWorked(res, "failed to end sessions");

    res = admin.runCommand(refresh);
    assert.commandWorked(res, "failed to refresh");

    assert.eq(config.system.sessions.count(),
              0,
              "endSessions and refresh should result in 0 remaining sessions");

    // delete some sessions that were never created
    res = admin.runCommand({
        endSessions: [
            {"id": UUID("bacb219c-214c-47f9-a94a-6c7f434b3bae")},
            {"id": UUID("bacb219c-214c-47f9-a94a-6c7f434b3baf")}
        ]
    });

    res = admin.runCommand(refresh);
    assert.commandWorked(res, "failed to refresh");

    // verify that end on the session handle actually ends sessions
    {
        var session = conn.startSession();

        assert.commandWorked(session.getDatabase("admin").runCommand(refresh), "failed to refresh");
        assert.eq(
            config.system.sessions.count(), 1, "refresh should have written 1 session record");

        session.endSession();
        assert.commandWorked(admin.runCommand(refresh), "failed to refresh");
        assert.eq(config.system.sessions.count(),
                  0,
                  "endSessions and refresh should result in 0 remaining sessions");
    }

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