summaryrefslogtreecommitdiff
path: root/jstests/core/list_all_local_sessions.js
blob: 434e8ed660c2a324287b0efdc887e0754a187f38 (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
// Basic tests for the $listLocalSessions {allUsers: true} aggregation stage.
//
// @tags: [
//   # This test attempts to start a session and find it using the $listLocalSessions stage. The
//   # former operation must be routed to the primary in a replica set, whereas the latter may be
//   # routed to a secondary.
//   assumes_read_preference_unchanged,
//   # Sessions are asynchronously flushed to disk, so a stepdown immediately after calling
//   # startSession may cause this test to fail to find the returned sessionId.
//   does_not_support_stepdowns,
// ]

(function() {
'use strict';

const admin = db.getSiblingDB('admin');

// Get current log level.
let originalLogLevel = assert.commandWorked(admin.setLogLevel(1)).was.verbosity;

try {
    const listAllLocalSessions = function() {
        return admin.aggregate([{'$listLocalSessions': {allUsers: true}}]);
    };

    // Start a new session and capture its sessionId.
    const myid = assert.commandWorked(db.runCommand({startSession: 1})).id.id;
    assert(myid !== undefined);

    // Ensure that the cache now contains the session and is visible by admin.
    const resultArray = assert.doesNotThrow(listAllLocalSessions).toArray();
    assert.gte(resultArray.length, 1);
    const resultArrayMine = resultArray
                                .map(function(sess) {
                                    return sess._id.id;
                                })
                                .filter(function(id) {
                                    return 0 == bsonWoCompare({x: id}, {x: myid});
                                });
    assert.eq(resultArrayMine.length, 1);
} finally {
    admin.setLogLevel(originalLogLevel);
}
})();