summaryrefslogtreecommitdiff
path: root/jstests/libs/conn_pool_helpers.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/libs/conn_pool_helpers.js')
-rw-r--r--jstests/libs/conn_pool_helpers.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/jstests/libs/conn_pool_helpers.js b/jstests/libs/conn_pool_helpers.js
new file mode 100644
index 00000000000..9beb49da1d1
--- /dev/null
+++ b/jstests/libs/conn_pool_helpers.js
@@ -0,0 +1,64 @@
+load("jstests/libs/parallelTester.js");
+
+function configureReplSetFailpoint(st, kDbName, failpoint, modeValue) {
+ st.rs0.nodes.forEach(function(node) {
+ assert.commandWorked(node.getDB("admin").runCommand({
+ configureFailPoint: failpoint,
+ mode: modeValue,
+ data: {
+ shouldCheckForInterrupt: true,
+ nss: kDbName + ".test",
+ },
+ }));
+ });
+}
+
+function launchFinds(mongos, threads, {times, readPref, shouldFail}) {
+ jsTestLog("Starting " + times + " connections");
+ for (var i = 0; i < times; i++) {
+ var thread = new Thread(function(connStr, readPref, dbName, shouldFail) {
+ var client = new Mongo(connStr);
+ const ret = client.getDB(dbName).runCommand(
+ {find: "test", limit: 1, "$readPreference": {mode: readPref}});
+
+ if (shouldFail) {
+ assert.commandFailed(ret);
+ } else {
+ assert.commandWorked(ret);
+ }
+ }, mongos.host, readPref, 'test', shouldFail);
+ thread.start();
+ threads.push(thread);
+ }
+}
+
+function assertHasConnPoolStats(mongos, allHosts, args, checkNum, connPoolStatsCmd = undefined) {
+ checkNum++;
+ jsTestLog("Check #" + checkNum + ": " + tojson(args));
+ let {ready = 0, pending = 0, active = 0, hosts = allHosts, isAbsent, checkStatsFunc} = args;
+ checkStatsFunc = checkStatsFunc ? checkStatsFunc : function(stats) {
+ return stats.available == ready && stats.refreshing == pending &&
+ (stats.inUse + stats.leased) == active;
+ };
+
+ function checkStats(res, host) {
+ let stats = res.hosts[host];
+ if (!stats) {
+ jsTestLog("Connection stats for " + host + " are absent");
+ return isAbsent;
+ }
+
+ jsTestLog("Connection stats for " + host + ": " + tojson(stats));
+ return checkStatsFunc(stats);
+ }
+
+ function checkAllStats() {
+ let cmdName = connPoolStatsCmd ? connPoolStatsCmd : "connPoolStats";
+ let res = mongos.adminCommand({[cmdName]: 1});
+ return hosts.map(host => checkStats(res, host)).every(x => x);
+ }
+
+ assert.soon(checkAllStats, "Check #" + checkNum + " failed", 10000);
+ jsTestLog("Check #" + checkNum + " successful");
+ return checkNum;
+}