summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/currentop_inactive_transaction_includes_last_client_info.js
blob: 25a5857d8517f18ac8d2af2194e55e090e4073c5 (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
/**
 * Tests that the object returned by currentOp() for an inactive transaction includes information
 * about the last client that has run an operation against this transaction.
 *
 * @tags: [uses_transactions]
 */

(function() {
'use strict';

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

const collName = 'currentop_last_client_info';
const dbName = 'test';
const testDB = rst.getPrimary().getDB(dbName);
const adminDB = rst.getPrimary().getDB('admin');
testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});
assert.commandWorked(testDB[collName].insert({x: 1}, {writeConcern: {w: "majority"}}));

// Start a new Session.
const lsid = assert.commandWorked(testDB.runCommand({startSession: 1})).id;
const txnNumber = NumberLong(0);
assert.commandWorked(testDB.runCommand({
    find: collName,
    lsid: lsid,
    txnNumber: txnNumber,
    readConcern: {level: "snapshot"},
    startTransaction: true,
    autocommit: false
}));

const currentOpFilter = {
    active: false,
    'lsid.id': {$eq: lsid.id},
    'client': {$exists: true}
};

let currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: currentOpFilter}]).toArray();
assert.eq(currentOp.length, 1);

let currentOpEntry = currentOp[0];
const connectionId = currentOpEntry.connectionId;
// Check that the currentOp object contains information about the last client that has run an
// operation and that its values align with our expectations.
assert.eq(currentOpEntry.appName, "MongoDB Shell");
assert.eq(currentOpEntry.clientMetadata.application.name, "MongoDB Shell");
assert.eq(currentOpEntry.clientMetadata.driver.name, "MongoDB Internal Client");

// Create a new Client and run another operation on the same session.
const otherClient = new Mongo(rst.getPrimary().host);
assert.commandWorked(otherClient.getDB(dbName).runCommand(
    {find: collName, lsid: lsid, txnNumber: txnNumber, autocommit: false}));

currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: currentOpFilter}]).toArray();
currentOpEntry = currentOp[0];
// Check that the last client that has ran an operation against this session has a different
// connectionId than the previous client.
assert.neq(currentOpEntry.connectionId, connectionId);

assert.commandWorked(testDB.adminCommand({
    commitTransaction: 1,
    lsid: lsid,
    txnNumber: txnNumber,
    autocommit: false,
    writeConcern: {w: 'majority'}
}));

rst.stopSet();
})();