summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorTed Tuckman <ted.tuckman@mongodb.com>2018-09-24 16:27:58 -0400
committerTed Tuckman <ted.tuckman@mongodb.com>2018-10-04 11:22:05 -0400
commitc589abf31b748d0f13e710d8d59394b65ab6891d (patch)
tree637288a6b560f8906bc0dd6ff3bf1b55eb709b79 /jstests
parentf9436146f64ad7caa2ce525c1d75a85f918d68c0 (diff)
downloadmongo-c589abf31b748d0f13e710d8d59394b65ab6891d.tar.gz
SERVER-37005 Add fields to GenericCursor
Diffstat (limited to 'jstests')
-rw-r--r--jstests/core/currentop_cursors.js188
-rw-r--r--jstests/noPassthrough/currentop_active_cursor.js1
2 files changed, 156 insertions, 33 deletions
diff --git a/jstests/core/currentop_cursors.js b/jstests/core/currentop_cursors.js
index b4e0949fac9..e8fad63f8bb 100644
--- a/jstests/core/currentop_cursors.js
+++ b/jstests/core/currentop_cursors.js
@@ -2,44 +2,168 @@
* Tests that an idle cursor will appear in the $currentOp output if the idleCursors option is
* set to true.
*
- * @tags: [assumes_read_concern_unchanged]
+ * @tags: [assumes_read_concern_unchanged, requires_capped]
*/
(function() {
"use strict";
const coll = db.jstests_currentop;
- const adminDB = db.getSiblingDB("admin");
- coll.drop();
+ // Avoiding using the shell helper to avoid the implicit collection recreation.
+ db.runCommand({drop: coll.getName()});
+ assert.commandWorked(db.createCollection(coll.getName(), {capped: true, size: 1000}));
for (let i = 0; i < 5; ++i) {
assert.commandWorked(coll.insert({"val": i}));
}
- const findOut =
- assert.commandWorked(db.runCommand({find: "jstests_currentop", batchSize: 2})).cursor.id;
- const result = adminDB
- .aggregate([
- {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
- {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
- ])
- .toArray();
- assert.eq(result.length, 1, result);
- assert.eq(result[0].cursor.nDocsReturned, 2, result);
- assert.eq(result[0].cursor.tailable, false, result);
- assert.eq(result[0].cursor.awaitData, false, result);
- assert.eq(result[0].cursor.noCursorTimeout, false, result);
- assert.eq(result[0].cursor.ns, coll.getFullName(), result);
- assert.eq(result[0].cursor.originatingCommand.find, "jstests_currentop", result);
- assert.eq(result[0].cursor.originatingCommand.batchSize, 2, result);
-
- const noIdle = adminDB
- .aggregate([
- {$currentOp: {allUsers: false, idleCursors: false}},
- {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
- ])
- .toArray();
- assert.eq(noIdle.length, 0, tojson(noIdle));
- const noFlag =
- adminDB.aggregate([{$currentOp: {allUsers: false}}, {$match: {type: "idleCursor"}}])
- .toArray();
-
- assert.eq(noIdle.length, 0, tojson(noFlag));
+ /**
+ * runTest creates a new collection called jstests_currentop and then runs the provided find
+ * query. It calls $currentOp and does some basic assertions to make sure idleCursors is
+ * behaving as intended in each case.
+ * findFunc: A function that runs a find query. Is expected to return a cursorID.
+ * Arbitrary code can be run in findFunc as long as it returns a cursorID.
+ * assertFunc: A function that runs assertions against the results of the $currentOp.
+ * Takes the following arguments
+ * 'findOut': The cursorID returned from findFunc.
+ * 'result': The results from running $currenpOp as an array of JSON objects.
+ * Arbitrary code can be run in assertFunc, and there is no return value needed.
+ */
+ function runTest({findFunc, assertFunc}) {
+ const adminDB = db.getSiblingDB("admin");
+ const findOut = findFunc();
+ const result =
+ adminDB
+ .aggregate([
+ {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
+ {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
+ ])
+ .toArray();
+ assert.eq(result[0].cursor.ns, coll.getFullName(), result);
+ assert.eq(result[0].cursor.originatingCommand.find, coll.getName(), result);
+ assertFunc(findOut, result);
+ const noIdle =
+ adminDB
+ .aggregate([
+ {$currentOp: {allUsers: false, idleCursors: false}},
+ {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": findOut}]}}
+ ])
+ .toArray();
+ assert.eq(noIdle.length, 0, tojson(noIdle));
+ const noFlag =
+ adminDB.aggregate([{$currentOp: {allUsers: false}}, {$match: {type: "idleCursor"}}])
+ .toArray();
+
+ assert.eq(noIdle.length, 0, tojson(noFlag));
+ }
+
+ runTest({
+ findFunc: function() {
+ return assert.commandWorked(db.runCommand({find: "jstests_currentop", batchSize: 2}))
+ .cursor.id;
+ },
+ assertFunc: function(cursorId, result) {
+ assert.eq(result.length, 1, result);
+ const idleCursor = result[0].cursor;
+ assert.eq(idleCursor.nDocsReturned, 2, result);
+ assert.eq(idleCursor.nBatchesReturned, 1, result);
+ assert.eq(idleCursor.tailable, false, result);
+ assert.eq(idleCursor.awaitData, false, result);
+ assert.eq(idleCursor.noCursorTimeout, false, result);
+ assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
+ assert.lte(idleCursor.createdDate, idleCursor.lastAccessDate, result);
+ }
+ });
+ runTest({
+ findFunc: function() {
+ return assert
+ .commandWorked(db.runCommand({
+ find: "jstests_currentop",
+ batchSize: 2,
+ tailable: true,
+ awaitData: true,
+ noCursorTimeout: true
+ }))
+ .cursor.id;
+ },
+ assertFunc: function(cursorId, result) {
+
+ assert.eq(result.length, 1, result);
+ const idleCursor = result[0].cursor;
+ assert.eq(idleCursor.tailable, true, result);
+ assert.eq(idleCursor.awaitData, true, result);
+ assert.eq(idleCursor.noCursorTimeout, true, result);
+ assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
+ }
+ });
+ runTest({
+ findFunc: function() {
+ return assert.commandWorked(db.runCommand({find: "jstests_currentop", batchSize: 2}))
+ .cursor.id;
+ },
+ assertFunc: function(cursorId, result) {
+ const secondCursor =
+ assert.commandWorked(db.runCommand({find: "jstests_currentop", batchSize: 2}));
+ const adminDB = db.getSiblingDB("admin");
+ const secondResult =
+ adminDB
+ .aggregate([
+ {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
+ {
+ $match: {
+ $and: [
+ {type: "idleCursor"},
+ {"cursor.cursorId": secondCursor.cursor.id}
+ ]
+ }
+ }
+ ])
+ .toArray();
+ assert.lt(result[0].cursor.createdDate, secondResult[0].cursor.createdDate, function() {
+ return tojson(result) + tojson(secondResult);
+ });
+
+ }
+ });
+
+ runTest({
+ findFunc: function() {
+ return assert
+ .commandWorked(
+ db.runCommand({find: "jstests_currentop", batchSize: 4, noCursorTimeout: true}))
+ .cursor.id;
+ },
+ assertFunc: function(cursorId, result) {
+ const idleCursor = result[0].cursor;
+ assert.eq(result.length, 1, result);
+ assert.eq(idleCursor.nDocsReturned, 4, result);
+ assert.eq(idleCursor.nBatchesReturned, 1, result);
+ assert.eq(idleCursor.noCursorTimeout, true, result);
+ assert.eq(idleCursor.originatingCommand.batchSize, 4, result);
+ }
+ });
+
+ runTest({
+ findFunc: function() {
+ return assert.commandWorked(db.runCommand({find: "jstests_currentop", batchSize: 2}))
+ .cursor.id;
+ },
+ assertFunc: function(cursorId, result) {
+ const adminDB = db.getSiblingDB("admin");
+ const originalAccess = result[0].cursor.lastAccessDate;
+ assert.commandWorked(
+ db.runCommand({getMore: cursorId, collection: "jstests_currentop", batchSize: 2}));
+ result =
+ adminDB
+ .aggregate([
+ {$currentOp: {localOps: true, allUsers: false, idleCursors: true}},
+ {$match: {$and: [{type: "idleCursor"}, {"cursor.cursorId": cursorId}]}}
+ ])
+ .toArray();
+ const idleCursor = result[0].cursor;
+ assert.eq(idleCursor.nDocsReturned, 4, result);
+ assert.eq(idleCursor.nBatchesReturned, 2, result);
+ assert.eq(idleCursor.originatingCommand.batchSize, 2, result);
+ assert.lt(idleCursor.createdDate, idleCursor.lastAccessDate, result);
+ assert.lt(originalAccess, idleCursor.lastAccessDate, result);
+ }
+ });
+
})();
diff --git a/jstests/noPassthrough/currentop_active_cursor.js b/jstests/noPassthrough/currentop_active_cursor.js
index c8375f17e5e..a7c6f40a6db 100644
--- a/jstests/noPassthrough/currentop_active_cursor.js
+++ b/jstests/noPassthrough/currentop_active_cursor.js
@@ -29,7 +29,6 @@
const cursorObject = activeCursors[0].cursor;
assert.eq(cursorObject.originatingCommand.find, coll.getName(), tojson(activeCursors));
assert.eq(cursorObject.nDocsReturned, 2, tojson(activeCursors));
- assert.eq(cursorObject.ns, coll.getFullName(), tojson(activeCursors));
assert.eq(cursorObject.tailable, false, tojson(activeCursors));
assert.eq(cursorObject.awaitData, false, tojson(activeCursors));
}