summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/connections_opened.js
blob: 20c53b0db1df4b7d303b10a9de40470d687b96b2 (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 serverStatus tracking of connections opened

var numPerTypeToCreate = 50;

// We need to create a new mongod to ensure no one else is talking to us in the background, and
// will screw up our stats.
var mongo = MongoRunner.runMongod({});
var db = mongo.getDB("test");

var availableConnections = db.serverStatus().connections.available;
if (availableConnections < (numPerTypeToCreate * 10)) {
    numPerTypeToCreate = Math.floor(availableConnections / 10);
}

print("numPerTypeToCreate: " + numPerTypeToCreate);

var testDB = 'connectionsOpenedTest';
var signalCollection = 'keepRunning';

function createPersistentConnection() {
    assert.soon(function() {
        try {
            permConns.push(new Mongo(db.getMongo().host));
            return true;
        } catch (x) {
            return false;
        }
    }, "Timed out waiting for persistent connection to connect", 30000, 5000);
}

function createTemporaryConnection() {
    // Retry connecting until you are successful
    var pollString = "var conn = null;" +
        "assert.soon(function() {" +
        "try { conn = new Mongo(\"" + db.getMongo().host + "\"); return conn" +
        "} catch (x) {return false;}}, " +
        "\"Timed out waiting for temporary connection to connect\", 30000, 5000);";
    // Poll the signal collection until it is told to terminate.
    pollString += "assert.soon(function() {" +
        "return conn.getDB('" + testDB + "').getCollection('" + signalCollection + "')" +
        ".findOne().stop;}, \"Parallel shell never told to terminate\", 10 * 60000);";
    return startParallelShell(pollString, null, true);
}

function waitForConnections(expectedCurrentConnections, expectedTotalConnections) {
    assert.soon(
        function() {
            var currentConnInfo = db.serverStatus().connections;
            return (expectedCurrentConnections == currentConnInfo.current) &&
                (expectedTotalConnections, currentConnInfo.totalCreated);
        },
        () => {
            return "Incorrect connection numbers. Expected " + expectedCurrentConnections +
                " current connections and " + expectedTotalConnections + " total" +
                " connections. Connection info from serverStatus: " +
                tojson(db.serverStatus().connections);
        },
        5 * 60000);
}

var originalConnInfo = db.serverStatus().connections;
assert.gt(originalConnInfo.current, 0);
assert.gt(originalConnInfo.totalCreated, 0);

jsTestLog("Creating persistent connections");
var permConns = [];
for (var i = 0; i < numPerTypeToCreate; i++) {
    permConns.push(createPersistentConnection());
}

jsTestLog("Testing that persistent connections increased the current and totalCreated counters");
waitForConnections(originalConnInfo.current + numPerTypeToCreate,
                   originalConnInfo.totalCreated + numPerTypeToCreate);

jsTestLog("Creating temporary connections");
db.getSiblingDB(testDB).dropDatabase();
db.getSiblingDB(testDB).getCollection(signalCollection).insert({stop: false});

var tempConns = [];
for (var i = 0; i < numPerTypeToCreate; i++) {
    tempConns.push(createTemporaryConnection());
}

jsTestLog("Testing that temporary connections increased the current and totalCreated counters");
waitForConnections(originalConnInfo.current + numPerTypeToCreate * 2,
                   originalConnInfo.totalCreated + numPerTypeToCreate * 2);

jsTestLog("Waiting for all temporary connections to be closed");
// Notify waiting parallel shells to terminate, causing the connection count to go back down.
db.getSiblingDB(testDB).getCollection(signalCollection).update({}, {$set: {stop: true}});
for (var i = 0; i < tempConns.length; i++) {
    tempConns[i]();  // wait on parallel shell to terminate
}

jsTestLog("Testing that current connections counter went down after temporary connections closed");
waitForConnections(originalConnInfo.current + numPerTypeToCreate,
                   originalConnInfo.totalCreated + numPerTypeToCreate * 2);

MongoRunner.stopMongod(mongo);