summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/verify_session_cache_updates.js
blob: 6303931ef609b11ed43433eb61aaedbbaeba69e9 (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
// @tags: [requires_sharding]

(function() {
'use strict';

// This test makes assertions about the number of sessions, which are not compatible with
// implicit sessions.
TestData.disableImplicitSessions = true;

function runTest(conn) {
    for (var i = 0; i < 10; ++i) {
        conn.getDB("test").test.save({a: i});
    }

    function countSessions(conn, since) {
        conn.adminCommand({refreshLogicalSessionCacheNow: 1});
        return conn.getDB("config").system.sessions.countDocuments({lastUse: {"$gt": since}});
    }

    function getLatestSessionTime(conn) {
        conn.getDB("admin").runCommand({refreshLogicalSessionCacheNow: 1});
        let lastSession = conn.getDB("config")
                              .system.sessions.aggregate([{"$sort": {lastUse: -1}}, {$limit: 1}])
                              .toArray();
        return (lastSession.length ? lastSession[0].lastUse : new Date(0));
    }

    let origSessTime = getLatestSessionTime(conn);

    // initially we have no sessions
    assert.eq(0, countSessions(conn, origSessTime));

    // Calling startSession in the shell doesn't initiate the session
    var session = conn.startSession();
    assert.eq(0, countSessions(conn, origSessTime));

    // running a command that doesn't require auth does touch
    session.getDatabase("admin").runCommand("hello");
    assert.eq(1, countSessions(conn, origSessTime));

    // running a session updating command does touch
    session.getDatabase("admin").runCommand({serverStatus: 1});
    assert.eq(1, countSessions(conn, origSessTime));

    // running a session updating command updates last use
    {
        var lastUse = getLatestSessionTime(conn);
        sleep(200);
        session.getDatabase("admin").runCommand({serverStatus: 1});
        assert.eq(1, countSessions(conn, origSessTime));
        assert.gt(getLatestSessionTime(conn), lastUse);
    }

    // verify that reading from a cursor updates last use
    {
        var cursor = session.getDatabase("test").test.find({}).batchSize(1);
        cursor.next();
        var lastUse = getLatestSessionTime(conn);
        sleep(200);
        assert.eq(1, countSessions(conn, origSessTime));
        cursor.next();
        assert.gt(getLatestSessionTime(conn), lastUse);
    }

    session.endSession();
}

{
    var mongod = MongoRunner.runMongod({nojournal: ""});
    runTest(mongod);
    MongoRunner.stopMongod(mongod);
}

{
    var st = new ShardingTest({shards: 1, mongos: 1, config: 1});
    st.rs0.getPrimary().getDB("admin").runCommand({refreshLogicalSessionCacheNow: 1});

    runTest(st.s0);
    st.stop();
}
})();