summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/sessions_collection_upserts.js
blob: 9f9c0fe5f6a63b34a240ea856958e7f5d7ecb299 (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
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * Tests how internal writes are replicated when the systems collection is refreshed.
 *
 * @tags: [
 *  requires_replication,
 * ]
 */
(function() {
'use strict';

const kConfigSessionsNs = "config.system.sessions";

const refreshCmd = {
    refreshLogicalSessionCacheNow: 1
};

const startSessionCmd = {
    startSession: 1
};

// Refresh logical session cache and check that the number of sessions are as expected.
function refreshSessionsAndVerifyCount(config, expectedCount) {
    config.runCommand(refreshCmd);
    assert.eq(config.system.sessions.count(), expectedCount);
}

function findOplogOp(conn, operation, namespace) {
    return rst.findOplog(conn, {op: operation, ns: namespace}).toArray();
}

function createSessions(db) {
    let sessionIDs = [];
    for (let i = 0; i < 5; i++) {
        let res = db.runCommand(startSessionCmd);
        assert.commandWorked(res, "unable to start session");
        sessionIDs.push({id: res.id.id});
    }
    return sessionIDs;
}

const dbName = "test";

const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();

let testDB = primary.getDB(dbName);
let configDB = primary.getDB("config");

// Before refreshing logical session, expect that oplog is not updated with new inserts.
let ops = findOplogOp(primary, "i", kConfigSessionsNs);
assert.eq(0, ops.length);

// Refresh sessions to observe the insert of the implicit session.
refreshSessionsAndVerifyCount(configDB, 1);
ops = findOplogOp(primary, "i", kConfigSessionsNs);
jsTestLog("Implicit session inserted to sessions collection:\n" + tojson(ops));
assert.eq(1, ops.length);
const implicitSessionId = ops[0].o2._id;
const implicitSessionLastUse = ops[0].o.lastUse;

let sessionIDs = [];
(function insertAndRefreshSessions() {
    jsTestLog("Insert and refresh sessions");
    sessionIDs = createSessions(testDB);
    refreshSessionsAndVerifyCount(configDB, 6);

    ops = findOplogOp(primary, "i", kConfigSessionsNs);
    jsTestLog("New sessions inserted to sessions collection:\n" + tojson(ops));
    assert.eq(6, ops.length);

    ops = findOplogOp(primary, "u", kConfigSessionsNs);
    jsTestLog("Implicit session's lastUse time is updated:\n" + tojson(ops));
    assert.eq(1, ops.length);
    assert.eq(ops[0].o2._id, implicitSessionId);
    assert.neq(ops[0].o.diff.u.lastUse, implicitSessionLastUse);
})();

(function insertUpdateAndRefreshSessions() {
    jsTestLog("Insert new sessions and update existing sessions");
    createSessions(testDB);
    // Update lastUse of each new session we just created.
    let res = testDB.runCommand({refreshSessions: sessionIDs});
    assert.commandWorked(res, "unable to refresh session");
    refreshSessionsAndVerifyCount(configDB, 11);

    ops = findOplogOp(primary, "i", kConfigSessionsNs);
    jsTestLog("New sessions inserted to sessions collection:\n" + tojson(ops));
    assert.eq(11, ops.length);

    ops = findOplogOp(primary, "u", kConfigSessionsNs);
    jsTestLog("Implicit session's lastUse time is updated:\n" + tojson(ops));
    assert.eq(7, ops.length);
})();

rst.stopSet();
})();